added feature for printing remaining time when max retries reached
authorCody Coats <cpcoats@gmail.com>
Fri, 18 Apr 2014 23:20:35 +0000 (19:20 -0400)
committerCody Coats <cpcoats@gmail.com>
Fri, 18 Apr 2014 23:20:35 +0000 (19:20 -0400)
tweepy/api.py
tweepy/binder.py

index d7198bb11cc95ca431becef3ca8b6c0dee374883..401162d5ee8f19f7c3c77c3e0fd6a2eafaa2f89c 100644 (file)
@@ -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
-
index f47998d1f3c72b27b5607973bdded142d63e9f72..6469f4d902c607741e778b0486ff6abad469bb12 100644 (file)
@@ -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
-