Skip to content

Instantly share code, notes, and snippets.

@qingwang
Created May 31, 2014 16:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qingwang/146063b9d28f31ae4fcd to your computer and use it in GitHub Desktop.
Save qingwang/146063b9d28f31ae4fcd to your computer and use it in GitHub Desktop.
# Install these gems first:
# gem install 'google_custom_search_api' 'wolfram-alpha' 'imdb' 'wunderground' 'whois' 'sinatra'
require 'google_custom_search_api'
require 'wolfram-alpha'
require 'imdb'
require 'wunderground'
require 'whois'
require 'json'
require 'sinatra'
require 'sinatra/json'
set :bind, '0.0.0.0'
#### insert your API keys ####
WUNDERGROUND_API_KEY = ""
WOLFRAMALPHA_API_KEY = ""
GOOGLE_API_KEY = ""
GOOGLE_SEARCH_CX = ""
#### API keys section finished ####
SEPARATOR = "\n----------------------------------------\n"
TEST_RESPONSES = [
"Test Succeeded!",
"Test Failed!",
"大师说所有的测试都要失败",
"WKSOGAYLOL"
]
SLEEP_RESPONSES = [
"!too early",
"yeah right",
"你只是上床玩手机而已",
"night night",
"记得起来上厕所"
]
WHOIS_PROPERTIES = {
:standard => ["domain", "status",
"created_on", "expires_on"],
:extend => ["registrar", "admin_contacts",
"nameservers"]
}
post '/bot' do
def get_result(params)
trigger = params[:trigger_word][1..-1]
arg = params[:text].split(" ")[1..-1].join(" ")
begin
eval("process_#{trigger}(arg)")
rescue Exception
"I have no idea (if you think I really should know please contact my master to fix me)"
end
end
def process_test(arg)
TEST_RESPONSES[rand(TEST_RESPONSES.size)]
end
def process_sleep(arg)
SLEEP_RESPONSES[rand(SLEEP_RESPONSES.size)]
end
def process_slap(arg)
"bt slaps @#{arg} around a bit with a large trout"
end
def process_g(arg)
raw_result = GoogleCustomSearchApi.search(arg)
formatted_result= [SEPARATOR]
p raw_result
raw_result["items"][0..4].each do |item|
formatted_result << "*#{item.title}* : #{item.link}\n"
formatted_result << item.snippet
formatted_result << SEPARATOR
end
formatted_result.join("\n")
end
def process_weather(arg)
w_api ||= Wunderground.new(WUNDERGROUND_API_KEY)
raw_result = w_api.forecast_for(arg)
formatted_result = parse_weather(raw_result, arg)
formatted_result.join("\n")
end
def parse_weather(raw_result, arg)
result = [arg]
(0..3).each do |i|
this_day = raw_result["forecast"]["txt_forecast"]["forecastday"][i]
result << "*#{this_day["title"]}* | #{this_day["fcttext_metric"]}"
end
result
end
def process_w(arg)
client ||= WolframAlpha::Client.new WOLFRAMALPHA_API_KEY, { "format" => "plaintext" }
response = client.query(arg)
results = response.find_all { |pod| !pod.title.nil? }
formatted_result = [SEPARATOR]
results.each do |result|
formatted_result << "*#{result.title}*\n"
result.subpods.each do |subpod|
formatted_result << subpod.plaintext
end
formatted_result << SEPARATOR
end
formatted_result.join("\n")
end
def process_imdb(arg)
movies = Imdb::Search.new(arg).movies[0..20]
formatted_result= [SEPARATOR]
movies.each do |i|
formatted_result << "*#{i.title}*\n#{i.rating} | #{i.genres}\n#{i.plot}" if !i.plot.nil? && (i.rating.to_i > 4)
end
formatted_result << "*ONLY LISTING ITEMS WITH RATING \> 4*"
formatted_result.join(SEPARATOR)
end
def process_whois(arg)
w ||= Whois::Client.new
domain_name = arg[1..arg.length-2].split("|")[1]
formatted_result = [SEPARATOR]
result = w.lookup(domain_name).parser
begin
WHOIS_PROPERTIES[:standard].each do |property|
formatted_result << "*#{property}*\n#{result.send(property)}"
end
WHOIS_PROPERTIES[:extend].each do |property|
extented_property = "*#{property}*\n"
result.send(property).each do |element|
extented_property += "#{element}\n"
end
formatted_result << extented_property
end
rescue Whois::AttributeNotImplemented
"N/A"
rescue Whois::AttributeNotSupported
"N/A"
end
formatted_result.join(SEPARATOR)
end
json :text => get_result(params)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment