]> git.donarmstrong.com Git - bin.git/blob - get_coverart
* handle missing asin properly
[bin.git] / get_coverart
1 #! /usr/bin/perl
2 # get_coverart downloads coverart, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2011 by Don Armstrong <don@donarmstrong.com>.
6
7
8 use warnings;
9 use strict;
10
11 use Getopt::Long;
12 use Pod::Usage;
13
14 =head1 NAME
15
16 get_coverart -- download coverart
17
18 =head1 SYNOPSIS
19
20 get_coverart --asin B000NIIUX8 --cover cover.jpg [options]
21
22  Options:
23   --asin Amazon image ID
24   --cover file name to save cover to
25   --debug, -d debugging level (Default 0)
26   --help, -h display this help
27   --man, -m display manual
28
29 =head1 OPTIONS
30
31 =over
32
33 =item B<--debug, -d>
34
35 Debug verbosity. (Default 0)
36
37 =item B<--help, -h>
38
39 Display brief usage information.
40
41 =item B<--man, -m>
42
43 Display this manual.
44
45 =back
46
47 =head1 EXAMPLES
48
49 get_coverart --asin B000NIIUX8 --cover cover.jpg
50 get_coverart --asin B000NIIUX8 > cover.jpg
51
52 =cut
53
54
55 use URI::Escape;
56 use WWW::Mechanize;
57 use HTTP::Cookies;
58 use IO::Dir;
59 use IO::File;
60 use vars qw($DEBUG);
61
62 my %options = (debug           => 0,
63                help            => 0,
64                man             => 0,
65                );
66
67 GetOptions(\%options,
68            'asin=s',
69            'artist=s',
70            'album=s',
71            'cover=s',
72            'debug|d+','help|h|?','man|m');
73
74 pod2usage() if $options{help};
75 pod2usage({verbose=>2}) if $options{man};
76
77 $DEBUG = $options{debug};
78
79 my @USAGE_ERRORS;
80 if (not exists $options{asin}) {
81     push @USAGE_ERRORS,"You must give an ASIN.";
82 }
83
84 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
85
86
87 my $wfh = \*STDOUT;
88 if (exists $options{cover}) {
89     $wfh = IO::File->new($options{cover},O_WRONLY|O_CREAT|O_EXCL) or die "Unable to create file: $options{cover}: $!";
90 }
91
92 my $m = WWW::Mechanize->new();
93
94 my $content;
95 my $parser;
96 my $token;
97 if (not length $options{asin}) {
98     my $url_end = uri_escape("$options{artist} $options{album}");
99     mm_get($m,"http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dpopular&field-keywords=$url_end");
100     $content = $m->content();
101     $parser = HTML::TokeParser->new(\$content);
102     while ($token = $parser->get_tag('div')) {
103         if (exists $token->[1]{class} and $token->[1]{class} eq 'productImage') {
104             $token= $parser->get_token();
105             ($options{asin}) = $token->[2]{href} =~ m{^http://[^/]+/[^/]+/dp/([^/]+)$};
106             next unless defined $options{asin};
107         }
108     }
109     if (not length $options{asin}) {
110         print STDERR "Unable to find cover for artist $options{artist} and album $options{album}\n" unless
111         exit 1;
112     }
113 }
114
115 mm_get($m,"http://www.amazon.com/gp/product/$options{asin}");
116 if ($m->status() != 200) {
117     print STDERR "Unable to get product for asin $options{asin}\n";
118     exit 1;
119 }
120 $content = $m->content();
121 $parser = HTML::TokeParser->new(\$content);
122 my $image;
123 while ($token = $parser->get_tag('img')) {
124     if (exists $token->[1]{id} and $token->[1]{id} eq 'prodImage') {
125         $image = $token->[1]{src};
126     }
127 }
128 mm_get($m,$image);
129 print {$wfh} $m->content;
130
131
132 sub mm_get{
133     my ($m,$url) = @_;
134     my $rerun = 8;
135     my $return;
136     do {
137         eval {
138             $return = $m->get($url);
139         };
140     } while ($@ and
141              ($rerun-- > 0) and sleep 5);
142     return $return;
143 }
144
145
146 __END__