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>.
16 get_coverart -- download coverart
20 get_coverart --asin B000NIIUX8 --cover cover.jpg [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
35 Debug verbosity. (Default 0)
39 Display brief usage information.
49 get_coverart --asin B000NIIUX8 --cover cover.jpg
50 get_coverart --asin B000NIIUX8 > cover.jpg
62 my %options = (debug => 0,
72 'debug|d+','help|h|?','man|m');
74 pod2usage() if $options{help};
75 pod2usage({verbose=>2}) if $options{man};
77 $DEBUG = $options{debug};
80 if (not exists $options{asin}) {
81 push @USAGE_ERRORS,"You must give an ASIN.";
84 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
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}: $!";
92 my $m = WWW::Mechanize->new();
97 if (not length $options{asin}) {
98 my ($artist,$album) = map {s/_/ /g; $_} @options{qw(artist album)};
99 my $url_end = uri_escape("$artist $album");
100 mm_get($m,"http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dpopular&field-keywords=$url_end");
101 $content = $m->content();
102 $parser = HTML::TokeParser->new(\$content);
103 while ($token = $parser->get_tag('div')) {
104 if (exists $token->[1]{class} and $token->[1]{class} eq 'productImage') {
105 $token= $parser->get_token();
106 ($options{asin}) = $token->[2]{href} =~ m{^http://[^/]+/[^/]+/dp/([^/]+)$};
107 next unless defined $options{asin};
110 if (not length $options{asin}) {
111 print STDERR "Unable to find cover for artist $options{artist} and album $options{album}\n" unless
116 mm_get($m,"http://www.amazon.com/gp/product/$options{asin}");
117 if ($m->status() != 200) {
118 print STDERR "Unable to get product for asin $options{asin}\n";
121 $content = $m->content();
122 $parser = HTML::TokeParser->new(\$content);
124 while ($token = $parser->get_tag('img')) {
125 if (exists $token->[1]{id} and $token->[1]{id} eq 'prodImage') {
126 $image = $token->[1]{src};
130 print {$wfh} $m->content;
139 $return = $m->get($url);
142 ($rerun-- > 0) and sleep 5);