Commit 49f47913 authored by Cody Coats's avatar Cody Coats
Browse files

added feature for printing remaining time when max retries reached

parent 295552ce
......@@ -18,7 +18,8 @@ class API(object):
host='api.twitter.com', search_host='search.twitter.com',
cache=None, secure=True, api_root='/1.1', search_root='',
retry_count=0, retry_delay=0, retry_errors=None, timeout=60,
parser=None, compression=False, wait_on_rate_limit=False):
parser=None, compression=False, wait_on_rate_limit=False,
wait_on_rate_limit_notify=False):
self.auth = auth_handler
self.host = host
self.search_host = search_host
......@@ -32,6 +33,7 @@ class API(object):
self.retry_errors = retry_errors
self.timeout = timeout
self.wait_on_rate_limit = wait_on_rate_limit
self.wait_on_rate_limit_notify = wait_on_rate_limit_notify
self.parser = parser or ModelParser()
""" statuses/home_timeline """
......@@ -738,4 +740,3 @@ class API(object):
}
return headers, body
......@@ -68,7 +68,7 @@ def bind_api(**config):
# This causes Twitter to issue 301 redirect.
# See Issue https://github.com/tweepy/tweepy/issues/12
self.headers['Host'] = self.host
# Monitoring rate limits
self._remaining_calls = None
self._reset_time = None
......@@ -142,8 +142,10 @@ def bind_api(**config):
self._remaining_calls is not None and self._remaining_calls < 1:
sleep_time = self._reset_time - int(time.time())
if sleep_time > 0:
time.sleep(sleep_time + 5) # sleep for few extra sec
if self.wait_on_rate_limit_notify:
print "Max retries reached. Sleeping for: " + str(sleep_time)
time.sleep(sleep_time + 5) # sleep for few extra sec
# Open connection
if self.api.secure:
conn = httplib.HTTPSConnection(self.host, timeout=self.api.timeout)
......@@ -167,15 +169,15 @@ def bind_api(**config):
resp = conn.getresponse()
except Exception as e:
raise TweepError('Failed to send request: %s' % e)
rem_calls = resp.getheader('x-rate-limit-remaining')
if rem_calls is not None:
self._remaining_calls = int(rem_calls)
self._remaining_calls = int(rem_calls)
elif isinstance(self._remaining_calls, int):
self._remaining_calls -= 1
reset_time = resp.getheader('x-rate-limit-reset')
if reset_time is not None:
self._reset_time = int(reset_time)
self._reset_time = int(reset_time)
if self.wait_on_rate_limit and self._remaining_calls == 0 and (resp.status == 429 or resp.status == 420): # if ran out of calls before waiting switching retry last call
continue
......@@ -187,7 +189,7 @@ def bind_api(**config):
if 'retry-after' in resp.msg:
retry_delay = float(resp.msg['retry-after'])
elif self.retry_errors and resp.status not in self.retry_errors:
break
break
# Sleep before retrying request again
time.sleep(retry_delay)
......@@ -236,4 +238,3 @@ def bind_api(**config):
_call.pagination_mode = 'page'
return _call
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment