Created
July 19, 2016 20:36
-
-
Save mattryles/bc412df91c893467f8b57bbaa05782af to your computer and use it in GitHub Desktop.
This file contains 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
'use strict'; | |
require('env2')('.env'); | |
const amazon = require('amazon-product-api'); | |
var client = amazon.createClient({ | |
awsId: "ID", | |
awsSecret: "SECRET", | |
awsTag: "TAG" | |
}); | |
exports.handler = function (event, context, callback) { | |
const barcode = event.params.querystring.barcode; | |
if(!barcode || isNaN(parseInt(barcode))) | |
return callback(null, []); | |
client.itemSearch({ | |
keywords: barcode, | |
searchIndex: 0 | |
}); | |
client.itemLookup({ | |
idType: 'EAN', | |
itemId: barcode, | |
responseGroup: 'ItemAttributes,Images', | |
domain: 'webservices.amazon.co.uk' | |
}).then(function(amazonResults) { | |
if(!amazonResults.length || amazonResults.length < 1) | |
return callback(null, []); | |
var items = amazonResults.filter(function(itemDetails) { | |
return itemDetails.ItemAttributes[0]; | |
}).map(function(itemDetails) { | |
var itemAttrs = itemDetails.ItemAttributes[0]; | |
return { | |
barcode : barcode, | |
source : "amazon", | |
dateFetched : Date.now(), | |
title : (itemAttrs.Title) ? itemAttrs.Title[0] : undefined, | |
productAmount : (itemAttrs.PackageQuantity) ? itemAttrs.PackageQuantity[0] : undefined, | |
image_url : (itemDetails.MediumImage) ? itemDetails.MediumImage[0].URL[0] : undefined, | |
price : (itemAttrs.ListPrice) ? itemAttrs.ListPrice[0].FormattedPrice[0] : undefined, | |
releaseDate : (itemAttrs.ReleaseDate) ? itemAttrs.ReleaseDate[0] : undefined | |
}; | |
}); | |
return callback(null, items); | |
}).catch(function(err) { | |
console.log(err); | |
return callback(err); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment