Re-implement Facebook service to use OAuth2 and Graph API. This allows
[hermes] / package / src / org / maemo / hermes / engine / facebook / service.py
index 4ea03ce..ff46c26 100644 (file)
@@ -14,8 +14,10 @@ class Service(org.maemo.hermes.engine.service.Service):
 
 
     # -----------------------------------------------------------------------
-    def __init__(self, facebook, create_birthday_only = False):
+    def __init__(self, service_id, facebook, create_birthday_only = False):
         self.fb = facebook
+        self._service_id = service_id
+        self._create_birthday_only = create_birthday_only
         
         self._friends_by_name = {}
         self._friends_by_url = {}
@@ -26,10 +28,21 @@ class Service(org.maemo.hermes.engine.service.Service):
 
 
     # -----------------------------------------------------------------------
+    def get_friends_to_create_contacts_for(self):
+        friends = []
+        if self._create_birthday_only:
+            for friend in self._friends_without_contact:
+                if friend.is_interesting():
+                    friends.append(friend)
+                    
+        return friends
+    
+    
+    # -----------------------------------------------------------------------
     def get_friends(self):
         """Returns all friends on Facebook"""
         
-        return self._contacts_by_friend.keys()
+        return self._friends_by_url.values()
     
     
     # -----------------------------------------------------------------------
@@ -44,7 +57,7 @@ class Service(org.maemo.hermes.engine.service.Service):
     def get_unmatched_friends(self):
         """Returns a list of all friends that didn't match a contact."""
          
-        return self._friends_by_name.values()
+        return self._friends_without_contact
 
 
     # -----------------------------------------------------------------------
@@ -65,27 +78,22 @@ class Service(org.maemo.hermes.engine.service.Service):
             if key in data and data[key]:
                 callback(data[key])
         
-        friends_data = self._get_friends_data()
+        friends_data = self.fb.get_friends()
         for data in friends_data:
-            key = canonical(data['name']) # FIXME: deal with name collision
-            friend = self._create_friend(data['name'])
+            friend = Friend(data['name'])
         
-            if 'profile_url' not in data:
-                data['profile_url'] = "http://www.facebook.com/profile.php?id=" + str(data['uid'])
+            if 'link' not in data:
+                data['link'] = "http://www.facebook.com/profile.php?id=" + str(data['id'])
         
             if_defined(data, 'website', friend.add_url)
-            if_defined(data, 'profile_url', friend.add_url)
-            if_defined(data, 'birthday_date', friend.set_birthday_date)
+            if_defined(data, 'link', friend.add_url)
+            if_defined(data, 'birthday', friend.set_birthday_date)
 
-            if_defined(data, 'pic_big', friend.set_photo_url)
+            if_defined(data, 'picture', friend.set_photo_url)
             
-            if friend.has_birthday_date(): # FIXME: remove this, either you want to add your contacts or not? 
-                self._friends_without_contact.add(friend)
-            url = data['profile_url']
-            self._friends_by_url[url] = friend
-            
-            if url not in self._known_urls:
-                self._friends_by_name[key] = friend
+            url = data['link']
+            friend.add_url(url)
+            self._register_friend(friend)
 
 
     # -----------------------------------------------------------------------
@@ -117,16 +125,36 @@ class Service(org.maemo.hermes.engine.service.Service):
     
 
     # -----------------------------------------------------------------------
+    def _register_friend(self, friend):
+        self._friends_without_contact.add(friend)
+        
+        for url in friend.get_urls():
+            if self.is_profile_url(url):
+                self._friends_by_url[url] = friend
+
+        if self._allow_friend_to_match_by_name(friend):
+            key = canonical(friend.get_name())
+            self._friends_by_name[key] = friend
+
+
+    # -----------------------------------------------------------------------
+    def is_profile_url(self, url):
+        """Return True if this is a URL for this service."""
+
+        return url and "facebook.com" in url
+
+
+    # -----------------------------------------------------------------------
+    def _allow_friend_to_match_by_name(self, friend):
+        for url in friend.get_urls():
+            if url in self._known_urls:
+                return False
+        return True
+
+
+    # -----------------------------------------------------------------------
     def _register_match(self, contact, friend):
         friend.set_contact(contact)
         self._friends_without_contact.discard(friend)
         self._friends_by_contact[contact] = friend
         self._contacts_by_friend[friend] = contact
-
-
-    # -----------------------------------------------------------------------
-    def _get_friends_data(self):
-        """Returns a list of dicts, where each dict represents the raw data
-           of a friend"""
-        
-        return self.fb.users.getInfo(self.fb.friends.get(), Service.attrs)