Premshree Pillai ([info]premshree) wrote,
@ 2004-12-21 11:47:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Current music:U2 - Vertigo

Current playlist

</a></a></a>
</a></a></a>
Current playlist
So I wrote some Ruby code to add my latest playlist to my journal. It’ll appear on the right side of my journal.

What I wanted was to include images of album covers along with links to Amazon. This would have involved testing HTTP GET/POST requests to my journal, and I’m sure that while testing the stuff I would have messed up a lot (and it’s a pain anyway).

So I don’t have links, which means it’s quite senseless. But what the heck, it adds color to my blog, so I’m letting it be.

The script reads the playlist from an M3U file, grabs the images from Amazon, and FTPs ’em to my server. Requires Ruby/Amazon. And, of course, to make AWS calls you&rsquo'll need an AWS subscription ID.

Those images will change as my playlist changes.

Ruby code »
#!/usr/bin/ruby

#
# $3:40 AM 12/21/2004$ $premshree$
#

require 'amazon/search'
include Amazon::Search
require 'net/ftp'
require 'net/http'

def ftp_image(image)
	ftp = Net::FTP.new($server_ftp)
	ftp.login($server_user, $server_pass)
	files = ftp.chdir($server_path)
	ftp.putbinaryfile(image)
	ftp.close
end

def dump_image(url, filename)
	resp = Net::HTTP.get_response(URI.parse(url))
	image_data = resp.body
	File.open(filename, 'wb') { |f|
		f << image_data
	}
	ftp_image(filename)
	$count = $count + 1
end

def get_product_details(artist)
	begin
		$req.artist_search(artist, 'music', LITE, 1) { |prod|
			artist = prod['artists'][0].to_s
			album = prod['product_name'].to_s
			image_url_small = prod['image_url_small'].to_s
			dump_image(image_url_small, 'image' + ($count+1).to_s + '.jpg')
			return [artist, album, image_url_small]
		}
	rescue
		print
	end
end

def process_m3u(file)
	artists = []
	data = File.open(file, 'rb').readlines
	data.each { |line|
		if (line =~ /(EXTINF:)([0-9]+,)([\w\s\d]+) - ([\w\s\d]+)\r\n$/)
			if (!artists.include?($3))
				artists << $3
			end
		end
	}
	artists.each { |artist|
		get_product_details(artist)
		if ($count == 6)
			break
		end
	}
end

$count = 0

SUBSCRIPTION_ID = 'xxx' # AWS subscription ID (see http://www.amazon.com/webservices)
$server_ftp = 'ftp.myserver.com'
$server_user = 'foo'
$server_pass = 'bar'
$server_path = '/path/to/dir'

$req = Request.new(SUBSCRIPTION_ID)
process_m3u('mylist.m3u')


(Post a new comment)


[info]four
2004-12-21 06:14 am UTC (link)
dude. you are awesome.

(Reply to this)

(Deleted post)

[info]premshree
2004-12-21 06:23 am UTC (link)
is anybody else going too? :-?

(Reply to this)(Parent)(Thread)


[info]astra1111
2004-12-21 06:26 am UTC (link)
not that i know.. philip is also there for yahoo.. so i was wondering...

(Reply to this)(Parent)(Thread)


[info]premshree
2004-12-21 04:37 pm UTC (link)
Philip's been there for quite sometime now.

(Reply to this)(Parent)


[info]freegeek
2004-12-21 06:23 am UTC (link)
Nice.

(Reply to this)


[info]vighy
2004-12-21 07:17 am UTC (link)
nice.

the pic is damn small... can't identify any of the artistes :-/

(Reply to this)(Thread)


[info]premshree
2004-12-21 12:48 pm UTC (link)
That's what I said in my post. ;)

Technical glitches, or... technical laziness. :)

(Reply to this)(Parent)


[info]solzaire
2004-12-21 07:46 am UTC (link)
Good stuff!

(Reply to this)


[info]louiswu
2004-12-22 09:44 am UTC (link)
Larger album covers might be available on freedb.org or such, no?

(Reply to this)(Thread)


[info]premshree
2004-12-22 09:57 am UTC (link)
Amazon provides large, medium, and small images of all products; I’m using the small images for the “Last Playlist” thing that you can see at the right column on my journal.

Ideally I should link to the Amazon product link and also display the artist and album on mouseover. But I’m not doing that because of the reasons I mentioned in the post.

(Reply to this)(Parent)


[info]tushar
2005-01-12 07:31 am UTC (link)
Your Perl is showing :)

(Reply to this)(Thread)


[info]premshree
2005-01-12 08:43 am UTC (link)
Heh, but not really. :)

(Reply to this)(Parent)


Create an Account
Forgot your login?
Login w/ OpenID
English • Español • Deutsch • Русский…