X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Ffish.rb;h=5b24d2417f5fb71403a2cd1231787446de8d288b;hb=22e6cefa54de681b131ecb97fc9383ff5e990dfe;hp=68e6d29cf872f0a6e3091fd3a8def5f92de3b7d5;hpb=e67353c23940b892e0546c8451facb54064561aa;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/fish.rb b/data/rbot/plugins/fish.rb index 68e6d29c..5b24d241 100644 --- a/data/rbot/plugins/fish.rb +++ b/data/rbot/plugins/fish.rb @@ -1,16 +1,29 @@ -require 'net/http' -require 'uri/common' -Net::HTTP.version_1_2 - class BabelPlugin < Plugin LANGS = %w{en fr de it pt es nl ru zh zt el ja ko} + BASEURL = 'http://babelfish.yahoo.com' + + Config.register Config::EnumValue.new('translate.default_from', + :values => LANGS, :default => 'en', + :desc => "Default language to translate from") + Config.register Config::EnumValue.new('translate.default_to', + :values => LANGS, :default => 'en', + :desc => "Default language to translate to") + def help(plugin, topic="") - "translate to => translate from english to , translate from => translate to english from , translate => translate from to . If is an http url, translates the referenced webpage and returns the 1st content paragraph. Languages: #{LANGS.join(', ')}" + case topic + when 'cache' + "translate cache [view|clear] => view or clear the translate cache contents" + else + from = @bot.config['translate.default_from'] + to = @bot.config['translate.default_to'] + "translate to => translate from #{from} to , translate from => translate to #{to} from , translate => translate from to . If is an http url, translates the referenced webpage and returns the 1st content paragraph. Languages: #{LANGS.join(', ')}. Other topics: cache" + end end + def translate(m, params) langs = LANGS - trans_from = params[:fromlang] ? params[:fromlang] : 'en' - trans_to = params[:tolang] ? params[:tolang] : 'en' + trans_from = params[:fromlang] ? params[:fromlang] : @bot.config['translate.default_from'] + trans_to = params[:tolang] ? params[:tolang] : @bot.config['translate.default_to'] trans_text = params[:phrase].to_s lang_match = langs.join("|") @@ -19,14 +32,17 @@ class BabelPlugin < Plugin return end - data_text = URI.escape trans_text + data_text = CGI.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) + m.reply 'webpage translation is not currently supported' + return + # TODO FIXME + # url = BASEURL+'/translate_url' + + # "?lp=#{trans_pair}&trurl=#{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}" @@ -41,42 +57,65 @@ class BabelPlugin < Plugin "content-type" => "application/x-www-form-urlencoded; charset=utf-8" } - query = "/babelfish/tr" + query = "/translate_txt" begin - resp = @bot.httputil.get_response('http://babelfish.altavista.com'+query, - :method => :post, - :body => data, - :headers => headers) + body = @bot.httputil.get(BASEURL+query, + :method => :post, + :body => data, + :headers => headers) rescue Exception => e m.reply "http error: #{e.message}" return end - if (resp.code == "200") - lines = Array.new - resp.body.each_line { |l| lines.push l } - - l = lines.join(" ") - debug "babelfish response: #{l}" - - if(l =~ /^\s+
(.*)<\/div>/) - answer = $1 - # cache the answer - if(answer.length > 0) - @registry["#{trans_pair}/#{data_text}"] = answer - end - m.reply answer - return + case body + when nil + m.reply "couldn't talk to babelfish :(" + when /^\s*
(.*?)<\/div><\/div>\s*$/ + answer = $1 + # cache the answer + if(answer.length > 0) + @registry["#{trans_pair}/#{data_text}"] = answer end + m.reply answer + return + when /^\s+