import twitter
from org.maemo.hermes.engine.names import canonical
+from org.maemo.hermes.engine.friend import Friend
class Service(org.maemo.hermes.engine.service.Service):
"""Twitter backend for Hermes.
self._friends = None
self._friends_by_url = {}
+ self._friends_by_contact = {}
# -----------------------------------------------------------------------
- def get_friends(self):
+ def get_name(self):
+ return "Twitter"
+
+
+ # -----------------------------------------------------------------------
+ def process_friends(self):
"""Return a list of friends from this service, or 'None' if manual mapping
is not supported."""
for tweeter in api.GetFriends():
key = canonical(tweeter.name)
url = 'http://twitter.com/%s' % (tweeter.screen_name)
- friend = {'name': tweeter.name, 'pic': tweeter.profile_image_url,
- 'birthday_date': None, 'profile_url': url,
- 'homepage': tweeter.url, 'account': 'twitter'}
- if friend['pic'].find('/default_profile') > -1:
- friend['pic'] = None
+ friend = Friend(tweeter.name)
+ friend.set_nickname(tweeter.screen_name)
+ friend.add_url(url)
+ friend.add_url(tweeter.url)
+ if '/default_profile' not in tweeter.profile_image_url:
+ friend.set_photo_url(tweeter.profile_image_url)
self._friends[key] = friend
self._friends_by_url[url] = friend
# -----------------------------------------------------------------------
- def process_contact(self, contact, overwrite = False, friend = None):
+ def pre_process_contact(self, contact):
+ if not self._friends:
+ self.get_friends()
+
+ for url in contact.get_urls():
+ if url in self._friends_by_url:
+ matched_friend = self._friends_by_url[url]
+ self._friends_by_contact[contact] = matched_friend
+
+
+ # -----------------------------------------------------------------------
+ def process_contact(self, contact, friend):
"""Called for each contact in the address book. Any friends linked to
from the contact should have their matching updated. The backend should
enrich the contact with any meta-data it can; and return 'True' if any
if not self._friends:
self.get_friends()
-
- matched_friend = None
- for url in contact.get_urls():
- if url in self._friends_by_url:
- matched_friend = self._friends_by_url[url]
- break
-
- if not matched_friend:
- for id in contact.get_identifiers():
- if id in self._friends:
- matched_friend = self._friends[id]
- break
+
+ if self._friends_by_contact.has_key(contact):
+ friend.update(self._friends_by_contact[contact])
+ return
+
+ for id in contact.get_identifiers():
+ if id in self._friends:
+ matched_friend = self._friends[id]
+ print contact.get_name(), " -> ", matched_friend
+ if matched_friend in self._friends_by_contact.values():
+ print "COLLISSION avoided for %s" % friend
+ else:
+ print "Match found %s" % friend
+ self._friends_by_contact[contact] = matched_friend
+ friend.update(matched_friend)
- if matched_friend:
- print contact.get_name(), " -> ", matched_friend['profile_url']
- else:
- print contact.get_name(), " no match to Twitter with ", contact.get_identifiers()
-
# -----------------------------------------------------------------------