Add phone number support to friends, and contacts, and populate from
[hermes] / package / src / org / maemo / hermes / engine / linkedin / api.py
index 882360a..6f87b2c 100644 (file)
@@ -3,12 +3,14 @@ import httplib
 import gnome.gconf
 from oauth import oauth
 from xml.dom.minidom import parseString
+from org.maemo.hermes.engine.phonenumber import PhoneNumber
 from org.maemo.hermes.engine.friend import Friend
 
 class LinkedInApi():
     """LinkedIn API for Hermes.
                 
        Copyright (c) Fredrik Wendt <fredrik@wendt.se> 2010.
+       Copyright (c) Andrew Flegg <andrew@bleb.org> 2010.
        Released under the Artistic Licence."""
        
     GCONF_API_KEY = '/apps/maemo/hermes/linkedin_key'
@@ -18,7 +20,7 @@ class LinkedInApi():
     
     LI_SERVER = "api.linkedin.com"
     LI_API_URL = "https://api.linkedin.com"
-    LI_CONN_API_URL = LI_API_URL + "/v1/people/~/connections"
+    LI_CONN_API_URL = LI_API_URL + "/v1/people/~/connections:(id,first-name,last-name,picture-url,site-standard-profile-request:(url),date-of-birth,main-address,location:(country:(code)),phone-numbers,member-url-resources)"
     LI_PROFILE_API_URL = LI_API_URL + "/v1/people/~"
 
     REQUEST_TOKEN_URL = LI_API_URL + "/uas/oauth/requestToken"
@@ -96,9 +98,10 @@ class LinkedInApi():
     # -----------------------------------------------------------------------
     def _parse_dom(self, dom):
         def get_first_tag(node, tagName):
-            tags = node.getElementsByTagName(tagName)
-            if tags and len(tags) > 0:
-                return tags[0]
+            if node:
+                tags = node.getElementsByTagName(tagName)
+                if tags and len(tags) > 0:
+                    return tags[0]
         
         def extract(node, tagName):
             tag = get_first_tag(node, tagName)
@@ -110,6 +113,22 @@ class LinkedInApi():
             if tag:
                 url = extract(tag, 'url').replace("&amp;", "&")
                 return re.sub('[?&](auth|trk)\w*=[^&]*', '', url)
+            
+        def extract_phone_numbers(node):
+            country = extract(get_first_tag(node, 'country'), 'code')
+            tag = get_first_tag(node, 'phone-numbers')
+            numbers = []
+            for element in tag.childNodes:
+                if element.nodeName != 'phone-number':
+                    continue
+                 
+                phone_type = extract(element, 'phone-type')
+                device = phone_type == 'mobile' and phone_type or None
+                type = phone_type in set(['home', 'work']) and phone_type or None
+                
+                number = PhoneNumber(extract(element, 'phone-number'), device = device, type = type, country = country)
+                numbers.append(number)
+            return numbers
         
         # look for errors
         errors = dom.getElementsByTagName('error')
@@ -136,6 +155,9 @@ class LinkedInApi():
                 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)
                 
                 friends.append(friend)