Skip to content

Instantly share code, notes, and snippets.

@jcrubino
Created November 16, 2014 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcrubino/6ee3d5b749ed950185f3 to your computer and use it in GitHub Desktop.
Save jcrubino/6ee3d5b749ed950185f3 to your computer and use it in GitHub Desktop.
def GetSleepTime(self, resources):
'''Determines the minimum number of seconds that a program must wait
before hitting the server again without exceeding the rate_limit
imposed for the currently authenticated user.
Returns:
The minimum seconds that the api must have to sleep before query again
'''
if resources[0] == '/':
resources = resources[1:]
resource_families = resources[:resources.find('/')] if '/' in resources else resources
rate_status = self.GetRateLimitStatus(resource_families)
try:
reset_time = rate_status['resources'][resource_families]['/' + resources]['reset']
remaining = rate_status['resources'][resource_families]['/' + resources]['remaining']
except:
raise TwitterError({'message': 'Wrong resources'})
if remaining == 0:
utc_now = datetime.datetime.utcnow()
utc_stuct = utc_now.timetuple()
current_time = timegm(utc_stuct)
delta = reset_time - current_time
if delta < 0:
return 0
else:
return delta
else:
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment