1 from org.maemo.hermes.engine.names import canonical
2 import org.maemo.hermes.engine.service
4 class Service(org.maemo.hermes.engine.service.Service):
5 """Twitter backend for Hermes.
7 Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
8 Copyright (c) Fredrik Wendt <fredrik@wendt.se> 2010.
9 Released under the Artistic Licence."""
12 # -----------------------------------------------------------------------
13 def __init__(self, twitterApi):
14 self._twitter = twitterApi
16 self._friends_by_name = {}
17 self._friends_by_url = {}
18 self._friends_by_contact = {}
20 self._known_urls = set()
23 # -----------------------------------------------------------------------
28 # -----------------------------------------------------------------------
29 def pre_process_contact(self, contact):
30 """Registers URLs of all previous mappings, and makes sure that any Friends with those
31 URLs don't get match by name."""
32 for url in contact.get_urls():
33 self._known_urls.add(url)
36 # -----------------------------------------------------------------------
37 def process_friends(self):
38 for tweeter in self._get_tweeters():
39 key = canonical(tweeter.name)
40 url = 'http://twitter.com/%s' % (tweeter.screen_name)
41 friend = self._create_friend(tweeter.name)
42 friend.set_nickname(tweeter.screen_name)
44 friend.add_url(tweeter.url)
45 if '/default_profile' not in tweeter.profile_image_url:
46 friend.set_photo_url(tweeter.profile_image_url)
48 self._friends.append(friend)
49 self._friends_by_url[url] = friend
50 if url not in self._known_urls:
51 self._friends_by_name[key] = friend
54 # -----------------------------------------------------------------------
55 def process_contact(self, contact):
56 if self._friends_by_contact.has_key(contact):
59 if self._match_contact_to_friend_by_urls(contact):
62 self._match_contact_to_friend_by_identifiers(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):
82 return self._twitter.GetFriends()
85 def _match_contact_to_friend_by_urls(self, contact):
86 for url in contact.get_urls():
87 if url in self._friends_by_url:
88 matched_friend = self._friends_by_url[url]
89 self._register_match(contact, matched_friend)
90 print contact.get_name(), " -> url -> ", matched_friend
96 def _match_contact_to_friend_by_identifiers(self, contact):
97 for id in contact.get_identifiers():
98 if id in self._friends_by_name:
99 matched_friend = self._friends_by_name[id]
100 print contact.get_name(), " -> name -> ", matched_friend
101 self._register_match(contact, matched_friend)
107 def _register_match(self, contact, friend):
108 self._friends_by_contact[contact] = friend
109 self._friends.remove(friend)