uncomplete adjustments to avoid name collision
authorFredrik Wendt <fredrik@wendt.se>
Sat, 5 Jun 2010 21:32:49 +0000 (23:32 +0200)
committerFredrik Wendt <fredrik@wendt.se>
Sat, 5 Jun 2010 21:32:49 +0000 (23:32 +0200)
Signed-off-by: Fredrik Wendt <fredrik@wendt.se>

package/src/org/maemo/hermes/engine/twitter/service.py

index f20384f..fc30d48 100644 (file)
@@ -2,6 +2,7 @@ import org.maemo.hermes.engine.service
 
 import twitter
 from org.maemo.hermes.engine.names import canonical
+from org.maemo.hermes.engine.friend import Friend
 
 class Service(org.maemo.hermes.engine.service.Service):
     """Twitter backend for Hermes.
@@ -21,10 +22,16 @@ class Service(org.maemo.hermes.engine.service.Service):
         
         self._friends = None
         self._friends_by_url = {}
+        self._friends_by_contact = {}
 
    
     # -----------------------------------------------------------------------
-    def get_friends(self):
+    def get_name(self):
+        return "Twitter"
+    
+    
+    # -----------------------------------------------------------------------
+    def process_friends(self):
         """Return a list of friends from this service, or 'None' if manual mapping
            is not supported."""
            
@@ -37,11 +44,12 @@ class Service(org.maemo.hermes.engine.service.Service):
         for tweeter in api.GetFriends():
             key    = canonical(tweeter.name)
             url    = 'http://twitter.com/%s' % (tweeter.screen_name)
-            friend = {'name':          tweeter.name, 'pic': tweeter.profile_image_url,
-                      'birthday_date': None,         'profile_url': url,
-                      'homepage':      tweeter.url,  'account': 'twitter'}
-            if friend['pic'].find('/default_profile') > -1:
-                friend['pic'] = None
+            friend = Friend(tweeter.name)
+            friend.set_nickname(tweeter.screen_name)
+            friend.add_url(url)
+            friend.add_url(tweeter.url)
+            if '/default_profile' not in tweeter.profile_image_url:
+                friend.set_photo_url(tweeter.profile_image_url)
           
             self._friends[key] = friend
             self._friends_by_url[url] = friend
@@ -50,7 +58,18 @@ class Service(org.maemo.hermes.engine.service.Service):
 
     
     # -----------------------------------------------------------------------
-    def process_contact(self, contact, overwrite = False, friend = None):
+    def pre_process_contact(self, contact):
+        if not self._friends:
+            self.get_friends()
+            
+        for url in contact.get_urls():
+            if url in self._friends_by_url:
+                matched_friend = self._friends_by_url[url]
+                self._friends_by_contact[contact] = matched_friend
+
+
+    # -----------------------------------------------------------------------
+    def process_contact(self, contact, friend):
         """Called for each contact in the address book. Any friends linked to
            from the contact should have their matching updated. The backend should 
            enrich the contact with any meta-data it can; and return 'True' if any
@@ -60,24 +79,22 @@ class Service(org.maemo.hermes.engine.service.Service):
            
         if not self._friends:
             self.get_friends()
-            
-        matched_friend = None
-        for url in contact.get_urls():
-            if url in self._friends_by_url:
-                matched_friend = self._friends_by_url[url]
-                break
-
-        if not matched_friend:
-            for id in contact.get_identifiers():
-                if id in self._friends:
-                    matched_friend = self._friends[id]
-                    break
+        
+        if self._friends_by_contact.has_key(contact):
+            friend.update(self._friends_by_contact[contact])
+            return
+        
+        for id in contact.get_identifiers():
+            if id in self._friends:
+                matched_friend = self._friends[id]
+                print contact.get_name(), " -> ", matched_friend
+                if matched_friend in self._friends_by_contact.values():
+                    print "COLLISSION avoided for %s" % friend
+                else:
+                    print "Match found %s" % friend
+                    self._friends_by_contact[contact] = matched_friend
+                    friend.update(matched_friend)
                 
-        if matched_friend:
-            print contact.get_name(), " -> ", matched_friend['profile_url']
-        else:
-            print contact.get_name(), " no match to Twitter with ", contact.get_identifiers()
-            
     
     
     # -----------------------------------------------------------------------