-
Notifications
You must be signed in to change notification settings - Fork 1
/
amazon_search.rb
66 lines (57 loc) · 1.85 KB
/
amazon_search.rb
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
# -*- coding: utf-8 -*-
require 'rubygems'
require 'amazon/aws'
require 'amazon/aws/search'
ENV['AMAZONRCDIR'] = '' # PATH
ENV['AMAZONRCFILE'] = 'amazonrc'
#
# It is necessary to create a amazonrc
#
#[global]
# key_id = {AWS Access Key ID}
# secret_key_id = {AWS Secret Access Key}
# associate = {Amazon Associate ID}
# locale = jp
# cache = true
# cache_dir = {Cache Directory}
#
class AmazonSearch < SkypeBot::Plugin
def initialize(*args)
super
@prefix = @config['prefix'] || 'が欲しい'
@limit = @config['limit'] || 3
end
def on_privmsg(prefix, channel, message)
case message
when /^(.+)#{@prefix}$/
search($1).each do |item|
sendMessage(channel, item)
end
end
end
private
def search(keyword)
begin
request = Amazon::AWS::Search::Request.new
query = Amazon::AWS::ItemSearch.new(:All, { :Keywords => keyword })
response = Amazon::AWS::ResponseGroup.new(:Small, :OfferSummary)
datas = []
results = request.search(query, response).item_search_response.items.item
results[0...@limit].each do |item|
title = item.item_attributes.title.to_s
uri = item.detail_page_url.to_s
prices = item.offer_summary
price = case
when prices.lowest_new_price : prices.lowest_new_price.formatted_price
when prices.lowest_used_price : "[中古] #{prices.lowest_used_price.formatted_price}"
when prices.lowest_collectible_price : "[コレクター] #{prices.lowest_collectible_price.formatted_price}"
end
datas << "これかな? [Amazon.co.jp] #{title} #{price} (#{URI.short_uri(uri)})"
end
return datas
rescue Exception => e
p e.inspect
return 'うーん、なにそれ?みつからないよー。'
end
end
end