Added error handling when API calls timeout

This commit is contained in:
Anna
2010-05-04 14:32:43 -04:00
parent 6044fa27d2
commit f37246f30b
3 changed files with 66 additions and 32 deletions

View File

@ -4,6 +4,7 @@ import re
import gtk
from threading import Thread,RLock
from twitter import Api
from urllib2 import HTTPError,URLError
class SafeApi(Api):
''' This is just a Twitter API with an RLock for multi-threaded access '''
@ -28,34 +29,37 @@ class GetTweets(Thread):
def run(self):
with self.api.lock:
try:
# username/Home entries need to load the appropriate Home feed
if self.list_name == self.username + '/Home':
statuses = self.api.GetHomeTimeline(count=self.num_entries)
# username/Home entries need to load the appropriate Home feed
if self.list_name == self.username + '/Home':
statuses = self.api.GetHomeTimeline(count=self.num_entries)
# For @username, check if it is one of our usernames, or
# just needs to be searched on
elif self.list_name == '@' + self.username:
statuses = self.api.GetMentions(count=self.num_entries)
elif re.match('@', self.list_name):
statuses = results_to_statuses(self.api.Search(self.list_name, rpp=self.num_entries))
# For @username, check if it is one of our usernames, or
# just needs to be searched on
elif self.list_name == '@' + self.username:
statuses = self.api.GetMentions(count=self.num_entries)
elif re.match('@', self.list_name):
statuses = results_to_statuses(self.api.Search(self.list_name, rpp=self.num_entries))
# Direct Messages should match like /Home, above
elif self.list_name == self.username + '/Direct Messages':
statuses = dms_to_statuses(self.api.GetDirectMessages())
# Direct Messages should match like /Home, above
elif self.list_name == self.username + '/Direct Messages':
statuses = dms_to_statuses(self.api.GetDirectMessages())
# User lookups go straight to the user
elif re.match(r'user: ', self.list_name):
statuses = self.api.GetUserTimeline(re.sub(r'^user: ', r'', self.list_name), count=self.num_entries)
# User lookups go straight to the user
elif re.match(r'user: ', self.list_name):
statuses = self.api.GetUserTimeline(re.sub(r'^user: ', r'', self.list_name), count=self.num_entries)
# Lists load the appropriate list from the appropriate account
elif re.match(r'list: ', self.list_name):
real_list = re.sub(r'list: .*/(.*)', r'\1', self.list_name)
statuses = self.api.GetListStatuses(real_list, per_page=self.num_entries)
# Lists load the appropriate list from the appropriate account
elif re.match(r'list: ', self.list_name):
real_list = re.sub(r'list: .*/(.*)', r'\1', self.list_name)
statuses = self.api.GetListStatuses(real_list, per_page=self.num_entries)
# Everything else is a straight search
else:
statuses = results_to_statuses(self.api.Search(self.list_name, rpp=self.num_entries))
# Everything else is a straight search
else:
statuses = results_to_statuses(self.api.Search(self.list_name, rpp=self.num_entries))
except HTTPError,URLError:
statuses = None
gtk.gdk.threads_enter()
try:
@ -79,7 +83,10 @@ class GetSingleTweet(Thread):
def run(self):
statuses = []
with self.api.lock:
statuses.append(self.api.GetStatus(self.single_tweet))
try:
statuses.append(self.api.GetStatus(self.single_tweet))
except HTTPError,URLError:
statuses = None
gtk.gdk.threads_enter()
try:
@ -107,7 +114,7 @@ class GetFollowing(Thread):
with self.api.lock:
relationship = self.api.ShowFriendships(target_screen_name=screen_name)
following = relationship.source.following
except HTTPError:
except HTTPError,URLError:
following = false
self.pane.set_following(following)
@ -131,7 +138,7 @@ class GetVerified(Thread):
with self.api.lock:
user = self.api.GetUser(screen_name)
verified = user.verified
except HTTPError:
except HTTPError,URLError:
verified = false
self.pane.set_verified(verified)