TwythonRateLimitError.retry_after is always None. #317

Closed
opened 2014-04-23 21:46:57 -07:00 by dmtucker · 1 comment
dmtucker commented 2014-04-23 21:46:57 -07:00 (Migrated from github.com)

According to the Twython documentation (which, by the way, does not match the version on PyPI), "The amount of seconds to retry your request in will be appended to" a TwythonRateLimitError; however, I have never observed this. I think it is because of changes in Twitter API 1.1 (see HTTP Headers and Response Codes) that contrast with version 1. I'm not sure if this is the only place a TwythonRateLimitError is raised, but on line 178 of api.py, the incorrect header ('retry-after' instead of 'X-Rate-Limit-Reset') is retrieved and passed to TwythonRateLimitError. This causes TwythonRateLimitError.retry_after to always be None.

The script below demonstrates the issue (assuming the API keys are set in the environment and currently being rate-limited, which I have a script to do if you want).

#!/usr/bin/env python

import os
import datetime
from twython import Twython


def main():
    twitter = Twython(
        os.environ.get('TWITTER_CONSUMER_KEY'),
        access_token=os.environ.get('TWITTER_ACCESS_TOKEN')
    )
    try:
        results = twitter.search(q='test')
    except Exception as e:  # This should probably be TwythonRateLimitError.
        if e.retry_after is not None:
            print('e.retry_after: '+str(e.retry_after))
        else:
            print(
                'retry_after: ' +
                str(twitter.get_lastfunction_header('retry_after'))
            )
            print(
                'X-Rate-Limit-Reset: ' +
                str(twitter.get_lastfunction_header('X-Rate-Limit-Reset'))
            )
        raise
    finally:
        print(
            datetime.datetime.fromtimestamp(
                int(twitter.get_lastfunction_header('X-Rate-Limit-Reset'))
            ).strftime('%Y-%m-%d %H:%M:%S')
        )


if __name__ == "__main__":
    main()

Twython is great! I am currently using it to build a retriever that returns geotagged Tweets in GeoJSON format. Just out of curiosity, when is the next release planned for PyPI?

According to the [Twython documentation](https://twython.readthedocs.org/en/latest/api.html#exceptions) (which, by the way, does not match the version on [PyPI](https://pypi.python.org/pypi/twython)), "The amount of seconds to retry your request in will be appended to" a `TwythonRateLimitError`; however, I have never observed this. I think it is because of changes in [Twitter API 1.1](https://dev.twitter.com/docs/rate-limiting/1.1) (see HTTP Headers and Response Codes) that contrast with [version 1](https://dev.twitter.com/docs/rate-limiting/1). I'm not sure if this is the only place a `TwythonRateLimitError` is raised, but on line 178 of [api.py](https://github.com/ryanmcgrath/twython/blob/master/twython/api.py), the incorrect header ('retry-after' instead of 'X-Rate-Limit-Reset') is retrieved and passed to `TwythonRateLimitError`. This causes `TwythonRateLimitError.retry_after` to always be `None`. The script below demonstrates the issue (assuming the API keys are set in the environment and currently being rate-limited, which I have a script to do if you want). ``` #!/usr/bin/env python import os import datetime from twython import Twython def main(): twitter = Twython( os.environ.get('TWITTER_CONSUMER_KEY'), access_token=os.environ.get('TWITTER_ACCESS_TOKEN') ) try: results = twitter.search(q='test') except Exception as e: # This should probably be TwythonRateLimitError. if e.retry_after is not None: print('e.retry_after: '+str(e.retry_after)) else: print( 'retry_after: ' + str(twitter.get_lastfunction_header('retry_after')) ) print( 'X-Rate-Limit-Reset: ' + str(twitter.get_lastfunction_header('X-Rate-Limit-Reset')) ) raise finally: print( datetime.datetime.fromtimestamp( int(twitter.get_lastfunction_header('X-Rate-Limit-Reset')) ).strftime('%Y-%m-%d %H:%M:%S') ) if __name__ == "__main__": main() ``` Twython is great! I am currently using it to build a retriever that returns geotagged Tweets in GeoJSON format. Just out of curiosity, when is the next release planned for PyPI?
michaelhelmick commented 2014-04-25 15:51:07 -07:00 (Migrated from github.com)

I've been trying to get around to find some time to merge some PRs. Tomorrow I have a full day of work that I need to get done. But I'll try to set the first hour aside to fixing, testing and pushing up a new version of Twython

I've been trying to get around to find some time to merge some PRs. Tomorrow I have a full day of work that I need to get done. But I'll try to set the first hour aside to fixing, testing and pushing up a new version of Twython
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: code/twython#317
No description provided.