Skip to content

Instantly share code, notes, and snippets.

@mashiro
Created November 19, 2013 06:11
Show Gist options
  • Save mashiro/7541034 to your computer and use it in GitHub Desktop.
Save mashiro/7541034 to your computer and use it in GitHub Desktop.
# coding: utf-8
require 'nokogiri'
require 'cgi'
class GoogleAnswer
include Donkun::Plugin
set :help, <<-EOS
age|birthday <query>
指定した人物の生年月日を表示します
born <query>
指定した人物の出身地を表示します
q[a]|c <query>
Google 先生に質問します
EOS
match /(?:age|birthday)\s+(.+)/, :method => :age
def age(m, query)
m.target.notice search("#{query} 生年月日")
end
match /(?:born)\s+(.+)/, :method => :born
def born(m, query)
m.target.notice search("#{query} 生まれ")
end
match /(?:qa?|c)\s+(.+)/, :method => :qa
def qa(m, query)
m.target.notice search(query)
end
def search(query)
res = get 'http://www.google.co.jp/search', :params => params.merge({:q => query})
doc = Nokogiri::HTML res
res = if box = doc.at_css('#cwmcwd')
"#{box.at_css('#cwles').text} #{box.at_css('#cwos').text}"
elsif box = doc.at_css('.leg_calc')
"#{box.at_css('.vk_sh').text} #{box.at_css('.vk_ans').text}"
else
box = doc.at_css('.vk_bk')
"#{box.at_css('.vk_ans').text} - #{box.at_css('.vk_sh').text}"
end
sanitize res
rescue
OWATA
end
def params
config[:params] || {}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment