X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=get_coverart;h=ea5f654a090cae7b457c63afa6cf17ca557e8b54;hb=3d5241a316e3ff729b19b878b0841558120f75e9;hp=6633e8e257602cc115f21686bf839b2d67b8c2ae;hpb=01b0563f74ef39631e7df188749ead7e3217d91f;p=bin.git diff --git a/get_coverart b/get_coverart index 6633e8e..ea5f654 100755 --- a/get_coverart +++ b/get_coverart @@ -66,6 +66,8 @@ my %options = (debug => 0, GetOptions(\%options, 'asin=s', + 'artist=s', + 'album=s', 'cover=s', 'debug|d+','help|h|?','man|m'); @@ -89,15 +91,37 @@ if (exists $options{cover}) { 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}; }