1 from org.maemo.hermes.engine.names import canonical
2 from org.maemo.hermes.engine.friend import Friend
3 import org.maemo.hermes.engine.service
6 class Service(org.maemo.hermes.engine.service.Service):
7 """Twitter backend for Hermes.
9 Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
10 Copyright (c) Fredrik Wendt <fredrik@wendt.se> 2010.
11 Released under the Artistic Licence."""
14 # -----------------------------------------------------------------------
15 def __init__(self, service_id, twitterApi):
16 org.maemo.hermes.engine.service.Service.__init__(self, service_id)
18 self._twitter = twitterApi
20 self._friends_by_name = {}
21 self._friends_by_url = {}
22 self._friends_by_contact = {}
24 self._known_urls = set()
27 # -----------------------------------------------------------------------
28 def pre_process_contact(self, contact):
29 """Registers URLs of all previous mappings, and makes sure that any Friends with those
30 URLs don't get match by name."""
31 for url in contact.get_urls():
32 self._known_urls.add(url)
35 # -----------------------------------------------------------------------
36 def process_friends(self):
37 tweeters = self._get_tweeters()
38 #print "got ", len(tweeters), " tweeters from twitter"
39 for tweeter in tweeters:
40 key = canonical(tweeter.name)
41 url = 'http://twitter.com/%s' % (tweeter.screen_name)
42 friend = Friend(tweeter.name)
43 friend.set_nickname(tweeter.screen_name)
45 friend.add_url(tweeter.url)
46 if '/default_profile' not in tweeter.profile_image_url:
47 friend.set_photo_url(tweeter.profile_image_url)
49 self._friends.append(friend)
50 self._friends_by_url[url] = friend
51 if url not in self._known_urls:
52 self._friends_by_name[key] = friend
55 # -----------------------------------------------------------------------
56 def process_contact(self, contact):
57 if self._friends_by_contact.has_key(contact) or \
58 self._match_contact_to_friend_by_urls(contact) or \
59 self._match_contact_to_friend_by_identifiers(contact):
60 return self._get_friend_by_contact(contact)
65 # -----------------------------------------------------------------------
66 def finalise(self, updated, overwrite=False):
70 # -----------------------------------------------------------------------
71 def get_contacts_with_match(self):
72 return self._friends_by_contact
75 # -----------------------------------------------------------------------
76 def get_unmatched_friends(self):
80 # -----------------------------------------------------------------------
81 def _get_tweeters(self):
83 return self._twitter.get_friends()
84 except urllib2.HTTPError, e:
85 if e.code >= 500 and e.code <= 599:
86 print "Twitter down (fail whale): " + e.message
91 if e.message == 'This method requires authentication.':
92 raise org.maemo.hermes.engine.service.CredentialsExpiredException('Twitter')
98 # -----------------------------------------------------------------------
99 def _get_friend_by_contact(self, contact):
100 return self._friends_by_contact[contact]
103 def _match_contact_to_friend_by_urls(self, contact):
104 for url in contact.get_urls():
105 if url in self._friends_by_url:
106 matched_friend = self._friends_by_url[url]
107 self._register_match(contact, matched_friend)
113 def _match_contact_to_friend_by_identifiers(self, contact):
114 for id in contact.get_identifiers():
115 if id in self._friends_by_name:
116 matched_friend = self._friends_by_name[id]
117 self._register_match(contact, matched_friend)
123 def _register_match(self, contact, friend):
124 friend.set_contact(contact)
125 self._friends_by_contact[contact] = friend
126 if friend in self._friends:
127 self._friends.remove(friend)