Skip to content

Instantly share code, notes, and snippets.

@Govan
Created May 11, 2010 20:26
Show Gist options
  • Save Govan/397820 to your computer and use it in GitHub Desktop.
Save Govan/397820 to your computer and use it in GitHub Desktop.
#
# Assuming your address book entries have a URL field 'twitter' containing either their full Twitter URL, or just
# their username, go fetch the image and drop it into the addressbook.
#
# Works for me, but your milage may vary.
#
require 'rubygems'
require 'twitter'
require 'open-uri'
require 'fileutils'
require 'appscript'
include Appscript
httpauth = Twitter::HTTPAuth.new('USERNAME', 'PASSWORD')
client = Twitter::Base.new(httpauth)
address_book_image_dir = File.expand_path("~/Library/Application Support/AddressBook/Images/")
app("Address Book").people.get.each do |person|
person.urls.get.each do |url|
if 'twitter' == (url.label.get.downcase)
puts "Updating avatar for #{person.name.get}"
twitter_url = url.value.get
twitter_id = twitter_url.split("/").last
friend = client.user(twitter_id)
twitter_avatar = friend.profile_image_url.gsub(/_normal(\.[a-zA-Z]+)$/, '_bigger\1')
new_image = File.join(address_book_image_dir, person.id_.get.split(':')[0])
File.open(new_image, 'w+') { |f| f.write open(twitter_avatar).read }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment