From 01b0563f74ef39631e7df188749ead7e3217d91f Mon Sep 17 00:00:00 2001 From: Don Armstrong Date: Tue, 23 Aug 2011 00:34:03 +0000 Subject: [PATCH] add get_coverart --- get_coverart | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100755 get_coverart diff --git a/get_coverart b/get_coverart new file mode 100755 index 0000000..6633e8e --- /dev/null +++ b/get_coverart @@ -0,0 +1,123 @@ +#! /usr/bin/perl +# get_coverart downloads coverart, and is released +# under the terms of the GPL version 2, or any later version, at your +# option. See the file README and COPYING for more information. +# Copyright 2011 by Don Armstrong . + + +use warnings; +use strict; + +use Getopt::Long; +use Pod::Usage; + +=head1 NAME + +get_coverart -- download coverart + +=head1 SYNOPSIS + +get_coverart --asin B000NIIUX8 --cover cover.jpg [options] + + Options: + --asin Amazon image ID + --cover file name to save cover to + --debug, -d debugging level (Default 0) + --help, -h display this help + --man, -m display manual + +=head1 OPTIONS + +=over + +=item B<--debug, -d> + +Debug verbosity. (Default 0) + +=item B<--help, -h> + +Display brief usage information. + +=item B<--man, -m> + +Display this manual. + +=back + +=head1 EXAMPLES + +get_coverart --asin B000NIIUX8 --cover cover.jpg +get_coverart --asin B000NIIUX8 > cover.jpg + +=cut + + +use URI::Escape; +use WWW::Mechanize; +use HTTP::Cookies; +use IO::Dir; +use IO::File; +use vars qw($DEBUG); + +my %options = (debug => 0, + help => 0, + man => 0, + ); + +GetOptions(\%options, + 'asin=s', + 'cover=s', + 'debug|d+','help|h|?','man|m'); + +pod2usage() if $options{help}; +pod2usage({verbose=>2}) if $options{man}; + +$DEBUG = $options{debug}; + +my @USAGE_ERRORS; +if (not exists $options{asin}) { + push @USAGE_ERRORS,"You must give an ASIN."; +} + +pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS; + + +my $wfh = \*STDOUT; +if (exists $options{cover}) { + $wfh = IO::File->new($options{cover},O_WRONLY|O_CREAT|O_EXCL) or die "Unable to create file: $options{cover}: $!"; +} + +my $m = WWW::Mechanize->new(); + +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}"; + exit 1; +} +my $content = $m->content(); +my $parser = HTML::TokeParser->new(\$content); +my $image; +while (my $token = $parser->get_tag('img')) { + if (exists $token->[1]{id} and $token->[1]{id} eq 'prodImage') { + $image = $token->[1]{src}; + } +} +mm_get($m,$image); +print {$wfh} $m->content; + + +sub mm_get{ + my ($m,$url) = @_; + my $rerun = 8; + my $return; + do { + eval { + $return = $m->get($url); + }; + } while ($@ and + ($rerun-- > 0) and sleep 5); + return $return; +} + + +__END__ -- 2.39.5