-
Notifications
You must be signed in to change notification settings - Fork 0
/
azorder.pl
executable file
·302 lines (273 loc) · 11.6 KB
/
azorder.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/perl
# *********************************************************************
# Copyright Notice.
# *********************************************************************
#
# Copyright (C) 2011 Charles Ledvina.
#
# This file is part of Amazon to MARC Converter.
#
# Amazon to MARC Converter is free software: you can redistribute it
# and/or modify it under the terms of the GNU Affero General Public
# License as published by the Free Software Foundation, either version
# 3 of the License, or (at your option) any later version.
#
# Amazon to MARC Converter is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with Amazon to MARC Converter. If not, see
# <http://www.gnu.org/licenses/>.
#
# Charles Ledvina
# 5 Whaler Court
# Third Lake, IL 60030-2620
# USA
# http://www.libcat.org
open INIFILE, 'az2marc.ini';
while (<INIFILE>) {
chomp;
my ($k, $v) = split / = |=/;
$INI{$k} = $v
}
use LWP::Simple;
require 'clweb.pm';
$ctime = time;
$qs = $ENV{QUERY_STRING} || <STDIN>;
%q = qstring($qs);
$q{CT} ||= 'com';
&cookie;
if ($q{IAM} eq 'chopac' and $COOKS{iam} ne 'chopac') {
print "Set-Cookie: iam=chopac;\n";
print "Set-Cookie: ref=$ENV{HTTP_REFERER}\n";
}
print "Content-Type: text/html\n\n";
############################################ Scripts and CSS ##############################################################################
print <<"__END__SCRIPT__";
<style>
body { font-family: arial; font-size: 12 }
a:link { color: 006699; text-decoration: none }
a:visited { color: 006699; text-decoration: none }
.search { background-color: beige; padding: 25px; width: 90%; font-size: 14; text-align: center; border: solid; border-width: 1px }
.rec { background-color: eeeeee; color: 000000; padding: 25px; width: 90%; border: solid; border-width: 1px; margin-top: 5px }
.rec0 { background-color: eeeeee; color: 000000; padding: 25px; width: 90%; border: solid; border-width: 1px; margin-top: 5px }
.rec1 { background-color: 99FFAA; color: 000000; padding: 25px; width: 90%; border: solid; border-width: 1px; margin-top: 5px }
.rec2 { background-color: pink; color: 000000; padding: 25px; width: 90%; border: solid; border-width: 1px; margin-top: 5px }
.statbar { background-color: ffffff; color: 999999; padding: 0px; width: 50%; font-size: 12}
.bar { background-color: ffffff; color: 999999; padding: 0px; width: 90%; font-size: 10}
.button:hover { background-color: dddddd }
.button { margin: 1px; border: outset; border-width: 2px; background-color: eeeeee; padding: 2px }
.mb { font-size: 10px; margin: 1px; border: outset; border-width: 2px; background-color: eeeeee; padding: 1px }
.mf { font-size: 10px; border: solid; border-width: 1px; border-color: 006699; text-align: center; padding: 10px; background-color: dfefff }
.status { border: solid; border-width: 1px; width: 30%; color: black; background-color: dfefff; margin-top: 40px; padding: 10px }
.warn { color: red; font-weight: bold }
.ti { font-size: 16px }
textarea { height: 20px }
img { border-width: 0px }
.list { border-bottom: dashed; border-width: 1px; width: 90%; height: 20%; font-family: arial; font-size: 14 }
.til { padding: 15px; position: absolute; display: none; background-color: BBFFCC; opacity: .9; border: solid; border-width: 1px }
.sv { padding: 15px; position: fixed; left: 300px; top: 300px; display: none; background-color: BBFFCC; opacity: .9; border: solid; border-width: 1px }
.close { color: 999999; border: solid; border-width: 1px; padding: 5px}
</style>
<script>
var hR;
if (window.XMLHttpRequest) {
hR = new XMLHttpRequest();
}
else {
hR = new ActiveXObject("Msxml2.XMLHTTP");
}
// function cook() {
//if (document.cookie == "") {
//document.cookie = "$ctime-$$"
//}
//}
function viewButton() {
if (document.sform.isbn.value.match(/[a-wyz]/i)) {
document.getElementById("default").value = "Books";
document.getElementById("other").style.visibility = "visible"
}
else {
document.getElementById("default").value = "Blended";
document.getElementById("other").style.visibility = "hidden"
}
}
function ly(addr) {
hR.onreadystatechange = function() {
var s = document.getElementById('tlist');
s.style.display = "block";
if (hR.readyState == 4) {
s.innerHTML = hR.responseText
}
}
hR.open('GET', addr, true);
hR.send(null);
}
function sv(asin) {
hR.onreadystatechange = function() {
var s = document.getElementById('sv');
s.style.display = "block";
if (hR.readyState == 4) {
s.innerHTML = hR.responseText;
window.setTimeout('lo("sv")', 2000)
}
}
hR.open('GET','az2marc.pl?ct=$q{CT}&rda=$q{RDA}&kw=' + asin + '&qx=2' , true);
hR.send(null);
}
function vw(asin) {
location.href = 'az2marc.pl?iam=$q{IAM}&ct=$q{CT}&rda=$q{RDA}&kw=' + asin
}
function ex(asin) {
location.href = 'az2marc.pl?iam=$q{IAM}&ct=$q{CT}&rda=$q{RDA}&kw=' + asin + '&qx=1'
}
function buy(asin) {
location.href = 'http://www.amazon.$q{CT}/exec/obidos/ASIN/' + asin
}
//buy-from-tan.gif
function lo(myDiv) {
var s = document.getElementById(myDiv);
s.style.display="none";
}
</script>
<body onLoad='viewButton(); document.sform.isbn.select(); cook()'>
__END__SCRIPT__
############################################ Program Control ###########################################################################
if ($q{ISBN}) { &amazon }
else { &sform }
############################################ Search Form ###############################################################################
sub sform {
$CSL{$q{CT}} = 'selected';
print "<center><table class='search'><tr><td colspan='2'><form name='sform' action='$ENV{SCRIPT_NAME}'>";
print "Enter Keywords, ISBN, EAN or UPC: ";
print " <input type='text' name='isbn' value='$q{ISBN}' onKeyUp='viewButton()'>";
print "<span id='go' style='display:none'> <input type='submit' id='default' class='button' name='type' value='Blended'></span>";
print "<span id='other' style='visibility:hidden'> <input type='submit' class='button' name='type' value='Books'>";
print " <input type='submit' class='button' name='type' value='Video'>";
print " <input type='submit' class='button' name='type' value='Music'>";
print " <input type='submit' class='button' name='type' value='VideoGames'>";
print " <input type='submit' class='button' name='type' value='KindleStore'></span>";
### onMouseOver='ly(\"$ENV{SCRIPT_NAME}?lv=1\")' onMouseOut='lo(\"tlist\")' insert in to export button ###
print "<tr><td align='left'><input type='button' class='mb' value='Export Saved Records' onClick='location.href=\"az2marc.pl?save=batch\"'>";
if ($q{RDA}) { $rchk = ' checked' }
print "<td align='right'><input type='checkbox' name='rda' value='1'$rchk>RDA Format | Choose a Country: <select name='ct'>";
print "<option value='com' $CSL{com}>United States";
print "<option value='ca' $CSL{ca}>Canada";
print "<option value='fr' $CSL{fr}>France";
print "<option value='de' $CSL{de}>Germany";
print "<option value='co.uk' $CSL{'co.uk'}>United Kingdom";
print "</select>";
print "<input type='hidden' value='$q{IAM}' name='iam'>";
print "<div class='til' id='tlist'></div>";
print "</table></form></center>"
}
########################################################## Display a single brief record ##############################################################
sub display {
print "<table class='list'>";
print "<tr><td valign='center' align='center' width='20%'><img src='http://images.amazon.com/images/P/$AZ{asin}.01._SL110_SCTZZZZZZZ_.jpg'>";
print "<td valign='top'><a class='ti' href='javascript:buy(\"$AZ{asin}\")'><b>$AZ{title}</b></a>";
print "<br>$AZ{publisher}, $AZ{date}.<br>$AZ{price}<br><img src='http://chopac.org/images/buy-from-tan.gif' onClick='buy(\"$AZ{asin}\")'>";
print "<p><a href='azrev.pl?q=$AZ{asin}'>Customer Reviews</a>";
print "<td align='right' width='20%'>";
print "<div class='mf'>Marc Functions<br>";
print "<input type='button' class='mb' value='View' onClick='vw(\"$AZ{asin}\")'>";
print "<input type='button' class='mb' value='Export' onClick='ex(\"$AZ{asin}\")'>";
print "<input type='button' class='mb' value='Save' onClick='sv(\"$AZ{asin}\")'>";
print "</div></table>";
print "<div id='sv' class='sv'></div>";
}
####################################################### Pages through the results ######################################################################
sub pager {
my $np = $Page + 1;
my $pp = $Page - 1;
$q{ISBN} =~ s/ +/+/g;
print "<td align='right'>";
if ($Page > 1) { print " <a href='$ENV{SCRIPT_NAME}?ct=$q{CT}&isbn=$q{ISBN}&type=$q{TYPE}&pg=$pp'>< Prev</a>" }
else { print "< Prev" }
if ($Page < $Pages) { print " <a href='$ENV{SCRIPT_NAME}?ct=$q{CT}&isbn=$q{ISBN}&type=$q{TYPE}&pg=$np'>Next ></a>" }
else { print " Next >" }
print "</center></table>";
}
##################################################### Connects to Amazon and searches and gets results #################################################
sub amazon {
&sform;
$q{TYPE} ||= 'Blended';
if ($q{TYPE} eq 'Blended' && $q{ISBN} =~ /[a-wyz]/) { $q{TYPE} = "Books" }
$q{PG} ||= 1;
##### This section was added to accomadate the new signed request requirements ################################################
use RequestSignatureHelper;
my $endPoint = "ecs.amazonaws.$q{CT}";
my $resGroup = "ItemAttributes,Tracks";
my $helper = new RequestSignatureHelper (
+RequestSignatureHelper::kAWSAccessKeyId => $INI{accesskey},
+RequestSignatureHelper::kAWSSecretKey => $INI{secretkey},
+RequestSignatureHelper::kEndPoint => $endPoint,
);
my $request = {
Service => 'AWSECommerceService',
AssociateTag => $INI{associate},
Operation => 'ItemSearch',
Keywords => $q{ISBN},
ItemPage => $q{PG},
SearchIndex => $q{TYPE},
Version => '2009-03-31',
ResponseGroup => 'ItemAttributes',
};
my $signedRequest = $helper->sign($request);
my $queryString = $helper->canonicalize($signedRequest);
my $ad = "http://" . $endPoint . "/onca/xml?" . $queryString;
################################################################################################################################
my $xml = get($ad);
if ($xml =~ s/^(.+?)(<item>)/$2/i) {
$leader = $1;
if ($leader =~ /<TotalResults>(.+?)<\/TotalResults>/i) { $Tot = $1 }
if ($leader =~ /<TotalPages>(.+?)<\/TotalPages>/i) { $Pages = $1 }
if ($leader =~ /<ItemPage>(.+?)<\/ItemPage>/i) { $Page = $1 }
print "<center><table class='bar'><tr><td>$Tot Records Found (Page $Page of $Pages)";
&pager;
}
else {
print "<center><table class='status'><tr><td>No Records Found!<br>";
exit;
}
while ($xml =~ s/<item>(.+?)<\/item>//i) {
my $x = $1;
%AZ = ();
if ($x =~ /<asin>(.+?)<\/asin>/i) {
$AZ{asin} = $1
}
while ($x =~ s/<isbn>(.+?)<\/isbn>//i) {
$AZ{isbn} = $1
}
if ($x =~ /<listprice>.+?<formattedprice>(.+?)<\/formattedprice>/i) {
$AZ{price} = $1
}
if ($x =~ /<author>(.+?)<\/author>/i) {
$AZ{author} = $1
}
if ($x =~ /<artist>(.+?)<\/artist>/i) {
$AZ{author} = $1
}
if ($x =~ /<title>(.+?)<\/title>/i) {
$AZ{title} = $1;
$AZ{title} .= " / by $AZ{author}" if $AZ{author};
}
if ($x =~ /<publisher>(.+?)<\/publisher>/i) {
$AZ{publisher} = $1;
}
if ($x =~ /<releasedate>(.+?)<\/releasedate>/i) {
$AZ{date} = $1;
$AZ{date} =~ s/^(....).+/$1/;
}
elsif ($x =~ /<publicationdate>(.+?)<\/publicationdate>/i) {
$AZ{date} = $1;
$AZ{date} =~ s/^(....).+/$1/;
}
&display;
}
print "<center><table class='bar'><tr><td>$Tot Records Found (Page $Page of $Pages)";
&pager
}