Using the Amazon search API as a quick-and-dirty way to find album covers.
I recently discovered that album cover art can be
embedded in MP3 files (using the ID3v2
APIC tag). I wouldn't much care
except that I've also got an iTunes extension
(CoverSutra) that will
display that art when a song is playing.
That's kind of cool, but I've got several hundred ripped albums with no cover art. I'm not scanning it all myself!
A few web searches for cover art were enough to convince me that Amazon is a good place to get it. And they have a search API, don't they?
After a quick trip to CPAN, I had a shell script that would grab covers. One of my Twitter followers asked to see it, so here it is:
#!/usr/bin/perl -- # -*- Perl -*-
use strict;
use Net::Amazon;
use Getopt::Std;
use LWP::UserAgent;
use HTTP::Cookies;
use vars qw($opt_a $opt_k);
my $usage = "Usage: $0 -a artist -k keywords\n";
die $usage if ! getopts('a:k:');
die $usage if ! $opt_a || ! $opt_k;
my $lwpua = LWP::UserAgent->new();
my $amzua = Net::Amazon->new(token => 'YOUR-AMAZON-KEY', max_pages => 1);
# Get a request object
my $response = $amzua->search(artist => $opt_a,
mode => 'music',
keywords => $opt_k);
if ($response->is_success()) {
my $prop = $response->properties();
if (defined $prop && exists $prop->{'ImageUrlLarge'}) {
print "Match: ", $prop->{'album'};
print " by ", join(", ", @{$prop->{'artists'}}), "\n";
$response = $lwpua->get($prop->{'ImageUrlLarge'});
if ($response->is_success()) {
unlink "/tmp/cover.jpg";
open (F, ">/tmp/cover.jpg");
print F $response->content();
close (F);
system("open /tmp/cover.jpg");
}
} else {
print "No ImageUrlLarge property\n";
}
} else {
print "Error: ", $response->message(), "\n";
}
So, for example:
perl getcover.pl -a "flogging molly" -k "within mile home"returns the cover for the Flogging Molly album Within a Mile of Home.
It blindly assumes that the first match is the right one, it
unceremoniously writes the cover to
/tmp/cover.jpg, and it assumes that
open will display it. Oh, and it won't work until
you replace “YOUR-AMAZON-KEY” with, well, your
Amazon key, naturally.
Still it seems to have done a reasonable job on 350 or so albums.
Now my “rip for iTunes” script embeds cover art when it's available. And I get pretty pictures on my desktop. Sweet.
Comments:
Amazon cover lookup is built into the KDE music application Amarok...you can even select a country to get locale-specific art. No need for a key, so it must do it by scraping the site itself.
Hi, am I allowed to use your code in my application?