So we all know that twitter can get over capacity at times. I
wanted a way to work around that, but also to be reasonably gentle on
the server.
This meta-wrapper will try 3 times to do the api call before giving
up. Its sort of rough and could be fleshed out with some custom
exceptions etc.
It is designed to be used with http://mike.verdone.ca/twitter/
if the following gets munged, it can also be viewed here for a time:
http://dpaste.com/18789/
If you are not using it in a class - remove the reference to self.
You call it use it basically like this:
from twitter.api import Twitter, TwitterError
twitter = Twitter('name','pw')
json = self.api_repeater(self.twitter.users.show,user_id=userid)
def api_repeater(self,api_call,**kwargs):
repeat a call to the twitter api when their servers are busy
tries = 0
while True:
try:
return api_call(**kwargs)
except TwitterError, e:
error_s = str(e.args[0]).split(':', 1)[1]
#print error_s
try:
error_dict = json.loads(error_s)
err = error_dict['error']
except:
err = str(e.args[0])
if 'Twitter is over capacity' in err:
if tries 3:
raise RuntimeError Twitter too busy
err = 'Twitter is over capacity. ' + str (tries)
tries =+ 1
print err
time.sleep(1)
else:
raise
except:
print sys.exc_info()[0]
raise