f20384f73faac0a3f437b4f9b3c5bc9badd9a627
[hermes] / package / src / org / maemo / hermes / engine / twitter / service.py
1 import org.maemo.hermes.engine.service
2
3 import twitter
4 from org.maemo.hermes.engine.names import canonical
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        Released under the Artistic Licence."""
11        
12        
13     # -----------------------------------------------------------------------
14     def __init__(self, username, password, gui_callback = None):
15         """Initialise the Twitter service, using the given credentials and
16            having a gui_callback available."""
17         
18         self._gui = gui_callback
19         self._username = username
20         self._password = password
21         
22         self._friends = None
23         self._friends_by_url = {}
24
25    
26     # -----------------------------------------------------------------------
27     def get_friends(self):
28         """Return a list of friends from this service, or 'None' if manual mapping
29            is not supported."""
30            
31         if self._friends:
32             return self._friends.values()
33         
34         self._friends = {}
35         
36         api = twitter.Api(username=self._username, password=self._password)
37         for tweeter in api.GetFriends():
38             key    = canonical(tweeter.name)
39             url    = 'http://twitter.com/%s' % (tweeter.screen_name)
40             friend = {'name':          tweeter.name, 'pic': tweeter.profile_image_url,
41                       'birthday_date': None,         'profile_url': url,
42                       'homepage':      tweeter.url,  'account': 'twitter'}
43             if friend['pic'].find('/default_profile') > -1:
44                 friend['pic'] = None
45           
46             self._friends[key] = friend
47             self._friends_by_url[url] = friend
48                 
49         return self._friends.values()
50
51     
52     # -----------------------------------------------------------------------
53     def process_contact(self, contact, overwrite = False, friend = None):
54         """Called for each contact in the address book. Any friends linked to
55            from the contact should have their matching updated. The backend should 
56            enrich the contact with any meta-data it can; and return 'True' if any
57            information was updated. If 'friend' is provided, the information should
58            be retrieved from friend. This will be one of the entries from 
59            'get_friends' when manual mapping is being done."""
60            
61         if not self._friends:
62             self.get_friends()
63             
64         matched_friend = None
65         for url in contact.get_urls():
66             if url in self._friends_by_url:
67                 matched_friend = self._friends_by_url[url]
68                 break
69
70         if not matched_friend:
71             for id in contact.get_identifiers():
72                 if id in self._friends:
73                     matched_friend = self._friends[id]
74                     break
75                 
76         if matched_friend:
77             print contact.get_name(), " -> ", matched_friend['profile_url']
78         else:
79             print contact.get_name(), " no match to Twitter with ", contact.get_identifiers()
80             
81     
82     
83     # -----------------------------------------------------------------------
84     def finalise(self, updated, overwrite = False):
85         """Once all contacts have been processed, allows for any tidy-up/additional
86            enrichment. If any contacts are updated at this stage, 'updated' should
87            be added to."""
88            
89         pass