Merge pull request #50 from mckellister/master
Handling rate limiting errors from search api
This commit is contained in:
commit
d93651c486
1 changed files with 20 additions and 0 deletions
|
|
@ -93,6 +93,18 @@ class APILimit(TwythonError):
|
|||
def __str__(self):
|
||||
return repr(self.msg)
|
||||
|
||||
class RateLimitError(TwythonError):
|
||||
"""
|
||||
Raised when you've hit a rate limit. retry_wait_seconds is the number of seconds to
|
||||
wait before trying again.
|
||||
"""
|
||||
def __init__(self, msg, retry_wait_seconds, error_code):
|
||||
self.retry_wait_seconds = int(retry_wait_seconds)
|
||||
TwythonError.__init__(self, msg, error_code)
|
||||
|
||||
def __str__(self):
|
||||
return repr(self.msg)
|
||||
|
||||
|
||||
class AuthError(TwythonError):
|
||||
"""
|
||||
|
|
@ -308,6 +320,14 @@ class Twython(object):
|
|||
searchURL = Twython.constructApiURL("http://search.twitter.com/search.json", kwargs)
|
||||
try:
|
||||
resp, content = self.client.request(searchURL, "GET", headers = self.headers)
|
||||
|
||||
if int(resp.status) == 420:
|
||||
retry_wait_seconds = resp['retry-after']
|
||||
raise RateLimitError("getSearchTimeline() is being rate limited. Retry after %s seconds." %
|
||||
retry_wait_seconds,
|
||||
retry_wait_seconds,
|
||||
resp.status)
|
||||
|
||||
return simplejson.loads(content.decode('utf-8'))
|
||||
except HTTPError, e:
|
||||
raise TwythonError("getSearchTimeline() failed with a %s error code." % `e.code`, e.code)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue