]> git.donarmstrong.com Git - bin.git/blob - get_coverart
add mutt alias which executes neomutt if that exists
[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 ($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};
108         }
109     }
110     if (not length $options{asin}) {
111         print STDERR "Unable to find cover for artist $options{artist} and album $options{album}\n" unless
112         exit 1;
113     }
114 }
115
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";
119     exit 1;
120 }
121 $content = $m->content();
122 $parser = HTML::TokeParser->new(\$content);
123 my $image;
124 while ($token = $parser->get_tag('img')) {
125     if (exists $token->[1]{id} and $token->[1]{id} eq 'prodImage') {
126         $image = $token->[1]{src};
127     }
128 }
129 mm_get($m,$image);
130 print {$wfh} $m->content;
131
132
133 sub mm_get{
134     my ($m,$url) = @_;
135     my $rerun = 8;
136     my $return;
137     do {
138         eval {
139             $return = $m->get($url);
140         };
141     } while ($@ and
142              ($rerun-- > 0) and sleep 5);
143     return $return;
144 }
145
146
147 __END__