Skip to content

Instantly share code, notes, and snippets.

@matbor
Created February 2, 2015 01:51
Show Gist options
  • Select an option

  • Save matbor/7314579ebcfd8fe734cb to your computer and use it in GitHub Desktop.

Select an option

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.
# 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