Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings Hermes
Summary Lists News SCM
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
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)
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)
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
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
70     # -----------------------------------------------------------------------
71     def get_contacts_with_match(self):
72         return self._friends_by_contact
73     
75     # -----------------------------------------------------------------------
76     def get_unmatched_friends(self):
77         return self._friends
78     
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         
91     
92     
93     # -----------------------------------------------------------------------
94     def _get_friend_by_contact(self, contact):
95         return self._friends_by_contact[contact]
98     def _match_contact_to_friend_by_urls(self, contact):
99         for url in contact.get_urls():
100             if url in self._friends_by_url:
101                 matched_friend = self._friends_by_url[url]
102                 self._register_match(contact, matched_friend)
103                 return True
104             
105         return False
108     def _match_contact_to_friend_by_identifiers(self, contact):
109         for id in contact.get_identifiers():
110             if id in self._friends_by_name:
111                 matched_friend = self._friends_by_name[id]
112                 self._register_match(contact, matched_friend)
113                 return True
114             
115         return False
118     def _register_match(self, contact, friend):
119         friend.set_contact(contact)
120         self._friends_by_contact[contact] = friend
121         if friend in self._friends:
122             self._friends.remove(friend)

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment