adding default _attachments for the profile url to contacts
authorBryan Clark <clarkbw@gnome.org>
Wed, 18 Feb 2009 10:45:00 -0800
changeset 47 ba518ea3ee85f7798835c50def4a433f1f9a6106
parent 46 8ee054c5fcc6120101915d69853487fcd0993ae8
child 48 384724bf1aee5c50c16e7bb840e0c33698ec30c0
push id1
push userroot
push dateWed, 08 Apr 2009 01:46:05 +0000
adding default _attachments for the profile url to contacts adding url kind of identity to contacts if it is found adding location field to contacts
server/python/junius/gettweets.py
--- a/server/python/junius/gettweets.py
+++ b/server/python/junius/gettweets.py
@@ -1,13 +1,14 @@
 #!/usr/bin/env python
 
 import base64, datetime, email.utils
 import pprint
 import re
+from urllib2 import urlopen
 
 import twitter
 
 import junius.model as model
 
 '''
 Fetch contacts, and tweets.
 '''
@@ -28,22 +29,40 @@ class JuniusAccount(object):
     def create_contact_if_necessary(self, username, account=None):
         if account is None:
             # after getting this info we should really be putting lots of it into
             # the contact database.  Location, Picture, Web Site
             account = self.twitter_account.GetUser(username)
 
         contacts = model.Contact.by_identity(self.dbs.contacts,
                                              key=['twitter', username])
+
         if len(contacts) == 0:
             # the contact does't exist, create it
+
+            attachments = {}
+            if account.profile_image_url:
+                try:
+                  response = urlopen(account.profile_image_url)
+                  attachments['default'] = { 'content_type': response.info()['Content-Type'],
+                                             'data': base64.b64encode(response.read()) }
+                except:
+                    pass
+
+            identities = [{'kind': 'twitter', 'value': username }]
+            if account.url: 
+                identities.append({'kind': 'url' , 'value' : account.url })
+
             contact = model.Contact(
                 name=account.name,
-                identities=[{'kind': 'twitter', 'value': username }]
+                identities=identities,
+                location=account.location,
+                _attachments=attachments
             )
+
             contact.store(self.dbs.contacts)
         else:
             contact = [model.Contact.load(self.dbs.contacts,contact.value['_id']) for contact in contacts.rows][0]
 
         return contact
 
     def sync(self):
         print '***** Fetching tweets'