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.

Posted by Steve Loughran on 16 Apr 2008 @ 09:07am UTC #

Hi, am I allowed to use your code in my application?

Posted by Manuel on 02 Dec 2008 @ 09:45pm UTC #
Add a comment or subscribe to (existing and future) comments on this essay.
Name:
Email*:
 *Please provide your real email address; it will not be displayed as part of the comment.
Homepage:
Comment**:
 **The following markup may be used in the body of the comment: a, abbr, b, br, code, em, i, p, pre, strong, and var. You can also use character entities. Any other markup will be discarded, including all attributes (except href on a). Your tag soup will be sanitized...