-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmazonPriceChecker.pl
155 lines (109 loc) · 3.68 KB
/
AmazonPriceChecker.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!perl-5.10
use strict;
use Carp;
use
v5.10; #make use of the say command and other nifty perl 10.0 onwards goodness
use Common::Sense;
use Net::Amazon;
use IO::Prompt::Simple;
use YAML;
#enable these for more verbose logging
#use Log::Log4perl qw(:easy);
#Log::Log4perl->easy_init($DEBUG);
#set the version number in a way Getopt::Euclid can parse
BEGIN { use version; our $VERSION = qv('0.1.1_1') }
use Getopt::Euclid
; # Create a command-line parser that implements the documentation below...
my $file = $ARGV{-f};
my $csv;
#get the google login data
my $amazon_details = YAML::LoadFile( $ENV{HOME} . "/.amazon_login" )
or die "Failed to read $ENV{HOME}/.amazon_login - $!";
if ( defined($file) ) {
open $csv, '>', $file or croak "Couldn't open $file: $!";
#print the header
print {$csv}
"isbn, "
. "title, "
. "author,"
. "ListPrice,"
. "OurPrice,"
. "UsedPrice,"
. "Media,"
. "\n"
or croak "Couldn't write to $csv because: $!";
}
my $ua = Net::Amazon->new(
token => $amazon_details->{token},
secret_key => $amazon_details->{secret_key},
associate_tag => $amazon_details->{associate_tag},
locale => 'uk', #you can change this to your preferred locale
);
#continue looping this until told to stop
while (1) {
my $answer = prompt 'Enter an ISBN (enter "q" to exit): ';
if ( $answer eq "q" ) {
if ( defined($file) ) {
close $csv or croak "Couldn't close $csv because: $!";
}
exit;
}
# Get a request object
my $response = $ua->search( isbn => $answer );
if ( $response->is_success() ) {
chomp($answer); #ensure the ISBN doesn't have a newline
#print $response->as_string(), "\n";
for my $prop ( $response->properties ) {
print "\n" . $answer . ", " .
$prop->title() . ", " . $prop->author() . ", "
. $prop->ListPrice() . ", "
. $prop->OurPrice() . ", "
. $prop->UsedPrice() . ", "
. $prop->Media() . "\n";
if ( defined($file) ) {
#and now to the file
print {$csv} $answer . ", "
. $prop->title() . ", "
. $prop->author() . ", "
. $prop->ListPrice() . ", "
. $prop->OurPrice() . ", "
. $prop->UsedPrice() . ", "
. $prop->Media() . "\n"
or croak "Couldn't write to $csv because: $!";
}
}
}
else {
print "Error: ", $response->message(), "\n";
}
} #end of while loop
__END__
=head1 NAME
AmazonPriceChecker - script to take an ISBN at the command line, return information,
optionally to a CSV file.
Return's ISBN, Title, Author, List Price, Amazon's Price and the Used Price, e.g.;
Deadline (Newsflesh Trilogy), Mira Grant, £7.99, £4.69, £3.83,
The script will continue looping round asking for an ISBN untill you enter 'q' to quit.
You will need your personal amazon developer's token (can be obtained from http://amazon.com/soap).
<b> Note</b> The script expect to find a file called ~/.amazon_login and will fail if it can't.
It's a YAML file and follows the following format;
---
token: my_amazon_token
secret_key: my_amazon_key
associate_tag: tag
=head1 USAGE
AmazonPriceChecker.pl [-f]
=head1 OPTIONS
=over
=item -f[ile] [=] <file>
Specify file to write the output to [default: AmazonPriceChecker.csv]
=for Euclid:
file.type: writable
file.default: 'AmazonPriceChecker.csv'
=item --version
=item --usage
=item --help
=item --man
Print the usual program information
=back
=begin remainder of documentation here. . .