Last active
August 29, 2015 14:06
-
-
Save inobo55/a39a79c0d693cf9dad5a to your computer and use it in GitHub Desktop.
Rubyで楽天APIとホットワードAPIを使ってみた
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
# encoding: utf-8 | |
require "open-uri" | |
require "nokogiri" | |
class Korekae | |
def index | |
url = "http://kizasi.jp/kizapi.py?type=rank" | |
xml = open(url).read | |
doc = Nokogiri::XML(xml) | |
trend_words = [] | |
companies = [] | |
doc.search("item").search("title").each do |item| | |
shopNames = rakutenAPI(item.inner_text) | |
if shopNames.empty? | |
next | |
end | |
companies[0,0] = shopNames # 配列に配列要素を追加 | |
trend_words.push(item.inner_text) | |
end | |
return {:company => companies,:trend => trend_words} | |
end | |
#トレンド単語から株を買うべきお店を提示 | |
def rakutenAPI(keyword) | |
sleep(1.5) | |
# applicationId = YOUR_APP_ID_HERE | |
base_url = "https://app.rakuten.co.jp/services/api/IchibaItem/Search/20140222?applicationId=YOUR_APP_ID_HERE" | |
param = "&keyword="+URI.encode(keyword)+"&format=xml" | |
url = base_url + param | |
xml = open(url).read | |
doc = Nokogiri::XML(xml) | |
shopNames = [] | |
doc.search("shopName").each do |shop| | |
shopNames.push(shop.inner_text) | |
end | |
return shopNames | |
end | |
end | |
# 表示 | |
kae = Korekae.new | |
hash = kae.index | |
trend_words = hash[:trend] | |
companies = hash[:company] | |
trend_words.each do |word| | |
p "・"+word | |
end | |
companies.each do |company| | |
p "@"+company | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment