58d76a0c9ab15238ec087af01051507c36e5623e
[hermes] / package / src / org / maemo / hermes / engine / twitter / service.py
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
4
5 class Service(org.maemo.hermes.engine.service.Service):
6     """Twitter backend for Hermes.
7        
8        Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
9        Copyright (c) Fredrik Wendt <fredrik@wendt.se> 2010.
10        Released under the Artistic Licence."""
11        
12        
13     # -----------------------------------------------------------------------
14     def __init__(self, service_id, twitterApi):
15         org.maemo.hermes.engine.service.Service.__init__(self, service_id)
16
17         self._twitter = twitterApi
18         
19         self._friends_by_name = {}
20         self._friends_by_url = {}
21         self._friends_by_contact = {}
22         self._friends = []
23         self._known_urls = set()
24     
25     
26     # -----------------------------------------------------------------------
27     def pre_process_contact(self, contact):
28         """Registers URLs of all previous mappings, and makes sure that any Friends with those
29            URLs don't get match by name."""
30         for url in contact.get_urls():
31             self._known_urls.add(url)
32
33
34     # -----------------------------------------------------------------------
35     def process_friends(self):
36         tweeters = self._get_tweeters()
37         #print "got ", len(tweeters), " tweeters from twitter"
38         for tweeter in tweeters:
39             key = canonical(tweeter.name)
40             url = 'http://twitter.com/%s' % (tweeter.screen_name)
41             friend = Friend(tweeter.name)
42             friend.set_nickname(tweeter.screen_name)
43             friend.add_url(url)
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)
47           
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
52
53     
54     # -----------------------------------------------------------------------
55     def process_contact(self, contact):
56         if self._friends_by_contact.has_key(contact) or \
57             self._match_contact_to_friend_by_urls(contact) or \
58             self._match_contact_to_friend_by_identifiers(contact):
59             return self._get_friend_by_contact(contact)
60         
61         return None
62     
63     
64     # -----------------------------------------------------------------------
65     def finalise(self, updated, overwrite=False):
66         pass
67
68
69     # -----------------------------------------------------------------------
70     def get_contacts_with_match(self):
71         return self._friends_by_contact
72     
73
74     # -----------------------------------------------------------------------
75     def get_unmatched_friends(self):
76         return self._friends
77     
78
79     # -----------------------------------------------------------------------
80     def _get_tweeters(self):
81         try:
82             return self._twitter.GetFriends()
83         except urllib2.HTTPError, e:
84             if e.code >= 500 and e.code <= 599:
85                 print "Twitter down (fail whale): " + e.message
86                 return []
87             else:
88                 raise e
89         
90     
91     
92     # -----------------------------------------------------------------------
93     def _get_friend_by_contact(self, contact):
94         return self._friends_by_contact[contact]
95
96
97     def _match_contact_to_friend_by_urls(self, contact):
98         for url in contact.get_urls():
99             if url in self._friends_by_url:
100                 matched_friend = self._friends_by_url[url]
101                 self._register_match(contact, matched_friend)
102                 return True
103             
104         return False
105
106
107     def _match_contact_to_friend_by_identifiers(self, contact):
108         for id in contact.get_identifiers():
109             if id in self._friends_by_name:
110                 matched_friend = self._friends_by_name[id]
111                 self._register_match(contact, matched_friend)
112                 return True
113             
114         return False
115
116
117     def _register_match(self, contact, friend):
118         friend.set_contact(contact)
119         self._friends_by_contact[contact] = friend
120         if friend in self._friends:
121             self._friends.remove(friend)
122