Big update to 0.1.0. Improved error handling, syncing, the works...
[hermes] / package / src / hermes.py
index ffed489..e7ec2fc 100644 (file)
@@ -2,7 +2,7 @@ import os.path
 import evolution
 from facebook import Facebook, FacebookError
 import twitter
-import unicodedata
+import trans
 import gnome.gconf
 from contacts import ContactStore
 import names
@@ -26,7 +26,7 @@ class Hermes:
 
 
   # -----------------------------------------------------------------------
-  def __init__(self, callback, twitter = None, facebook = False):
+  def __init__(self, callback, twitter = None, facebook = False, empty = False):
     """Constructor. Passed a callback which must implement three informational
        methods:
        
@@ -45,12 +45,16 @@ class Hermes:
                    
          facebook - boolean indicating if Facebook should be used. Defaults to
                     False.
+
+         empty - boolean indicating if 'empty' contacts consisting of a profile
+                 URL and birthday should be created.
                           """
 
     self.gc       = gnome.gconf.client_get_default()
     self.callback = callback
     self.twitter  = twitter
     self.facebook = facebook
+    self.create_empty = empty
 
     # -- Check the environment is going to work...
     #
@@ -117,11 +121,14 @@ class Hermes:
         self.do_fb_login()
 
       # Get the list of friends...
-      attrs = ['uid', 'name', 'pic_big', 'birthday_date', 'profile_url']
+      attrs = ['uid', 'name', 'pic_big', 'birthday_date', 'profile_url', 'first_name', 'last_name', 'website']
       for friend in self.fb.users.getInfo(self.fb.friends.get(), attrs):
-        friend['name'] = unicodedata.normalize('NFKD', unicode(friend['name']))
+        key = unicode(friend['name']).encode('trans')
+        self.friends[key] = friend
         friend['pic']  = friend[attrs[2]]
-        self.friends[friend['name']] = friend
+        if friend['website']:
+          friend['homepage'] = friend['website']
+
         if not friend['pic']:
           self.blocked_pictures.append(friend)
           
@@ -132,7 +139,7 @@ class Hermes:
       api = twitter.Api(username=user, password=passwd)
       users = api.GetFriends()
       for friend in api.GetFriends():
-        self.friends[friend.name] = {'name': unicodedata.normalize('NFKD', unicode(friend.name)), 'pic': friend.profile_image_url, 'birthday_date': None, 'twitter_url': 'http://twitter.com/%s' % (friend.screen_name), 'homepage' : friend.url}
+        self.friends[friend.name] = {'name': unicode(friend.name).encode('trans'), 'pic': friend.profile_image_url, 'birthday_date': None, 'twitter_url': 'http://twitter.com/%s' % (friend.screen_name), 'homepage' : friend.url}
   
     # TODO What if the user has *no* contacts?
 
@@ -147,7 +154,7 @@ class Hermes:
     print "+++ Syncing contacts..."
     addresses = evolution.ebook.open_addressbook('default')
     print "+++ Addressbook opened..."
-    store = ContactStore(addresses)
+    self.store = ContactStore(addresses)
     print "+++ Contact store created..."
     self.updated = []
     self.unmatched = []
@@ -162,29 +169,9 @@ class Hermes:
       found = False
       for name in names.variants(contact.get_name()):
         if name in self.friends:
-          friend = self.friends[name]
+          updated = self.update_contact(contact, self.friends[name], resync)
           found = True
-          updated = False
-      
-          if friend['pic'] and (resync or contact.get_property('photo') is None):
-            updated = store.set_photo(contact, friend['pic']) or updated
-        
-          if friend['birthday_date'] and (resync or contact.get_property('birth-date') is None):
-            date_str = friend['birthday_date'].split('/')
-            date_str.append('0')
-            updated = store.set_birthday(contact, int(date_str[1]),
-                                                  int(date_str[0]),
-                                                  int(date_str[2])) or updated
-
-          if 'profile_url' in friend and friend['profile_url']:
-            updated = store.add_url(contact, friend['profile_url'], unique='facebook.com') or updated
-            
-          if 'twitter_url' in friend and friend['twitter_url']:
-            updated = store.add_url(contact, friend['twitter_url'], unique='twitter.com') or updated
-            
-          if 'homepage' in friend and friend['homepage']:
-            updated = store.add_url(contact, friend['homepage']) or updated
-    
+   
           if updated:
             self.updated.append(contact)
             addresses.commit_contact(contact)
@@ -197,5 +184,57 @@ class Hermes:
       else:
         self.unmatched.append(contact)
 
-    store.close()
+    # -- Create 'empty' contacts with birthdays...
+    #
+    if self.create_empty:
+      for name in self.friends:
+        friend = self.friends[name]
+        if 'contact' in friend or 'birthday_date' not in friend or not friend['birthday_date']:
+          continue
+
+        contact = evolution.ebook.EContact()
+        contact.props.full_name = friend['name']
+        contact.props.given_name = friend['first_name']
+        contact.props.family_name = friend['last_name']
+
+        self.update_contact(contact, friend)
+   
+        addresses.add_contact(contact)
+        self.updated.append(contact)
+        addresses.commit_contact(contact)
+
+        print "Created [%s]" % (contact.get_name())
+        self.matched.append(contact)
+
+    self.store.close()
+
+
+  # -----------------------------------------------------------------------
+  def update_contact(self, contact, friend, resync = False):
+    """Update the given contact with information from the 'friend'
+       dictionary."""
+
+    updated = False
+    friend['contact'] = contact
+      
+    if friend['pic'] and (resync or contact.get_property('photo') is None):
+      updated = self.store.set_photo(contact, friend['pic']) or updated
+        
+    if friend['birthday_date'] and (resync or contact.get_property('birth-date') is None):
+      date_str = friend['birthday_date'].split('/')
+      date_str.append('0')
+      updated = self.store.set_birthday(contact, int(date_str[1]),
+                                                 int(date_str[0]),
+                                                 int(date_str[2])) or updated
+
+    if 'profile_url' in friend and friend['profile_url']:
+      updated = self.store.add_url(contact, friend['profile_url'], unique='facebook.com') or updated
+            
+    if 'twitter_url' in friend and friend['twitter_url']:
+      updated = self.store.add_url(contact, friend['twitter_url'], unique='twitter.com') or updated
+            
+    if 'homepage' in friend and friend['homepage']:
+      updated = self.store.add_url(contact, friend['homepage']) or updated
+
+    return updated