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
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 """
}
return headers, body
-
# 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
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)
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
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)
_call.pagination_mode = 'page'
return _call
-