Created
November 13, 2015 02:14
-
-
Save mash/c7fecc1840ecf32b310f to your computer and use it in GitHub Desktop.
nodejs scripts to access Amazon market place API and fetch stock and sales
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require("request") | |
, crypto = require("crypto") | |
, querystring = require("querystring") | |
, assert = require("assert") | |
, l = require("nlogger").logger(module) | |
, parseString = require("xml2js").parseString | |
; | |
function Client( options ) { | |
var requiredKeys = [ | |
"AWSAccessKeyId", | |
"SellerId", | |
"Secret" | |
]; | |
for (var i=0,len=requiredKeys.length; i<len; i++) { | |
var key = requiredKeys[ i ]; | |
this[ key ] = options[ key ]; | |
assert( this[ key ], "options[\""+key+"\"] required" ); | |
} | |
} | |
function hmacSHA256 (method, domain, path, secret, payload) { | |
var hmac = crypto.createHmac("sha256", secret); | |
hmac.update(method + "\n"); | |
hmac.update(domain + "\n"); | |
hmac.update(path + "\n"); | |
var keys = Object.keys(payload).sort(); | |
var qs = ''; | |
for (var i=0,len=keys.length; i<len; i++) { | |
qs += keys[ i ] + "=" + querystring.escape( payload[ keys[i] ] ); | |
if (i !== (len-1)) { | |
qs += "&"; | |
} | |
} | |
hmac.update( qs ); | |
return hmac.digest('base64'); | |
} | |
// http://docs.developer.amazonservices.com/en_US/orders/2013-09-01/Orders_ListOrders.html | |
Client.prototype.ListOrders = function (cond, callback) { | |
this.invoke( "POST", | |
"ListOrders", | |
"2013-09-01", | |
"/Orders/2013-09-01", | |
cond, | |
function (responseJSON) { | |
var orders = responseJSON.ListOrdersResponse.ListOrdersResult[0].Orders[0].Order | |
, items = 0 | |
, amount = 0; | |
if (! orders) { | |
return { | |
orders : 0, | |
items : 0, | |
amount : 0 | |
}; | |
} | |
for (var i=0,len=orders.length; i<len; i++) { | |
var order = orders[ i ]; | |
items += parseInt(order.NumberOfItemsUnshipped[0], 10) + | |
parseInt(order.NumberOfItemsShipped[0], 10); | |
amount += (order.OrderTotal ? parseInt(order.OrderTotal[0].Amount[0], 10) | |
: 0); | |
} | |
return { | |
orders : orders.length, | |
items : items, | |
amount : amount | |
}; | |
}, | |
callback ); | |
}; | |
// http://docs.developer.amazonservices.com/en_US/fba_inventory/FBAInventory_ListInventorySupply.html | |
Client.prototype.ListInventorySupply = function(cond, callback) { | |
this.invoke( "GET", | |
"ListInventorySupply", | |
"2010-10-01", | |
"/FulfillmentInventory/2010-10-01", | |
cond, | |
function (responseJSON) { | |
var member = responseJSON.ListInventorySupplyResponse.ListInventorySupplyResult[0].InventorySupplyList[0].member[0]; | |
return { | |
InStockSupplyQuantity : parseInt(member.InStockSupplyQuantity[0], 10), | |
TotalSupplyQuantity : parseInt(member.TotalSupplyQuantity[0], 10), | |
}; | |
}, | |
callback ); | |
}; | |
Client.prototype.invoke = function (method, action, version, path, cond, parser, callback) { | |
var timestamp = (new Date()).toISOString(); | |
var payload = { | |
"Action" : action, | |
"AWSAccessKeyId" : this[ "AWSAccessKeyId" ], | |
"SellerId" : this[ "SellerId" ], | |
"SignatureMethod" : "HmacSHA256", | |
"SignatureVersion" : "2", | |
"Timestamp" : timestamp, | |
"Version" : version | |
}; | |
for (var k in cond) { | |
payload[ k ] = cond[ k ]; | |
} | |
var signature = hmacSHA256( method.toUpperCase(), | |
"mws.amazonservices.jp", | |
path, | |
this.Secret, | |
payload ); | |
payload.Signature = signature; | |
request[ method.toLowerCase() ]({ | |
url : "https://mws.amazonservices.jp" + path, | |
qs : payload | |
}, function (err,response,body) { | |
if (err || (response.statusCode !== 200)) { | |
l.error( "err, statusCode, res.headers, body: ", err, response.statusCode, response.headers, body ); | |
callback( err, 0 ); | |
return; | |
} | |
parseString( body, function (err, result) { | |
if (err) { | |
callback( err, 0 ); | |
return; | |
} | |
callback( null, parser(result) ); | |
}); | |
}); | |
}; | |
module.exports = Client; | |
// var client = new Client({ | |
// "AWSAccessKeyId" : "xxxxxxxxxxxxxxxxxx", | |
// "SellerId" : "xxxxxxxxxxxxxx", | |
// "Secret" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
// }); | |
// client.ListOrders({ | |
// "MarketplaceId.Id.1" : "xxxxxxxxxxxxxxx", | |
// "CreatedAfter" : "2014-03-31T15:00:00Z", | |
// }, function(err, orders) { | |
// if (err) { | |
// l.error( err ); | |
// } | |
// l.info( "orders: ", orders ); | |
// }); | |
// client.ListInventorySupply({ | |
// "SellerSkus.member.1" : "IRKit-001", | |
// "ResponseGroup" : "Basic", | |
// }, function(err, inventory) { | |
// if (err) { | |
// l.error( err ); | |
// } | |
// l.info( "inventory: ", inventory ); | |
// }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Amazon = require("amazon-mws") | |
, async = require("async") | |
, _ = require("underscore") | |
, moment = require("moment") | |
; | |
var client = new Amazon({ | |
"AWSAccessKeyId" : "xxxxxxxxxxxxxxxxxxxxxxxx", | |
"SellerId" : "xxxxxxxxxxxxx", | |
"Secret" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
}); | |
async.parallel([ | |
function (callback) { | |
client.ListInventorySupply({ | |
"SellerSkus.member.1" : "IRKit-001", | |
"ResponseGroup" : "Basic" | |
}, function(err, inventory) { | |
if (err) { | |
callback( err ); | |
return; | |
} | |
callback( null, inventory.InStockSupplyQuantity ); | |
}); | |
}, | |
function (callback) { | |
client.ListOrders({ | |
"MarketplaceId.Id.1" : "xxxxxxxxxxxxxx", | |
"CreatedAfter" : moment().subtract("hours",1).toISOString() | |
}, function(err, orders) { | |
if (err) { | |
callback( err ); | |
return; | |
} | |
callback( null, [ orders.orders, orders.amount ] ); | |
}); | |
}, | |
], function (err,results) { | |
if (err) { | |
console.error( err ); | |
return; | |
} | |
// amazon.stock,amazon.orders,amazon.amount | |
console.log( _.flatten(results).join("\t") ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment