python-twitter icon indicating copy to clipboard operation
python-twitter copied to clipboard

Rate limit proble with DestroyFavorite

Open fpom opened this issue 8 years ago • 2 comments
trafficstars

It looks like rate limit is not correctly handled for method DestroyFavorite, for instance:

In [2]: api.CheckRateLimit("/favorites/destroy.json")
Out[2]: EndpointRateLimit(limit=15, remaining=15, reset=0)

In [3]: api.DestroyFavorite(status_id=774150870963396608)
Out[3]: Status(ID=774150870963396608, ....)

In [4]: api.CheckRateLimit("/favorites/destroy.json")
Out[4]: EndpointRateLimit(limit=0, remaining=0, reset=0)

On first call at [2] reset is 0 which is surprising, but I assume it is not initialised until the first call to the method. But then, on [4] the result from CheckRateLimit looks completely wrong, it should say 14 for remaining, 15 for limit, and not 0 for reset.

This is really a big issue when mass-deleting favorites, either we hit the rate limit or we sleep for nothing...

I guess this comes from lines 4945-4950 in api.py:

        if url and self.rate_limit:
            limit = resp.headers.get('x-rate-limit-limit', 0)
            remaining = resp.headers.get('x-rate-limit-remaining', 0)
            reset = resp.headers.get('x-rate-limit-reset', 0)

Is it possible that Twitter does not send the x-rate-limit headers or something?

Thanks for your help, Franck

fpom avatar Mar 09 '17 13:03 fpom

I'm getting real weird behavior with that endpoint. I'm going to take a closer look this weekend. Thanks for the report.

jeremylow avatar Mar 11 '17 23:03 jeremylow

Just in case this helps, I've "patched" this way:

class TwitterApi (twitter.Api) :
    def DestroyFavorite (self, status=None, status_id=None, include_entities=True) :
        try :
            sleep_on_rate_limit = self.sleep_on_rate_limit
            self.sleep_on_rate_limit = False
            return twitter.Api.DestroyFavorite(self, status, status_id, include_entities)
            self.sleep_on_rate_limit = sleep_on_rate_limit
        except twitter.TwitterError as err :
            if err.message[0]["code"] == 88 :
                time.sleep(15*60)
                return twitter.Api.DestroyFavorite(self, status, status_id, include_entities)
            raise

fpom avatar Mar 15 '17 15:03 fpom