Skip to content

Instantly share code, notes, and snippets.

@mashiro
Created October 31, 2013 10:40
Show Gist options
  • Select an option

  • Save mashiro/7247637 to your computer and use it in GitHub Desktop.

Select an option

Save mashiro/7247637 to your computer and use it in GitHub Desktop.
require 'nokogiri'
require 'cgi'
class Google
include Donkun::Plugin
match /(?:google|g)\s+(.+)/
def execute(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
links = doc.css('h3.r:not(.inl) > a').take(3)
results = links.map { |link| CGI.unescape_html "#{sanitize link.text} - #{link[:href]}" }
if stats = doc.at_css('#resultStats')
stats = stats.children.map { |s| sanitize s.text }.compact
results << stats.join(' ') unless stats.empty?
end
results.join("\n")
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