| Premshree Pillai ( @ 2004-12-21 11:47:00 |
| Current music: | U2 - Vertigo |
Current playlist
|
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')
</a>
</a>
</a>
</a>
</a>
</a>