1 import org.maemo.hermes.engine.service
4 from org.maemo.hermes.engine.names import canonical
6 class Service(org.maemo.hermes.engine.service.Service):
7 """Twitter backend for Hermes.
9 Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
10 Released under the Artistic Licence."""
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."""
18 self._gui = gui_callback
19 self._username = username
20 self._password = password
23 self._friends_by_url = {}
26 # -----------------------------------------------------------------------
27 def get_friends(self):
28 """Return a list of friends from this service, or 'None' if manual mapping
32 return self._friends.values()
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:
46 self._friends[key] = friend
47 self._friends_by_url[url] = friend
49 return self._friends.values()
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."""
65 for url in contact.get_urls():
66 if url in self._friends_by_url:
67 matched_friend = self._friends_by_url[url]
70 if not matched_friend:
71 for id in contact.get_identifiers():
72 if id in self._friends:
73 matched_friend = self._friends[id]
77 print contact.get_name(), " -> ", matched_friend['profile_url']
79 print contact.get_name(), " no match to Twitter with ", contact.get_identifiers()
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