from org.maemo.hermes.engine.names import canonical
+from org.maemo.hermes.engine.friend import Friend
import org.maemo.hermes.engine.service
+import urllib2
class Service(org.maemo.hermes.engine.service.Service):
"""Twitter backend for Hermes.
# -----------------------------------------------------------------------
- def __init__(self, twitterApi):
+ def __init__(self, service_id, twitterApi):
+ org.maemo.hermes.engine.service.Service.__init__(self, service_id)
+
self._twitter = twitterApi
self._friends_by_name = {}
self._friends_by_contact = {}
self._friends = []
self._known_urls = set()
-
-
- # -----------------------------------------------------------------------
- def get_name(self):
- return "Twitter"
# -----------------------------------------------------------------------
# -----------------------------------------------------------------------
def process_friends(self):
- for tweeter in self._get_tweeters():
+ tweeters = self._get_tweeters()
+ #print "got ", len(tweeters), " tweeters from twitter"
+ for tweeter in tweeters:
key = canonical(tweeter.name)
url = 'http://twitter.com/%s' % (tweeter.screen_name)
- friend = self._create_friend(tweeter.name)
+ friend = Friend(tweeter.name)
friend.set_nickname(tweeter.screen_name)
friend.add_url(url)
friend.add_url(tweeter.url)
# -----------------------------------------------------------------------
def process_contact(self, contact):
- if self._friends_by_contact.has_key(contact):
- return
-
- if self._match_contact_to_friend_by_urls(contact):
- return
+ if self._friends_by_contact.has_key(contact) or \
+ self._match_contact_to_friend_by_urls(contact) or \
+ self._match_contact_to_friend_by_identifiers(contact):
+ return self._get_friend_by_contact(contact)
- self._match_contact_to_friend_by_identifiers(contact)
+ return None
# -----------------------------------------------------------------------
# -----------------------------------------------------------------------
def _get_tweeters(self):
- return self._twitter.GetFriends()
+ try:
+ return self._twitter.GetFriends()
+ except urllib2.HTTPError, e:
+ if e.code >= 500 and e.code <= 599:
+ print "Twitter down (fail whale): " + e.message
+ return []
+ else:
+ raise e
+
+
+
+ # -----------------------------------------------------------------------
+ def _get_friend_by_contact(self, contact):
+ return self._friends_by_contact[contact]
def _match_contact_to_friend_by_urls(self, contact):
if url in self._friends_by_url:
matched_friend = self._friends_by_url[url]
self._register_match(contact, matched_friend)
- print contact.get_name(), " -> url -> ", matched_friend
return True
return False
for id in contact.get_identifiers():
if id in self._friends_by_name:
matched_friend = self._friends_by_name[id]
- print contact.get_name(), " -> name -> ", matched_friend
self._register_match(contact, matched_friend)
return True
def _register_match(self, contact, friend):
+ friend.set_contact(contact)
self._friends_by_contact[contact] = friend
self._friends.remove(friend)