]> git.donarmstrong.com Git - bin.git/blobdiff - get_coverart
add reset usb bus command
[bin.git] / get_coverart
index 6633e8e257602cc115f21686bf839b2d67b8c2ae..ea5f654a090cae7b457c63afa6cf17ca557e8b54 100755 (executable)
@@ -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};
     }