Implement OAuth for Twitter, and share common infrastructure for all
[hermes] / package / src / org / maemo / hermes / engine / facebook / service.py
index 6fa6c68..91dfe33 100644 (file)
@@ -15,11 +15,9 @@ class Service(org.maemo.hermes.engine.service.Service):
 
     # -----------------------------------------------------------------------
     def __init__(self, service_id, facebook, create_birthday_only = False):
-        """Initialise the Facebook service, finding Facebook API keys in gconf and
-           having a gui_callback available."""
-        
         self.fb = facebook
         self._service_id = service_id
+        self._create_birthday_only = create_birthday_only
         
         self._friends_by_name = {}
         self._friends_by_url = {}
@@ -30,41 +28,60 @@ class Service(org.maemo.hermes.engine.service.Service):
 
 
     # -----------------------------------------------------------------------
-    def get_name(self):
-        return "Facebook"
+    def get_friends_to_create_contacts_for(self):
+        friends = []
+        if self._create_birthday_only:
+            for friend in self._friends_without_contact:
+                if friend.has_birthday_date():
+                    friends.append(friend)
+                    
+        return friends
+    
     
-
     # -----------------------------------------------------------------------
     def get_friends(self):
-        return self._contacts_by_friend.keys()
+        """Returns all friends on Facebook"""
+        
+        return self._friends_by_url.values()
     
     
+    # -----------------------------------------------------------------------
     def get_contacts_with_match(self):
+        """Returns a dict, where each key value pair is a contact (key) that 
+           matched a friend (value)"""
+
         return self._friends_by_contact
     
+    
+    # -----------------------------------------------------------------------
     def get_unmatched_friends(self):
-        return self._friends_by_name.values()
+        """Returns a list of all friends that didn't match a contact."""
+         
+        return self._friends_without_contact
 
 
     # -----------------------------------------------------------------------
     def pre_process_contact(self, contact):
-        """Registers URLs of all previous mappings, and makes sure that any Friends with those
-           URLs don't get match by name."""
+        """Registers URLs of all previous mappings, and makes sure that any 
+           friend with such a URL don't get match by name."""
+           
         for url in contact.get_urls():
             self._known_urls.add(url)
     
     
     # -----------------------------------------------------------------------
     def process_friends(self):
+        """Retreives data from Facebook and parse that into Friend 
+           objects."""
         
         def if_defined(data, key, callback):
             if key in data and data[key]:
+                print key, data[key]
                 callback(data[key])
         
         friends_data = self._get_friends_data()
         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'])
@@ -75,17 +92,16 @@ class Service(org.maemo.hermes.engine.service.Service):
 
             if_defined(data, 'pic_big', 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
+            friend.add_url(url)
+            self._register_friend(friend)
 
 
     # -----------------------------------------------------------------------
     def process_contact(self, contact):
+        """If the contact is matched with a friend, that friend is returned,
+           otherwise None."""
+           
         matched_friend = None
         if self._friends_by_contact.has_key(contact):
             matched_friend = self._friends_by_contact[contact]
@@ -110,6 +126,34 @@ 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)
@@ -119,6 +163,7 @@ class Service(org.maemo.hermes.engine.service.Service):
 
     # -----------------------------------------------------------------------
     def _get_friends_data(self):
-        """Returns a list of dicts, where each dict represents a friend/contact"""
+        """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)