Skip to content

Instantly share code, notes, and snippets.

@justinvt
Created September 3, 2009 19:17
Show Gist options
  • Save justinvt/180478 to your computer and use it in GitHub Desktop.
Save justinvt/180478 to your computer and use it in GitHub Desktop.
require "rubygems"
require "net/http"
require "uri"
require "open-uri"
require "json"
require "nokogiri"
require "youtube_g"
query = "karel apppel"
GOOGLE_KEY = ""
# A quick method to do a get and parse the json that's returned
def api_get(url,data)
api_path = url
api = URI.parse(api_path)
headers = {'Referer' => 'http://www.example.com/'}
apicall = Net::HTTP.new(api.host)
full_path = URI.escape([api.path, data.to_a.map{|kv| kv.join("=")}.join("&")].join("?"))
response = apicall.get2(full_path, headers)
JSON.parse(response.body)
end
#search google for your query and if it makes a suggestion about spelling return the suggestion
def spell_check(query)
path = "http://www.google.com/search"
data = {:q => query, :hl => "en"}
full_path = URI.escape([path, data.to_a.map{|kv| kv.join("=")}.join("&")].join("?"))
suggestion = Nokogiri::HTML(open(full_path), nil, nil).css("a.spell")
suggestion.size == 0 ? query : suggestion[0].inner_html.gsub(/<[^<>]+>/,"")
end
# hash of google search results
def google_results(query)
data = {:v => "1.0", :q => query, :rsz=>"large"}
api_get("http://ajax.googleapis.com/ajax/services/search/web",data)["responseData"]["results"]
end
# is there a wikipedia article for the query?
def wikipedia_match?(query)
data = {:action=>"opensearch", :search=>query}
api_get("http://en.wikipedia.org/w/api.php",data)[0] == query
end
# returns the plaintext of the wikipedia article for query
def wikipedia_content(query)
#http://en.wikipedia.org/w/api.php?action=opensearch&search=india
wikipedia_url = google_results(query).map{|r| r["url"]}.select{|u| u=~ /http:\/\/[a-z]{1,4}\.wikipedia\.org/ }[0]
if wikipedia_url
article_content = Nokogiri::HTML(open(wikipedia_url), nil, nil).css("#bodyContent p")[0..-1]
readable = article_content.map{|p| p.inner_html.gsub(/<[^<>]+>/,"")}.reject{|p| p.length < 5}.join("\n\n")
end
end
# a hash of youtube results for query
def youtube_content(query)
client = YouTubeG::Client.new
client.videos_by(:query => query).videos.map(&:player_url)
end
spell_checked_query = spell_check(query)
puts wikipedia_match?(spell_checked_query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment