Ruby Script to Import Google Contact Photos From Gravatar

Google Contact photos are a much neglected feature of the Google Stack. It really adds to the user experience when you see each of your contact photos when you make or receive a call. However, it can be a real pain (especially if you have hundreds of contacts).

But I had an idea recently, to try and match my Google Contact emails with Gravatar and try to auto-populate some of the dozens of contacts that didn’t already have a photo (after all a Gravatar is better than nothing).

So I wrote a Ruby script to find my contacts missing a photo and try to update it with a Gravatar (wherever possible). NB: You may need to first install the GData (Google Data) gem by opening a Terminal window and issuing: sudo gem install gdata.

#!/usr/bin/env ruby

# Google Contact Photos - Gravatar Importer
# Written by Ashley Angell
# Licenced under Creative Commons with Attribution

require "rubygems"
require "gdata"
require "rexml/document"
require "digest/md5"
require "net/http"
include REXML

none = 'd5fe5cbcc31cff5f8ac010db72eb000c'
user = ARGV[0]
pass = ARGV[1]

client =
client.clientlogin(user, pass)
data = client.get("{user}/full?max-results=10000")
myxml = data.body
p "contacts"
puts "-"*70
i = 0
myxml.each_element("feed/entry") do |e|
    gd = e.elements['gd:email']
    if !gd.nil?
      email = gd.attributes['address'].downcase
      hash = Digest::MD5.hexdigest(email)
      image_src = "{hash}"
      nil_image = false
      image_element = e.get_elements("link[@rel='']")[0]
      if !image_element.nil? and image_element.attributes['gd:etag'].nil?
        data = nil
        md5 = nil
        Net::HTTP.start(URI.parse(image_src).host) do |http|
          resp = http.get(URI.parse(image_src).path)
          data = resp.body
          md5 = Digest::MD5.hexdigest(data)
"#{email}.png", 'w') do |f|
            f.puts data if md5 != none
        md5 = Digest::MD5.hexdigest(data)
        if md5 != none
          puts "#{email} > #{image_src}"
          client.put_file(image_element.attributes['href'], "#{email}.png", 'image/png')
          i = i + 1
          puts "#{email} > no match"
        puts "#{email} > skipped (already has photo)"
      File.delete("#{email}.png") if File.exists?("#{email}.png")
  rescue Exception => ex
    puts ex
puts "Updated #{i} contact photos"

To execute it, simply copy and paste this into a text editor (or download it and unzip) and from Terminal (command) window and execute the following commands:

sudo chmod +x googlegravatarimporter.rb [Enter]
./googlegravatarer.rb your_password [Enter]

It will cycle through your Google Contacts and indicate what action was taken. For me, surprisingly updated a few dozen contacts (even more than I expected).

I’ve posted this here for others that might want to do the same thing but cannot be bothered writing the script for it. Consider it posted here under Creative Commons with Attribution.

Convert SQL Server Database to a SQLite Database

Recently, I wanted to resurrect an old project of mine I worked on in my spare time originally designed to work on .Net technologies. Naturally, I backed it against a SQL Server 2005 Database which turned out to be a bad idea because it made portability of that data a bit of a nightmare.

So it was with great happiness that I found this tool (mirrored here) by liron.levi who posted an article on CodeProject on how to accomplish this task. You still need a Windows machine and an install of SQL Server (Express Edition with Advanced Tools is alright) but it got the job done just fine for me.

If you want a no-fuss easy way to convert your databases into a format easier to deal with and a lot more portable to-boot.

How to Boot Toshiba Portege M200 off an SD Memory Card

A long while ago I became the proud owner of one o the best Tablet PCs on the market, the Toshiba Portege M200. Problem is that if something goes awry on the internal hard disk, then you’re in trouble because the M200 is notoriously difficult to boot on external media. This was something I needed to do after trying out Windows 7 on it (which worked extremely well by the way) – but it came at a cost too high – missing features and tools and drivers that the default factory install has.

Things are made complex because while it is technically possible to boot off an external CD/DVD ROM, the number of external drive it will actually boot off are very few. Additionally, the BIOS wont boot off USB thumb drives (at all!) and USB hard disks either. There is one saving grace – you can boot off an SD memory card (as long as it *not* SDHC) from the built-in reader. The trouble with THIS is that it will only boot off an SD card if its been formatted and made bootable ‘just right’. Its so finiky that Toshiba have actually created some tools to aide in the process of making them bootable.

So, if you have a standard SD memory card, you can use the Toshiba SD Memory Format Utility and the Toshiba SD Memory Boot Utility to create a bootable image of the restore CDs. Obviously if the default install is bung, then how can you use these tools?

Well the good news is, I’ve got a copy of them from the Toshiba website. I have hosted them here in case they disappear, here is the Toshiba SD Memory Format Utility and here is the Toshiba SD Memory Boot Utility.

What I did to reinstall the factory image on my Portege M200 Tablet PC:

  1. Download the 2 links above and install them.
  2. Run ‘as administrator’ (if on Vista or Windows 7) the format utility and format the SD card.
  3. Run ‘as administrator’ (if on Vista or Windows 7) the boot utility and select the BOOT.IMG file from the /bin folder on the first restore CD. Here is a copy if you don’t have the disks.
  4. Restart the computer pressing F2 at POST and with the SD card in the reader, and use the arrows keys to select the SD/Floppy boot device.  Press [Enter].

When I booted it still gave me the CD ROM selection screen, which I eventually discovered was actually compatible with the External USB DVD ROM I was using (wouldn’t boot off it – but option ‘9’ worked in DOS) – after this, it just started installing the factory image as normal.

Any problems, drop me a line and I will try to help.  I know how frustrating this can be after spending most of the day trying Linux boot disks and direct copying boot sectors etc.  Ahh! but finally success!


‘File not found: lib’ Error installing Rails Gem

I recently had a problem trying to install Rails 3 on my MacBook with a fresh OSX Snow Leopard:

sudo gem install rails
Password: {entered}
Successfully installed rails-3.0.7
1 gem installed
Installing ri documentation for rails-3.0.7...
File not found: lib

Turns out this is a somewhat common problem.  But it seems that the solution is easy, just manually reinstall RDoc. To do this run these 3 commands:

sudo gem install rdoc-data
sudo rdoc-data --install
sudo gem rdoc --all --overwrite

The last line in particular will re-generate all the documentation for your installed gems (including Rails) and can take a while, but you should be able to confirm the fix by reissuing the Rails gem install command:

sudo gem install rails

shows that rails now installs properly and says that it has installed both ri and RDoc documentation without issue.