Fetching album covers from Last.Fm API 3

Posted by Dan Sosedoff on February 15, 2009

As previous post was about fetching covers media from Amazon Web Services, this post will be about fetching covers from popular music site – Last.fm. API documentation page

#!/usr/bin/ruby
 
require 'rubygems'
require 'net/http'
require 'cgi'
require 'xmlsimple'
 
# key from API documentation
$lastfm_key = "b25b959554ed76058ac220b7b2e0a026" 
$lastfm_host = "ws.audioscrobbler.com"
 
def fetch_cover(artist, album)
	artist = CGI.escape(artist)
	album = CGI.escape(album)
 
	path = "/2.0/?method=album.getinfo&api_key=#{$lastfm_key}&artist=#{artist}&album=#{album}"
	data = Net::HTTP.get($lastfm_host, path)
	xml = XmlSimple.xml_in(data)
	if xml['status'] == 'ok' then
		album = xml['album'][0]
 
		cover = {
			:small => album['image'][1]['content'],
			:medium => album['image'][2]['content'],
			:big => album['image'][3]['content']
		}
 
		return cover
	end
 
	return nil
end
 
puts fetch_cover('Nickelback', 'Dark Horse').inspect

Download ruby script

Trackbacks

Trackbacks are closed.

Comments

Comments are closed.

  1. CappY Sun, 17 May 2009 05:34:23 CDT

    Nice. :) Can you done that in PHP, it will be very useful for me. :)
    P.S. Strange im using same theme like you :P

  2. sosedoff Mon, 18 May 2009 02:27:55 CDT

    Hey, CappY, here is PHP version for this script: http://files.sosedoff.com/8db65c5c/
    But you have to install SimpleXML extension (usually comes by default)

  3. CappY Mon, 18 May 2009 19:14:27 CDT

    Thanks a lot. That really helped me with my project… Cuz I’m still learning. :)