Skip to content

Instantly share code, notes, and snippets.

@hackugyo
Last active May 26, 2018 13:14
Show Gist options
  • Save hackugyo/6cb9a5eda19df6b4cdb6 to your computer and use it in GitHub Desktop.
Save hackugyo/6cb9a5eda19df6b4cdb6 to your computer and use it in GitHub Desktop.
Amazon Product Advertising APIを使った、タイトルを食わせるとISBN (もしくはasin)を返すHubot。AWS関係のID/SECRETはここで登録して取得(電話で認証するので電話番号が必要) https://affiliate-program.amazon.com/gp/flex/advertising/api/sign-in-jp.html $ npm install amazon-product-api --saveしてからhubotのscripts配下に置いたら使えるはず。herokuの環境設定は $heroku config:set AMAZON_AWS_SECRET=YOUR_SECRET --app your-awesome-app-nam…
module.exports = (robot) ->
robot.hear /^get_isbn (.*)/i, (msg) ->
keyword = "#{msg.match[1]}"
amazon = require('amazon-product-api')
client = amazon.createClient({
awsId: process.env.AMAZON_AWS_ID,
awsSecret: process.env.AMAZON_AWS_SECRET,
awsTag: process.env.AMAZON_AWS_TAG,
})
client.itemSearch {
keywords: keyword,
searchIndex: 'Books',
responseGroup: 'ItemAttributes',
domain: 'ecs.amazonaws.jp'
}, (err, results) ->
if err
msg.send 'ERROR' # 検索結果0件のときはエラーになる
return
message = ''
for result,i in results
if (i >= 1)
message += "ほか #{results.length - 1}件"
message += "以上" if results.length >= 10
break
title = result.ItemAttributes[0].Title
isbn13 = result.ItemAttributes[0].EAN?[0] # EANの存在確認
isbn13 = ('ASIN ' + result.ASIN?[0]) unless isbn13?
url = result.DetailPageURL[0]
message += (isbn13 + ' ' + title + ' ' + url + '\n')
msg.send message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment