-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws.js
134 lines (107 loc) · 4.57 KB
/
aws.js
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
// Copyright (c) 2012 by Charles Engelke.
var AWS = function(accessKeyId, secretAccessKey, associateTag){
var self = this;
self.itemLookup = function(itemId, onSuccess, onError, idType){
var params = [];
idType = idType || "ISBN";
params.push({name: "Service", value: "AWSECommerceService"});
params.push({name: "AWSAccessKeyId", value: accessKeyId});
params.push({name: "AssociateTag", value: associateTag});
params.push({name: "Operation", value: "ItemLookup"});
params.push({name: "Timestamp", value: formattedTimestamp()});
params.push({name: "ItemId", value: itemId});
params.push({name: "IdType", value: idType});
params.push({name: "SearchIndex", value: "Books"});
params.push({name: "ResponseGroup", value: "ItemAttributes,Offers"});
var signature = computeSignature(params, secretAccessKey);
params.push({name: "Signature", value: signature});
var queryString = createQueryString(params);
var url = "https://webservices.amazon.com/onca/xml?"+queryString;
jQuery.ajax({
type : "GET",
url: url,
data: null,
success: extractAndReturnResult,
error: returnErrorMessage
});
function extractAndReturnResult(data, status, xhr){
var results = [];
var result, i, item;
var items = $(data).find("Items Item");
if (items === null) {
onError('Amazon returned a result in an unsupported format');
} else {
for(i=0; i<items.length; i++){
item = items[i];
result = {
asin: getValueFrom(item, "ASIN"),
isbn: getValueFrom(item, "ISBN"),
ean: getValueFrom(item, "EAN"),
author: getValueFrom(item, "ItemAttributes Author") || 'Anonymous',
title: getValueFrom(item, "ItemAttributes Title") || 'Untitled',
releaseDate: getValueFrom(item, "ItemAttributes PublicationDate"),
listPrice: getValueFrom(item, "ItemAttributes ListPrice FormattedPrice") || 'an unknown price',
availability: getValueFrom(item, "Offers Offer OfferListing Availability") || 'Availability unknown',
amazonPrice: getValueFrom(item, "Offers Offer OfferListing Price FormattedPrice") || 'missing price',
url: getValueFrom(item, "DetailPageURL") || ''
};
if (result.asin){
results.push(result);
}
}
}
onSuccess(results);
}
function returnErrorMessage(xhr, status, error){
onError('Ajax request failed with status message '+status);
}
}
function getValueFrom(item, selector){
var elements = $(item).find(selector);
if ((elements !== null) && (elements.length > 0)) {
return elements[0].textContent;
} else {
return null;
}
}
function formattedTimestamp(){
var now = new Date();
var year = now.getUTCFullYear();
var month = now.getUTCMonth()+1; // otherwise gives 0..11 instead of 1..12
if (month < 10) { month = '0' + month; } // leading 0 if needed
var day = now.getUTCDate();
if (day < 10) { day = '0' + day; }
var hour = now.getUTCHours();
if (hour < 10) { hour = '0' + hour; }
var minute = now.getUTCMinutes();
if (minute < 10) { minute = '0' + minute; }
var second = now.getUTCSeconds();
if (second < 10) { second = '0' + second; }
return year+'-'+month+'-'+day+'T'+hour+':'+minute+':'+second+'Z';
}
function createQueryString(params){
var queryPart = [];
var i;
params.sort(byNameField);
for(i=0; i<params.length; i++){
queryPart.push(encodeURIComponent(params[i].name) +
'=' +
encodeURIComponent(params[i].value));
}
return queryPart.join("&");
function byNameField(a, b){
if (a.name < b.name) { return -1; }
if (a.name > b.name) { return 1; }
return 0;
}
}
function computeSignature(params, secretAccessKey){
var stringToSign = 'GET\nwebservices.amazon.com\n/onca/xml\n' +
createQueryString(params);
var key = sjcl.codec.utf8String.toBits(secretAccessKey);
var hmac = new sjcl.misc.hmac(key, sjcl.hash.sha256);
var signature = hmac.encrypt(stringToSign);
signature = sjcl.codec.base64.fromBits(signature);
return signature;
}
}