Merge pull request #390 from nirg/monitor_rate_limit
authorAaron1011 <aa1ronham@gmail.com>
Fri, 18 Apr 2014 18:57:28 +0000 (14:57 -0400)
committerAaron1011 <aa1ronham@gmail.com>
Fri, 18 Apr 2014 18:57:28 +0000 (14:57 -0400)
Added monitoring of api rate limits and waiting for replenishment.

1  2 
tweepy/api.py
tweepy/binder.py

diff --cc tweepy/api.py
Simple merge
index 1f8c61960a2c9a14f8b75395428e50bf8fe577b9,7d79908cd8f4e1ad87c4ed65ec286c5a55a09559..2737bd79c0b2dcd6ff4050c2083b59b830a858a2
@@@ -153,17 -165,33 +165,33 @@@ def bind_api(**config)
                  try:
                      conn.request(self.method, url, headers=self.headers, body=self.post_data)
                      resp = conn.getresponse()
 -                except Exception, e:
 +                except Exception as e:
                      raise TweepError('Failed to send request: %s' % e)
+                 
+                 if self.wait_on_rate_limit:
+                     rem_calls = resp.getheader('x-rate-limit-remaining')
+                     if rem_calls is not None:
+                         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) 
+                     if rem_calls == 0 and (resp.status == 429 or resp.status == 420): # if ran out of calls before waiting switching retry last call
+                         continue
+                 retry_delay = self.retry_delay
                  # Exit request loop if non-retry error code
-                 if self.retry_errors:
-                     if resp.status not in self.retry_errors: break
-                 else:
-                     if resp.status == 200: break
+                 if resp.status == 200:
+                     break
+                 elif (resp.status == 429 or resp.status == 420) and self.wait_on_rate_limit:
+                     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 
  
                  # Sleep before retrying request again
-                 time.sleep(self.retry_delay)
+                 time.sleep(retry_delay)
                  retries_performed += 1
  
              # If an error was returned, throw an exception