Adding photos to my Ubuntu One contacts

We’ve recently added display of contact images to the Ubuntu One website, in preparation for starting to have images for all your contacts. Over the weekend, Andy Mabbett tweeted “I really, really, really wish @GoogleContacts would fetch Gravatars for all my contacts”, and I thought: yeah, Ubuntu One should do that too. So, hopefully we’ll be able to look at that on the U1 server so you automagically get images for all your contacts who have gravatars. (Obviously, the set of people who have friends with gravatars is fairly closely aligned with the set of people who are techies, but, well, I am, so worth a try.)

Anyway, we don’t have the Ubuntu One servers doing this (yet? the bloke you want to convince that this is a good idea is beuno), but Ubuntu One contacts have the advantage that they’re all synced down to your machine as well (hooray), so it’s easy to do this on your computer now rather than wait for the Ubuntu One servers to do it. So, a quick script.

Note: this script uses the new undeprecated desktopcouch APIs in Ubuntu 11.04, which is not yet released. So it likely won’t work for you right now unless you’re already running Ubuntu natty.

from desktopcouch.application.server import DesktopDatabase
from desktopcouch.records.record import Record
import urllib, md5
db = DesktopDatabase("contacts")
for contact in db.get_records(create_view=True, record_type="http://www.freedesktop.org/wiki/Specifications/desktopcouch/contact"):
    # sigh, db.get_records doesn't get Records at all, it gets a ViewResult. And
    # we can't just wrap row.value in a Record because that doesn't get attachments.
    # So re-fetch the Record from the database.
    r = db.get_record(contact.value['_id'])
    emails = r.get("email_addresses", None)
    if emails:
        for email in emails:
            addr = email.get('address', None)
            if addr:
                if "photo" in r.list_attachments():
                    print "Email %s already has photo in contact record" % addr
                    continue
                print ("Checking addr %s in record id %s:" % (addr, r.record_id)),
                hsh = md5.md5(addr.lower()).hexdigest().lower()
                fp = urllib.urlopen("http://www.gravatar.com/avatar/%s?d=404&s=50" % hsh)
                if fp.getcode() == 404:
                    print "not found."
                else:
                    r.attach(fp.read(), "photo", fp.info().get("Content-Type", "image/jpeg"))
                    fp.close()
                    db.put_record(r)
                    print "found!"

And now I have little pictures next to 123 out of 939 of my contacts on the Ubuntu One website (and hopefully soon in Evolution and on my phone as well).

More in the discussion (powered by webmentions)

  • (no mentions, yet.)