From 142f2bd25e6ca952b3b5c7fb10d3d873861c853b Mon Sep 17 00:00:00 2001 From: Don Armstrong Date: Thu, 25 Aug 2011 22:12:04 +0000 Subject: [PATCH] * handle missing asin properly --- get_coverart | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/get_coverart b/get_coverart index 6633e8e..41fb043 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,36 @@ if (exists $options{cover}) { my $m = WWW::Mechanize->new(); +my $content; +my $parser; +my $token; +if (not length $options{asin}) { + my $url_end = uri_escape("$options{artist} $options{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}; } -- 2.39.5