Created
February 2, 2015 01:51
-
-
Save matbor/7314579ebcfd8fe734cb to your computer and use it in GitHub Desktop.
Just playing with tweepy / twitter api and apilimits. This example gets the user_timeline remaining.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # how to check api limits with tweeepy | |
| #http://tweepy.readthedocs.org/ | |
| import tweepy # pip install tweepy | |
| import time | |
| # Consumer keys and access tokens, used for OAuth | |
| consumer_key = '' | |
| consumer_secret = '' | |
| access_token = '' | |
| access_token_secret = '' | |
| # OAuth process, using the keys and tokens | |
| auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
| auth.set_access_token(access_token, access_token_secret) | |
| api = tweepy.API(auth) | |
| #print full text | |
| #print(limits) | |
| while True: | |
| print('') | |
| print(time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(time.time()))) | |
| try: | |
| limits = api.rate_limit_status() | |
| #for usertime line api limits -- typicaly 150 hits per 15mins | |
| usertimeline_remain = limits['resources']['statuses']['/statuses/user_timeline']['remaining'] | |
| usertimeline_limit = limits['resources']['statuses']['/statuses/user_timeline']['limit'] | |
| usertimeline_time = limits['resources']['statuses']['/statuses/user_timeline']['reset'] | |
| usertimeline_time_local = time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime(usertimeline_time)) | |
| print('Twitter API: remain: %s / limit: %s - reseting at: %s' %(usertimeline_remain, usertimeline_limit, usertimeline_time_local)) | |
| print('response: %s' % (getattr(api, 'last_response', None))) #just get the response, 429 = rate limit exceeded / https://dev.twitter.com/overview/api/response-codes | |
| except: | |
| print('nope couldn\'t get them') | |
| print('response: %s' % (getattr(api, 'last_response', None))) #just get the response, 429 = rate limit exceeded / https://dev.twitter.com/overview/api/response-codes | |
| print(limits) | |
| time.sleep(30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment