Dependency inject service ID, so that it can be stamped on friends and
[hermes] / package / src / org / maemo / hermes / engine / facebook / service.py
1 import org.maemo.hermes.engine.service
2
3 from org.maemo.hermes.engine.names import canonical
4 from org.maemo.hermes.engine.friend import Friend
5
6 class Service(org.maemo.hermes.engine.service.Service):
7     """Facebook backend for Hermes.
8                 
9        Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
10        Copyright (c) Fredrik Wendt <fredrik@wendt.se> 2010.
11        Released under the Artistic Licence."""
12        
13     attrs = ['uid', 'name', 'pic_big', 'birthday_date', 'profile_url', 'first_name', 'last_name', 'website']
14
15
16     # -----------------------------------------------------------------------
17     def __init__(self, service_id, facebook, create_birthday_only = False):
18         """Initialise the Facebook service, finding Facebook API keys in gconf and
19            having a gui_callback available."""
20         
21         self.fb = facebook
22         self._service_id = service_id
23         
24         self._friends_by_name = {}
25         self._friends_by_url = {}
26         self._friends_by_contact = {}
27         self._contacts_by_friend = {}
28         self._friends_without_contact = set()
29         self._known_urls = set()
30
31
32     # -----------------------------------------------------------------------
33     def get_name(self):
34         return "Facebook"
35     
36
37     # -----------------------------------------------------------------------
38     def get_friends(self):
39         return self._contacts_by_friend.keys()
40     
41     
42     def get_contacts_with_match(self):
43         return self._friends_by_contact
44     
45     def get_unmatched_friends(self):
46         return self._friends_by_name.values()
47
48
49     # -----------------------------------------------------------------------
50     def pre_process_contact(self, contact):
51         """Registers URLs of all previous mappings, and makes sure that any Friends with those
52            URLs don't get match by name."""
53         for url in contact.get_urls():
54             self._known_urls.add(url)
55     
56     
57     # -----------------------------------------------------------------------
58     def process_friends(self):
59         
60         def if_defined(data, key, callback):
61             if key in data and data[key]:
62                 callback(data[key])
63         
64         friends_data = self._get_friends_data()
65         for data in friends_data:
66             key = canonical(data['name']) # FIXME: deal with name collision
67             friend = self._create_friend(data['name'])
68         
69             if 'profile_url' not in data:
70                 data['profile_url'] = "http://www.facebook.com/profile.php?id=" + str(data['uid'])
71         
72             if_defined(data, 'website', friend.add_url)
73             if_defined(data, 'profile_url', friend.add_url)
74             if_defined(data, 'birthday_date', friend.set_birthday_date)
75
76             if_defined(data, 'pic_big', friend.set_photo_url)
77             
78             if friend.has_birthday_date(): # FIXME: remove this, either you want to add your contacts or not? 
79                 self._friends_without_contact.add(friend)
80             url = data['profile_url']
81             self._friends_by_url[url] = friend
82             
83             if url not in self._known_urls:
84                 self._friends_by_name[key] = friend
85
86
87     # -----------------------------------------------------------------------
88     def process_contact(self, contact):
89         matched_friend = None
90         if self._friends_by_contact.has_key(contact):
91             matched_friend = self._friends_by_contact[contact]
92         
93         # we might get a hit if the friend has setup a URL with another service,
94         # such as putting the id link to Facebook on the Twitter account's profile
95         if not matched_friend:
96             for url in contact.get_urls():
97                 if url in self._friends_by_url:
98                     matched_friend = self._friends_by_url[url]
99                     self._register_match(contact, matched_friend)
100                     break
101
102         if not matched_friend:
103             for id in contact.get_identifiers():
104                 if id in self._friends_by_name:
105                     matched_friend = self._friends_by_name.pop(id)
106                     self._register_match(contact, matched_friend)
107                     break
108                 
109         return matched_friend
110     
111
112     # -----------------------------------------------------------------------
113     def _register_match(self, contact, friend):
114         friend.set_contact(contact)
115         self._friends_without_contact.discard(friend)
116         self._friends_by_contact[contact] = friend
117         self._contacts_by_friend[friend] = contact
118
119
120     # -----------------------------------------------------------------------
121     def _get_friends_data(self):
122         """Returns a list of dicts, where each dict represents a friend/contact"""
123         
124         return self.fb.users.getInfo(self.fb.friends.get(), Service.attrs)