1 import org.maemo.hermes.engine.service
3 from org.maemo.hermes.engine.names import canonical
4 from org.maemo.hermes.engine.friend import Friend
6 class Service(org.maemo.hermes.engine.service.Service):
7 """Facebook backend for Hermes.
9 Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
10 Copyright (c) Fredrik Wendt <fredrik@wendt.se> 2010.
11 Released under the Artistic Licence."""
13 attrs = ['uid', 'name', 'pic_big', 'birthday_date', 'profile_url', 'first_name', 'last_name', 'website']
16 # -----------------------------------------------------------------------
17 def __init__(self, service_id, facebook, create_birthday_only = False):
19 self._service_id = service_id
21 self._friends_by_name = {}
22 self._friends_by_url = {}
23 self._friends_by_contact = {}
24 self._contacts_by_friend = {}
25 self._friends_without_contact = set()
26 self._known_urls = set()
29 # -----------------------------------------------------------------------
30 def get_friends(self):
31 """Returns all friends on Facebook"""
33 return self._contacts_by_friend.keys()
36 # -----------------------------------------------------------------------
37 def get_contacts_with_match(self):
38 """Returns a dict, where each key value pair is a contact (key) that
39 matched a friend (value)"""
41 return self._friends_by_contact
44 # -----------------------------------------------------------------------
45 def get_unmatched_friends(self):
46 """Returns a list of all friends that didn't match a contact."""
48 return self._friends_by_name.values()
51 # -----------------------------------------------------------------------
52 def pre_process_contact(self, contact):
53 """Registers URLs of all previous mappings, and makes sure that any
54 friend with such a URL don't get match by name."""
56 for url in contact.get_urls():
57 self._known_urls.add(url)
60 # -----------------------------------------------------------------------
61 def process_friends(self):
62 """Retreives data from Facebook and parse that into Friend
65 def if_defined(data, key, callback):
66 if key in data and data[key]:
69 friends_data = self._get_friends_data()
70 for data in friends_data:
71 key = canonical(data['name']) # FIXME: deal with name collision
72 friend = Friend(data['name'])
74 if 'profile_url' not in data:
75 data['profile_url'] = "http://www.facebook.com/profile.php?id=" + str(data['uid'])
77 if_defined(data, 'website', friend.add_url)
78 if_defined(data, 'profile_url', friend.add_url)
79 if_defined(data, 'birthday_date', friend.set_birthday_date)
81 if_defined(data, 'pic_big', friend.set_photo_url)
83 if friend.has_birthday_date(): # FIXME: remove this, either you want to add your contacts or not?
84 self._friends_without_contact.add(friend)
85 url = data['profile_url']
86 self._friends_by_url[url] = friend
88 if url not in self._known_urls:
89 self._friends_by_name[key] = friend
92 # -----------------------------------------------------------------------
93 def process_contact(self, contact):
94 """If the contact is matched with a friend, that friend is returned,
98 if self._friends_by_contact.has_key(contact):
99 matched_friend = self._friends_by_contact[contact]
101 # we might get a hit if the friend has setup a URL with another service,
102 # such as putting the id link to Facebook on the Twitter account's profile
103 if not matched_friend:
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)
110 if not matched_friend:
111 for id in contact.get_identifiers():
112 if id in self._friends_by_name:
113 matched_friend = self._friends_by_name.pop(id)
114 self._register_match(contact, matched_friend)
117 return matched_friend
120 # -----------------------------------------------------------------------
121 def _register_match(self, contact, friend):
122 friend.set_contact(contact)
123 self._friends_without_contact.discard(friend)
124 self._friends_by_contact[contact] = friend
125 self._contacts_by_friend[friend] = contact
128 # -----------------------------------------------------------------------
129 def _get_friends_data(self):
130 """Returns a list of dicts, where each dict represents the raw data
133 return self.fb.users.getInfo(self.fb.friends.get(), Service.attrs)