Fix phone number support, add URLs and birthdays from LinkedIn.
authorAndrew Flegg <andrew@bleb.org>
Sun, 4 Jul 2010 10:41:22 +0000 (11:41 +0100)
committerAndrew Flegg <andrew@bleb.org>
Sun, 4 Jul 2010 10:41:22 +0000 (11:41 +0100)
Completes MB#10801.

package/src/org/maemo/hermes/engine/friend.py
package/src/org/maemo/hermes/engine/linkedin/api.py

index 687f785..28b8d74 100644 (file)
@@ -27,6 +27,9 @@ class Friend():
     # public accessors -----------------
     
     def add_url(self, url):
+        if not isinstance(url, basestring):
+            print url
+            raise Exception('Not valid to add non-string URLs')
         self._add('url', url)
     
     def add_phone(self, phone):
@@ -103,7 +106,7 @@ class Friend():
            Returns flag indicating if anything *was* changed."""
         
         def set_birthday(arg):
-            # Hackily assumes Facebook format
+            # Hackily assumes Facebook format (mm/d[/y])
             date_str = arg.split('/')
             date_str.append('0')
             return contact.set_birthday(int(date_str[1]),
@@ -119,8 +122,8 @@ class Friend():
                 updated += contact.add_url(url)
 
         if self._multi_attributes.has_key('phone'):
-            for url in self._multi_attributes['phone']:
-                updated += contact.add_phone(url)
+            for phone in self._multi_attributes['phone']:
+                updated += contact.add_phone(phone)
                 
         return updated
 
index 6f87b2c..101a526 100644 (file)
@@ -130,6 +130,27 @@ class LinkedInApi():
                 numbers.append(number)
             return numbers
         
+        def extract_urls(node):
+            tag = get_first_tag(node, 'member-url-resources')
+            urls = []
+            for element in tag.getElementsByTagName('url'):
+                urls.append(element.firstChild.nodeValue.replace("&amp;", "&"))
+            return urls
+        
+        def extract_birthday(node):
+            tag = get_first_tag(node, 'date-of-birth')
+            bday = None
+            if tag:
+                month = extract(tag, 'month')
+                day   = extract(tag, 'day')
+                year  = extract(tag, 'year')
+                if month and day:
+                    bday = '%s/%s' % (extract(tag, 'month'), extract(tag, 'day'))
+                    if year:
+                        bday = '%s/%s' % (bday, year)
+                    
+            return bday
+        
         # look for errors
         errors = dom.getElementsByTagName('error')
         if (len(errors) > 0):
@@ -143,26 +164,29 @@ class LinkedInApi():
         friends = []
         people = dom.getElementsByTagName('person')
         for p in people:
-            try:
-                fn = extract(p, 'first-name')
-                ln = extract(p, 'last-name')
-                photo_url = extract(p, 'picture-url')
-                id = extract(p, 'id')
-                public_url = extract_public_url(p)
+            fn = extract(p, 'first-name')
+            ln = extract(p, 'last-name')
+            photo_url = extract(p, 'picture-url')
+            id = extract(p, 'id')
+            public_url = extract_public_url(p)
+            bday = extract_birthday(p)
 
-                name = fn + " " + ln
-                friend = Friend(name)
-                friend.add_url(public_url)
-                if photo_url:
-                    friend.set_photo_url(photo_url)
-                    
-                for number in extract_phone_numbers(p):
-                    friend.add_phone(number)
+            name = fn + " " + ln
+            friend = Friend(name)
+            friend.add_url(public_url)
+            if photo_url:
+                friend.set_photo_url(photo_url)
                 
-                friends.append(friend)
-
-            except:
-                pass
+            if bday:
+                friend.set_birthday_date(bday)
+                
+            for number in extract_phone_numbers(p):
+                friend.add_phone(number)
+                
+            for url in extract_urls(p):
+                friend.add_url(url)
+            
+            friends.append(friend)
 
         return friends