Created
November 16, 2014 07:30
-
-
Save jcrubino/6ee3d5b749ed950185f3 to your computer and use it in GitHub Desktop.
This file contains 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
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