+my $content;
+my $parser;
+my $token;
+if (not length $options{asin}) {
+ my ($artist,$album) = map {s/_/ /g; $_} @options{qw(artist album)};
+ my $url_end = uri_escape("$artist $album");
+ mm_get($m,"http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dpopular&field-keywords=$url_end");
+ $content = $m->content();
+ $parser = HTML::TokeParser->new(\$content);
+ while ($token = $parser->get_tag('div')) {
+ if (exists $token->[1]{class} and $token->[1]{class} eq 'productImage') {
+ $token= $parser->get_token();
+ ($options{asin}) = $token->[2]{href} =~ m{^http://[^/]+/[^/]+/dp/([^/]+)$};
+ next unless defined $options{asin};
+ }
+ }
+ if (not length $options{asin}) {
+ print STDERR "Unable to find cover for artist $options{artist} and album $options{album}\n" unless
+ exit 1;
+ }
+}
+