Skip to content

Instantly share code, notes, and snippets.

@asonas
Forked from hatone/category1.yml
Created October 21, 2012 16:05
Show Gist options
  • Select an option

  • Save asonas/0acb82883dcf0a8bbf4f to your computer and use it in GitHub Desktop.

Select an option

Save asonas/0acb82883dcf0a8bbf4f to your computer and use it in GitHub Desktop.
アレとコレが一致するか?
- "Fantaisieantaisie Impromptu"
- "Quasi una Fantasia"
- "Klaviersonate Nr.11 A‐dur"
require "net/http"
require "uri"
require "json"
require "pp"
require "yaml"
require "cgi"
GOOGLE_KEY = ""
composer_list = YAML.load_file("composers.yml")
music_list = YAML.load_file("musics.yml")
api_path = "http://ajax.googleapis.com/ajax/services/search/web"
api = URI.parse(api_path)
headers = { :Referer => "http://hatone.info/" }
composer_list.each do |composer|
max_count = 0
category = ""
music_list.each do |music|
query = CGI.escape("#{composer} is similar to #{music}")
data = "?v=1.0&key=#{GOOGLE_KEY}&q=#{query}"
apicall = Net::HTTP.new(api.host)
response = apicall.get2(api.path + data, headers)
data = JSON.parse(response.body)
return exit if data['responseStatus'] == 403
# ここには既に数字入っているのでは?
count = data['responseData']['cursor']['estimatedResultCount']
count = count.to_i
# result_count = ...
# if max_count < result_count
if max_count < count
max_count = count
category = music
end
end
pp composer + " is " + category
end
@asonas

asonas commented Oct 21, 2012

Copy link
Copy Markdown
Author
  • rubygemsはいらないと思うので削除
  • シングルクォートとダブルクォートが混じってたので統一
  • to_sが頻発してたけど多分いらないと思ったので削除
  • やっていることはすごいシンプルなのでファイル名と変数名と適宜変更(リーダブルコードより)
  • return exit if data['responseStatus'] == 403 と、一行で書く(リーダブルコードより)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment