GetOptions(\%options,
'asin=s',
+ 'artist=s',
+ 'album=s',
'cover=s',
'debug|d+','help|h|?','man|m');
my $m = WWW::Mechanize->new();
+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;
+ }
+}
+
mm_get($m,"http://www.amazon.com/gp/product/$options{asin}");
if ($m->status() != 200) {
- print STDERR "Unable to get product for asin $options{asin}";
+ print STDERR "Unable to get product for asin $options{asin}\n";
exit 1;
}
-my $content = $m->content();
-my $parser = HTML::TokeParser->new(\$content);
+$content = $m->content();
+$parser = HTML::TokeParser->new(\$content);
my $image;
-while (my $token = $parser->get_tag('img')) {
+while ($token = $parser->get_tag('img')) {
if (exists $token->[1]{id} and $token->[1]{id} eq 'prodImage') {
$image = $token->[1]{src};
}