]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/fish.rb
lastfm plugin: command to compactly display next events in a requested location
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / fish.rb
index 8c115f90a3660022b74c723a157bd0711dae651a..68e6d29cf872f0a6e3091fd3a8def5f92de3b7d5 100644 (file)
@@ -5,7 +5,7 @@ Net::HTTP.version_1_2
 class BabelPlugin < Plugin
   LANGS = %w{en fr de it pt es nl ru zh zt el ja ko}
   def help(plugin, topic="")
-    "translate to <lang> <string> => translate from english to <lang>, translate from <lang> <string> => translate to english from <lang>, translate <fromlang> <tolang> <string> => translate from <fromlang> to <tolang>. Languages: #{LANGS.join(', ')}"
+    "translate to <lang> <string> => translate from english to <lang>, translate from <lang> <string> => translate to english from <lang>, translate <fromlang> <tolang> <string> => translate from <fromlang> to <tolang>. If <string> is an http url, translates the referenced webpage and returns the 1st content paragraph. Languages: #{LANGS.join(', ')}"
   end
   def translate(m, params)
     langs = LANGS
@@ -13,7 +13,6 @@ class BabelPlugin < Plugin
     trans_to = params[:tolang] ? params[:tolang] : 'en'
     trans_text = params[:phrase].to_s
     
-    query = "/babelfish/tr"
     lang_match = langs.join("|")
     unless(trans_from =~ /^(#{lang_match})$/ && trans_to =~ /^(#{lang_match})$/)
       m.reply "invalid language: valid languagess are: #{langs.join(' ')}"
@@ -22,6 +21,14 @@ class BabelPlugin < Plugin
 
     data_text = URI.escape trans_text
     trans_pair = "#{trans_from}_#{trans_to}"
+
+    if (trans_text =~ /^http:\/\//) && (URI.parse(trans_text) rescue nil)
+      url = 'http://babelfish.altavista.com/babelfish/trurl_pagecontent' +
+        "?lp=#{trans_pair}&url=#{data_text}"
+
+      return Utils.get_first_pars([url], 1, :message => m)
+    end
+
     data = "lp=#{trans_pair}&doit=done&intl=1&tt=urltext&urltext=#{data_text}"
 
     # check cache for previous lookups
@@ -30,39 +37,42 @@ class BabelPlugin < Plugin
       return
     end
 
-    http = @bot.httputil.get_proxy(URI.parse("http://babelfish.altavista.com"))
-
     headers = {
-      "content-type" => "application/x-www-form-urlencoded; charset=utf-8",
-      'accept-charset' => 'utf-8'
+      "content-type" => "application/x-www-form-urlencoded; charset=utf-8"
     }
 
-    http.start {|http|
-      resp = http.post(query, data, headers)
-  
-  if (resp.code == "200")
-    lines = Array.new
-    resp.body.each_line do |l|
-      lines.push l
+    query = "/babelfish/tr"
+
+    begin
+      resp = @bot.httputil.get_response('http://babelfish.altavista.com'+query,
+                                        :method => :post,
+                                        :body => data,
+                                        :headers => headers)
+    rescue Exception => e
+      m.reply "http error: #{e.message}"
+      return
     end
 
-    l = lines.join(" ")
-    debug "babelfish response: #{l}"
+    if (resp.code == "200")
+      lines = Array.new
+      resp.body.each_line { |l| lines.push l }
 
-    if(l =~ /^\s+<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/div>/)
-      answer = $1
-      # cache the answer
-      if(answer.length > 0)
-        @registry["#{trans_pair}/#{data_text}"] = answer
+      l = lines.join(" ")
+      debug "babelfish response: #{l}"
+
+      if(l =~ /^\s+<td bgcolor=white class=s><div style=padding:10px;>(.*)<\/div>/)
+        answer = $1
+        # cache the answer
+        if(answer.length > 0)
+          @registry["#{trans_pair}/#{data_text}"] = answer
+        end
+        m.reply answer
+        return
       end
-      m.reply answer
-      return
+      m.reply "couldn't parse babelfish response html :("
+    else
+      m.reply "couldn't talk to babelfish :("
     end
-    m.reply "couldn't parse babelfish response html :("
-  else
-    m.reply "couldn't talk to babelfish :("
-  end
-  }
   end
 end
 plugin = BabelPlugin.new