Skip to content

Instantly share code, notes, and snippets.

@yattom
Last active August 29, 2015 14:10
Show Gist options
  • Save yattom/e77dc2c4d73eb240bd59 to your computer and use it in GitHub Desktop.
Save yattom/e77dc2c4d73eb240bd59 to your computer and use it in GitHub Desktop.
An node script to get prices from Amazon (Japan). I threw away in the middle when I realized that prices for Kindle books are not available with Product Advertising API.
// An ugly quick hack to get prices from Amazon
// Using node-apac https://github.com/dmcquay/node-apac/
// and almost same as the example in its README
var util = require('util'),
OperationHelper = require('apac').OperationHelper;
var opHelper = new OperationHelper({
awsId: process.env.AWS_ID,
awsSecret: process.env.AWS_SECRET,
assocId: process.env.ASSOCIATE_ID,
endPoint: 'ecs.amazonaws.jp', // search on Amazon Japan
// xml2jsOptions: an extra, optional, parameter for if you want to pass additional options for the xml2js module. (see https://github.com/Leonidas-from-XIV/node-xml2js#options)
});
// execute(operation, params, callback)
// operation: select from http://docs.aws.amazon.com/AWSECommerceService/latest/DG/SummaryofA2SOperations.html
// params: parameters for operation (optional)
// callback(err, parsed, raw): callback function handling results. err = potential errors raised from xml2js.parseString() or http.request(). parsed = xml2js parsed response. raw = raw xml response.
opHelper.execute('ItemSearch', {
'SearchIndex': 'Books',
'Keywords': '翔泳社',
'ResponseGroup': 'ItemAttributes,Offers'
}, function(err, results) { // you can add a third parameter for the raw xml response, "results" here are currently parsed using xml2js
console.log(results);
if('ItemSearchErrorResponse' in results) {
console.log('Error: ');
console.log(results.ItemSearchErrorResponse.Error);
return;
}
// console.log(results.ItemSearchResponse.Items);
for(i = 0; i < results.ItemSearchResponse.Items[0].Item.length; i++) {
console.log('Item')
console.log(' ASIN: ' + results.ItemSearchResponse.Items[0].Item[i].ASIN);
console.log(' Title: ' + results.ItemSearchResponse.Items[0].Item[i].ItemAttributes[0].Title);
if(!('Offers' in results.ItemSearchResponse.Items[0].Item[i])) {
continue;
}
if(!('Offer' in results.ItemSearchResponse.Items[0].Item[i].Offers[0])) {
continue;
}
//console.log('>>>');
//console.log(results.ItemSearchResponse.Items[0].Item[i]);
//console.log('<<<');
console.log(' ' + results.ItemSearchResponse.Items[0].Item[i].Offers[0].TotalOffers + ' offers');
for(j = 0; j < results.ItemSearchResponse.Items[0].Item[i].Offers[0].Offer.length; j++) {
console.log(' ' + results.ItemSearchResponse.Items[0].Item[i].Offers[0].Offer[j].OfferListing[0].Price[0].FormattedPrice[0] + ' (' + results.ItemSearchResponse.Items[0].Item[i].Offers[0].Offer[j].OfferAttributes[0].Condition[0] + ')');
}
}
});
// The MIT License (MIT)
//
// Copyright (c) 2014 yattom / Tsutomu Yasui
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment