简体   繁体   中英

twython search api rate limit: Header information will not be updated

I want to handle the Search-API rate limit of 180 requests / 15 minutes. The first solution I came up with was to check the remaining requests in the header and wait 900 seconds. See the following snippet:

results = search_interface.cursor(search_interface.search, q=k, lang=lang, result_type=result_mode)

while True:
    try:
        tweet = next(results)
        if limit_reached(search_interface):
            sleep(900)

        self.writer(tweet)


def limit_reached(search_interface):
    remaining_rate = int(search_interface.get_lastfunction_header('X-Rate-Limit-Remaining'))
    return remaining_rate <= 2

But it seems, that the header information are not reseted to 180 after it reached the two remaining requests.

The second solution I came up with was to handle the twython exception for rate limitation and wait the remaining amount of time:

results = search_interface.cursor(search_interface.search, q=k, lang=lang, result_type=result_mode)
while True:
    try:
        tweet = next(results)

        self.writer(tweet)
    except TwythonError as inst:
        logger.error(inst.msg)
        wait_for_reset(search_interface)
        continue
    except StopIteration:
        break


def wait_for_reset(search_interface):
      reset_timestamp = int(search_interface.get_lastfunction_header('X-Rate-Limit-Reset'))
      now_timestamp = datetime.now().timestamp()
      seconds_offset = 10

      t = reset_timestamp - now_timestamp + seconds_offset
      logger.info('Waiting {0} seconds for Twitter rate limit reset.'.format(t))
      sleep(t)

But with this solution I receive this message INFO: Resetting dropped connection: api.twitter.com" and the loop will not continue with the last element of the generator. Have somebody faced the same problems?

Regards.

just rate limit yourself is my suggestion (assuming you are constantly hitting the limit ...)

QUERY_PER_SEC = 15*60/180.0  #180 per 15 minutes
#~5 seconds per query
class TwitterBot:
    last_update=0
    def doQuery(self,*args,**kwargs):
        tdiff = time.time()-self.last_update
        if tdiff < QUERY_PER_SEC:
            time.sleep(QUERY_PER_SEC-tdiff) 
        self.last_update = time.time()
        return search_interface.cursor(*args,**kwargs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM