Dependency inject service ID, so that it can be stamped on friends and
[hermes] / package / src / org / maemo / hermes / engine / twitter / service.py
1 from org.maemo.hermes.engine.names import canonical
2 import org.maemo.hermes.engine.service
3
4 class Service(org.maemo.hermes.engine.service.Service):
5     """Twitter backend for Hermes.
6        
7        Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
8        Copyright (c) Fredrik Wendt <fredrik@wendt.se> 2010.
9        Released under the Artistic Licence."""
10        
11        
12     # -----------------------------------------------------------------------
13     def __init__(self, service_id, twitterApi):
14         org.maemo.hermes.engine.service.Service.__init__(self, service_id)
15
16         self._twitter = twitterApi
17         
18         self._friends_by_name = {}
19         self._friends_by_url = {}
20         self._friends_by_contact = {}
21         self._friends = []
22         self._known_urls = set()
23     
24     
25     # -----------------------------------------------------------------------
26     def pre_process_contact(self, contact):
27         """Registers URLs of all previous mappings, and makes sure that any Friends with those
28            URLs don't get match by name."""
29         for url in contact.get_urls():
30             self._known_urls.add(url)
31
32
33     # -----------------------------------------------------------------------
34     def process_friends(self):
35         for tweeter in self._get_tweeters():
36             key = canonical(tweeter.name)
37             url = 'http://twitter.com/%s' % (tweeter.screen_name)
38             friend = self._create_friend(tweeter.name)
39             friend.set_nickname(tweeter.screen_name)
40             friend.add_url(url)
41             friend.add_url(tweeter.url)
42             if '/default_profile' not in tweeter.profile_image_url:
43                 friend.set_photo_url(tweeter.profile_image_url)
44           
45             self._friends.append(friend)
46             self._friends_by_url[url] = friend
47             if url not in self._known_urls:
48                 self._friends_by_name[key] = friend
49
50     
51     # -----------------------------------------------------------------------
52     def process_contact(self, contact):
53         if self._friends_by_contact.has_key(contact) or \
54             self._match_contact_to_friend_by_urls(contact) or \
55             self._match_contact_to_friend_by_identifiers(contact):
56             return self._get_friend_by_contact(contact)
57         
58         return None
59     
60     
61     # -----------------------------------------------------------------------
62     def finalise(self, updated, overwrite=False):
63         pass
64
65
66     # -----------------------------------------------------------------------
67     def get_contacts_with_match(self):
68         return self._friends_by_contact
69     
70
71     # -----------------------------------------------------------------------
72     def get_unmatched_friends(self):
73         return self._friends
74     
75
76     # -----------------------------------------------------------------------
77     def _get_tweeters(self):
78         return self._twitter.GetFriends()
79     
80     
81     # -----------------------------------------------------------------------
82     def _get_friend_by_contact(self, contact):
83         return self._friends_by_contact[contact]
84
85
86     def _match_contact_to_friend_by_urls(self, contact):
87         for url in contact.get_urls():
88             if url in self._friends_by_url:
89                 matched_friend = self._friends_by_url[url]
90                 self._register_match(contact, matched_friend)
91                 return True
92             
93         return False
94
95
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                 self._register_match(contact, matched_friend)
101                 return True
102             
103         return False
104
105
106     def _register_match(self, contact, friend):
107         friend.set_contact(contact)
108         self._friends_by_contact[contact] = friend
109         self._friends.remove(friend)
110