def rate_limit(resp):
 if ('X-RateLimit-Remaining' not in resp.headers):
  log.info('No rate limit detected. Hum along...')
  return
 remain = int(resp.headers['X-RateLimit-Remaining'])
 limit = int(resp.headers['X-RateLimit-Limit'])
 log.info('Rate limiter: %s allowed out of %d', remain, limit)
 if (remain > 1):
  return
 reset = int(resp.headers['X-RateLimit-Reset'])
 ts = datetime.fromtimestamp(reset)
 delay = (ts - datetime.now()).total_seconds()
 log.info('Hit rate limit. Have to wait for %d seconds', delay)
 if (delay < 0):
  delay = 2
 time.sleep(delay)

def rate_limit ( resp ) : if ( 'X-RateLimit-Remaining' not in resp . headers ) : log . info ( 'No rate limit detected. Hum along...' ) return remain = int ( resp . headers [ 'X-RateLimit-Remaining' ] ) limit = int ( resp . headers [ 'X-RateLimit-Limit' ] ) log . info ( 'Rate limiter: %s allowed out of %d' , remain , limit ) if ( remain > 1 ) : return reset = int ( resp . headers [ 'X-RateLimit-Reset' ] ) ts = datetime . fromtimestamp ( reset ) delay = ( ts - datetime . now ( ) ) . total_seconds ( ) log . info ( 'Hit rate limit. Have to wait for %d seconds' , delay ) if ( delay < 0 ) : delay = 2 time . sleep ( delay )
rate limiting decorator .