Pulling avatars from twitter in a Rails App
Something which may be of use to all bloggers and social app builders is that it is pretty easy to pull someones twitter profile photo in RoR heres the code :
# call the twitter api
url = 'http://twitter.com/users/show/show.xml?email='+CGI.escape(test@test.test)
# get the XML data as a string
xml_data = Net::HTTP.get_response(URI.parse(url)).body
# extract profile image tag information
doc = REXML::Document.new(xml_data)
profile_image = nil
doc.elements.each('user/profile_image_url') do |ele|
profile_image = ele.text
end
# if we have an image and its not a default avatar then continue
if profile_image and ! profile_image.include? "static.twitter.com"
begin
profile_image=profile_image.gsub("_normal","_bigger")
filename="/tmp/"+rand(100000).to_s+File.extname(profile_image).downcase
file=open(filename,"w")
file.write(open(profile_image).read)
file.close
# do as you will with the avatar
rescue
#fail
end
else
#fail
end
Tags: api, avatar, ruby, Ruby on Rails, twitter
January 21st, 2009 at 3:58 pm
[...] my dealings with twitter avatars (see here) I discovered how to resize and crop an image using MiniMagick. This is useful when dealing with [...]