diff options
author | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2006-10-14 13:12:59 +0000 |
---|---|---|
committer | Giuseppe Bilotta <giuseppe.bilotta@gmail.com> | 2006-10-14 13:12:59 +0000 |
commit | 9481bd5f3f5499146f1040e58dfef823e2989eeb (patch) | |
tree | b7d612d1d69fce7787bcc976d39f71b9e71d3b77 | |
parent | c7205ab96ecd7adb8ec0f7ebb3ee121d3d922023 (diff) |
Take advantage of Google WAP/WML interface
-rw-r--r-- | data/rbot/plugins/google.rb | 61 |
1 files changed, 25 insertions, 36 deletions
diff --git a/data/rbot/plugins/google.rb b/data/rbot/plugins/google.rb index 604f44cb..8c573c39 100644 --- a/data/rbot/plugins/google.rb +++ b/data/rbot/plugins/google.rb @@ -1,53 +1,42 @@ -require 'net/http' -require 'uri/common' +require 'uri' Net::HTTP.version_1_2 +GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im + class GooglePlugin < Plugin def help(plugin, topic="") - "search <string> => search google for <string>" + "google <string> => search google for <string>" end - def privmsg(m) - unless(m.params && m.params.length > 0) - m.reply "incorrect usage: " + help(m.plugin) - return - end - searchfor = URI.escape m.params - query = "/search?q=#{searchfor}&btnI=I%27m%20feeling%20lucky" - result = "not found!" + def google(m, params) + what = params[:words].to_s + searchfor = URI.escape what - proxy_host = nil - proxy_port = nil + url = "http://www.google.com/wml/search?q=#{searchfor}" - if(ENV['http_proxy']) - if(ENV['http_proxy'] =~ /^http:\/\/(.+):(\d+)$/) - proxy_host = $1 - proxy_port = $2 - end - end - - http = @bot.httputil.get_proxy(URI.parse("http://www.google.com")) begin - http.start {|http| - resp = http.get(query) - if resp.code == "302" - result = resp['location'] - end - } + wml = @bot.httputil.get(url) rescue => e - p e - if e.response && e.response['location'] - result = e.response['location'] - else - result = "error!" - end + m.reply "error googling for #{what}" + return end - m.reply "#{m.params}: #{result}" + results = wml.scan(GOOGLE_WAP_LINK) + if results.length == 0 + m.reply "no results found for #{what}" + return + end + results = results[0...3].map { |res| + "#{res[0]}. #{Bold}#{Utils.decode_html_entities res[2].strip}#{Bold}: #{URI.unescape res[1].strip}" + }.join(" | ") + + m.reply "Results for #{what}: #{results}" end end + plugin = GooglePlugin.new -plugin.register("search") -plugin.register("google") + +plugin.map "search *words", :action => 'google' +plugin.map "google *words", :action => 'google' |