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
20 self._create_birthday_only = create_birthday_only
22 self._friends_by_name = {}
23 self._friends_by_url = {}
24 self._friends_by_contact = {}
25 self._contacts_by_friend = {}
26 self._friends_without_contact = set()
27 self._known_urls = set()
30 # -----------------------------------------------------------------------
31 def get_friends_to_create_contacts_for(self):
33 if self._create_birthday_only:
34 for friend in self._friends_without_contact:
35 if friend.has_birthday_date():
36 friends.append(friend)
41 # -----------------------------------------------------------------------
42 def get_friends(self):
43 """Returns all friends on Facebook"""
45 return self._friends_by_url.values()
48 # -----------------------------------------------------------------------
49 def get_contacts_with_match(self):
50 """Returns a dict, where each key value pair is a contact (key) that
51 matched a friend (value)"""
53 return self._friends_by_contact
56 # -----------------------------------------------------------------------
57 def get_unmatched_friends(self):
58 """Returns a list of all friends that didn't match a contact."""
60 return self._friends_by_name.values()
63 # -----------------------------------------------------------------------
64 def pre_process_contact(self, contact):
65 """Registers URLs of all previous mappings, and makes sure that any
66 friend with such a URL don't get match by name."""
68 for url in contact.get_urls():
69 self._known_urls.add(url)
72 # -----------------------------------------------------------------------
73 def process_friends(self):
74 """Retreives data from Facebook and parse that into Friend
77 def if_defined(data, key, callback):
78 if key in data and data[key]:
81 friends_data = self._get_friends_data()
82 for data in friends_data:
83 key = canonical(data['name']) # FIXME: deal with name collision
84 friend = Friend(data['name'])
86 if 'profile_url' not in data:
87 data['profile_url'] = "http://www.facebook.com/profile.php?id=" + str(data['uid'])
89 if_defined(data, 'website', friend.add_url)
90 if_defined(data, 'profile_url', friend.add_url)
91 if_defined(data, 'birthday_date', friend.set_birthday_date)
93 if_defined(data, 'pic_big', friend.set_photo_url)
95 url = data['profile_url']
96 self._friends_without_contact.add(friend)
97 self._friends_by_url[url] = friend
99 if url not in self._known_urls:
100 self._friends_by_name[key] = friend
103 # -----------------------------------------------------------------------
104 def process_contact(self, contact):
105 """If the contact is matched with a friend, that friend is returned,
108 matched_friend = None
109 if self._friends_by_contact.has_key(contact):
110 matched_friend = self._friends_by_contact[contact]
112 # we might get a hit if the friend has setup a URL with another service,
113 # such as putting the id link to Facebook on the Twitter account's profile
114 if not matched_friend:
115 for url in contact.get_urls():
116 if url in self._friends_by_url:
117 matched_friend = self._friends_by_url[url]
118 self._register_match(contact, matched_friend)
121 if not matched_friend:
122 for id in contact.get_identifiers():
123 if id in self._friends_by_name:
124 matched_friend = self._friends_by_name.pop(id)
125 self._register_match(contact, matched_friend)
128 return matched_friend
131 # -----------------------------------------------------------------------
132 def _register_match(self, contact, friend):
133 friend.set_contact(contact)
134 self._friends_without_contact.discard(friend)
135 self._friends_by_contact[contact] = friend
136 self._contacts_by_friend[friend] = contact
139 # -----------------------------------------------------------------------
140 def _get_friends_data(self):
141 """Returns a list of dicts, where each dict represents the raw data
144 return self.fb.users.getInfo(self.fb.friends.get(), Service.attrs)