Improve message about credential expiry
[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 import urllib2
5
6 class Service(org.maemo.hermes.engine.service.Service):
7     """Twitter backend for Hermes.
8        
9        Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
10        Copyright (c) Fredrik Wendt <fredrik@wendt.se> 2010.
11        Released under the Artistic Licence."""
12        
13        
14     # -----------------------------------------------------------------------
15     def __init__(self, service_id, twitterApi):
16         org.maemo.hermes.engine.service.Service.__init__(self, service_id)
17
18         self._twitter = twitterApi
19         
20         self._friends_by_name = {}
21         self._friends_by_url = {}
22         self._friends_by_contact = {}
23         self._friends = []
24         self._known_urls = set()
25     
26     
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)
33
34
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)
44             friend.add_url(url)
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)
48           
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
53
54     
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)
61         
62         return None
63     
64     
65     # -----------------------------------------------------------------------
66     def finalise(self, updated, overwrite=False):
67         pass
68
69
70     # -----------------------------------------------------------------------
71     def get_contacts_with_match(self):
72         return self._friends_by_contact
73     
74
75     # -----------------------------------------------------------------------
76     def get_unmatched_friends(self):
77         return self._friends
78     
79
80     # -----------------------------------------------------------------------
81     def _get_tweeters(self):
82         try:
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
87                 return []
88             else:
89                 raise e
90         except Exception, e:
91             if e.message == 'This method requires authentication.':
92                 raise org.maemo.hermes.engine.service.CredentialsExpiredException('Twitter')
93             else:
94                 raise e
95         
96     
97     
98     # -----------------------------------------------------------------------
99     def _get_friend_by_contact(self, contact):
100         return self._friends_by_contact[contact]
101
102
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)
108                 return True
109             
110         return False
111
112
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)
118                 return True
119             
120         return False
121
122
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)
128