X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Ftranslator.rb;h=933969a1a7f1aa62ffb388fefcf41f5a96443309;hb=12a508e1a75bc1de501f36ca9e4767f2349fe12e;hp=a046b2069dd3151442997104118b294907c12717;hpb=88b3cb20e4aa317cba5ea419a8d77833e76da36c;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/translator.rb b/data/rbot/plugins/translator.rb index a046b206..933969a1 100644 --- a/data/rbot/plugins/translator.rb +++ b/data/rbot/plugins/translator.rb @@ -39,6 +39,14 @@ class Translator @cache = cache end + # Many translators use Mechanize, which changed namespace around version 1.0 + # To support both pre-1.0 and post-1.0 namespaces, we use these auxiliary + # method. The translator still needs to require 'mechanize' on initialization + # if it needs it. + def mechanize + return Mechanize if defined? Mechanize + return WWW::Mechanize + end # whether the translator supports this direction def support?(from, to) @@ -110,12 +118,12 @@ class NiftyTranslator < Translator def initialize(cache={}) require 'mechanize' super(Translator::Direction.all_from_to(%w[ja en zh_CN ko], %w[ja]), cache) - @form = WWW::Mechanize.new. - get('http://nifty.amikai.com/amitext/indexUTF8.jsp'). - forms_with(:name => 'translateForm').last end def do_translate(text, from, to) + @form ||= mechanize.new. + get('http://nifty.amikai.com/amitext/indexUTF8.jsp'). + forms_with(:name => 'translateForm').last @radio = @form.radiobuttons_with(:name => 'langpair').first @radio.value = "#{from},#{to}".upcase @radio.check @@ -150,7 +158,7 @@ class ExciteTranslator < Translator end def open_form(name) - WWW::Mechanize.new.get("http://www.excite.co.jp/world/#{name}"). + mechanize.new.get("http://www.excite.co.jp/world/#{name}"). forms_with(:name => 'world').first end @@ -182,35 +190,30 @@ end class GoogleTranslator < Translator INFO = 'Google Translate ' + LANGUAGES = + %w[af sq am ar hy az eu be bn bh bg my ca chr zh zh_CN zh_TW hr + cs da dv en eo et tl fi fr gl ka de el gn gu iw hi hu is id iu + ga it ja kn kk km ko lv lt mk ms ml mt mr mn ne no or ps fa pl + pt_PT pa ro ru sa sr sd si sk sl es sw sv tg ta tl te th bo tr + uk ur uz ug vi cy yi auto] def initialize(cache={}) - require 'mechanize' - load_form! - - # we can probably safely assume that google translate is able to translate from - # any language in the source lang drop down list to any language in the target one - # so we create the language pairs based on that assumption - sl = @source_list.options.map { |o| o.value.sub('-', '_') } - tl = @target_list.options.map { |o| o.value.sub('-', '_') } - super(Translator::Direction.all_from_to(tl, sl), cache) - end - - def load_form! - agent = WWW::Mechanize.new - # without faking the user agent, Google Translate will serve non-UTF-8 text - agent.user_agent_alias = 'Linux Konqueror' - @form = agent.get('http://www.google.com/translate_t'). - forms_with(:action => '/translate_t').first - @source_list = @form.fields_with(:name => 'sl').last - @target_list = @form.fields_with(:name => 'tl').last + require "uri" + require "json" + super(Translator::Direction.all_to_all(LANGUAGES), cache) end def do_translate(text, from, to) - load_form! + langpair = [from == 'auto' ? '' : from, to].map { |e| e.tr('_', '-') }.join("|") + raw_json = Irc::Utils.bot.httputil.get_response(URI.escape( + "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=#{text}&langpair=#{langpair}")).body + response = JSON.parse(raw_json) - @source_list.value = from.sub('_', '-') - @target_list.value = to.sub('_', '-') - @form.fields_with(:name => 'text').last.value = text - @form.submit.parser.search('div#result_box').inner_html + if response["responseStatus"] != 200 + raise Translator::NoTranslationError, response["responseDetails"] + else + translation = response["responseData"]["translatedText"] + return Utils.decode_html_entities(translation) + end end end @@ -220,16 +223,24 @@ class BabelfishTranslator < Translator def initialize(cache) require 'mechanize' - - @form = WWW::Mechanize.new.get('http://babelfish.altavista.com/babelfish/'). - forms_with(:name => 'frmTrText').first - @lang_list = @form.fields_with(:name => 'lp').first - language_pairs = @lang_list.options.map {|o| o.value.split('_')}. - reject {|p| p.empty?} + (_, lang_list) = parse_page + language_pairs = lang_list.options.map {|o| o.value.split('_')}. + reject {|p| p.empty?} super(Translator::Direction.pairs(language_pairs), cache) end + def parse_page + form = mechanize.new.get('http://babelfish.altavista.com/babelfish/'). + forms_with(:name => 'frmTrText').first + lang_list = form.fields_with(:name => 'lp').first + [form, lang_list] + end + def do_translate(text, from, to) + unless @form && @lang_list + @form, @lang_list = parse_page + end + if @form.fields_with(:name => 'trtext').empty? @form.add_field!('trtext', text) else @@ -280,16 +291,13 @@ class TranslatorPlugin < Plugin def initialize super - + @failed_translators = [] @translators = {} TRANSLATORS.each_pair do |name, c| - begin + watch_for_fail(name) do @translators[name] = c.new(@registry.sub_registry(name)) map "#{name} :from :to *phrase", :action => :cmd_translate, :thread => true - rescue Exception - warning _("Translator %{name} cannot be used: %{reason}") % - {:name => name, :reason => $!} end end @@ -301,35 +309,95 @@ class TranslatorPlugin < Plugin update_default end + def watch_for_fail(name, &block) + begin + yield + rescue Exception + @failed_translators << { :name => name, :reason => $!.to_s } + + warning _("Translator %{name} cannot be used: %{reason}") % + {:name => name, :reason => $!} + map "#{name} [*args]", :action => :failed_translator, + :defaults => {:name => name, :reason => $!} + end + end + + def failed_translator(m, params) + m.reply _("Translator %{name} cannot be used: %{reason}") % + {:name => params[:name], :reason => params[:reason]} + end + def help(plugin, topic=nil) - if @translators.has_key?(plugin) - translator = @translators[plugin] - _('%{translator} => Look up phrase using %{info}, supported from -> to languages: %{directions}') % { - :translator => plugin, - :info => translator.class::INFO, - :directions => translator.directions.map do |source, targets| - _('%{source} -> %{targets}') % - {:source => source, :targets => targets.to_a.join(', ')} - end.join(' | ') - } + case (topic.intern rescue nil) + when :failed + unless @failed_translators.empty? + failed_list = @failed_translators.map { |t| _("%{bold}%{translator}%{bold}: %{reason}") % { + :translator => t[:name], + :reason => t[:reason], + :bold => Bold + }} + + _("Failed translators: %{list}") % { :list => failed_list.join(", ") } + else + _("None of the translators failed") + end else - _('Command: , where is one of: %{translators}. If "translator" is used in place of the translator name, the first translator in translator.default_list which supports the specified direction will be picked automatically. Use "help " to look up supported from and to languages') % - {:translators => @translators.keys.join(', ')} + if @translators.has_key?(plugin) + translator = @translators[plugin] + _('%{translator} => Look up phrase using %{info}, supported from -> to languages: %{directions}') % { + :translator => plugin, + :info => translator.class::INFO, + :directions => translator.directions.map do |source, targets| + _('%{source} -> %{targets}') % + {:source => source, :targets => targets.to_a.join(', ')} + end.join(' | ') + } + else + help_str = _('Command: , where is one of: %{translators}. If "translator" is used in place of the translator name, the first translator in translator.default_list which supports the specified direction will be picked automatically. Use "help " to look up supported from and to languages') % + {:translators => @translators.keys.join(', ')} + + help_str << "\n" + _("%{bold}Note%{bold}: %{failed_amt} translators failed, see %{reverse}%{prefix}help translate failed%{reverse} for details") % { + :failed_amt => @failed_translators.size, + :bold => Bold, + :reverse => Reverse, + :prefix => @bot.config['core.address_prefix'].first + } + + help_str + end end end + def languages + @languages ||= @translators.map { |t| t.last.directions.keys }.flatten.uniq + end + def update_default @default_translators = bot.config['translator.default_list'] & @translators.keys end def cmd_translator(m, params) params[:to] = @bot.config['translator.destination'] if params[:to].nil? - + params[:from] ||= 'auto' translator = @default_translators.find {|t| @translators[t].support?(params[:from], params[:to])} + if translator - cmd_translate m, params.merge({:translator => translator, :show_provider => true}) + cmd_translate m, params.merge({:translator => translator, :show_provider => false}) else - m.reply _('None of the default translators (translator.default_list) supports translating from %{source} to %{target}') % {:source => from, :target => to} + # When translate command is used without source language, "auto" as source + # language is assumed. It means that google translator is used and we let google + # figure out what the source language is. + # + # Problem is that the google translator will fail if the system that the bot is + # running on does not have the json gem installed. + if params[:from] == 'auto' + m.reply _("Unable to auto-detect source language due to broken google translator, see %{reverse}%{prefix}help translate failed%{reverse} for details") % { + :reverse => Reverse, + :prefix => @bot.config['core.address_prefix'].first + } + else + m.reply _('None of the default translators (translator.default_list) supports translating from %{source} to %{target}') % {:source => params[:from], :target => params[:to]} + end end end @@ -339,32 +407,57 @@ class TranslatorPlugin < Plugin translator = @translators[tname] from, to, phrase = params[:from], params[:to], params[:phrase].to_s if translator - begin - translation = Timeout.timeout(@bot.config['translator.timeout']) do - translator.translate(phrase, from, to) + watch_for_fail(tname) do + begin + translation = Timeout.timeout(@bot.config['translator.timeout']) do + translator.translate(phrase, from, to) + end + m.reply(if params[:show_provider] + _('%{translation} (provided by %{translator})') % + {:translation => translation, :translator => tname.gsub("_", " ")} + else + translation + end) + + rescue Translator::UnsupportedDirectionError + m.reply _("%{translator} doesn't support translating from %{source} to %{target}") % + {:translator => tname, :source => from, :target => to} + rescue Translator::NoTranslationError + m.reply _('%{translator} failed to provide a translation') % + {:translator => tname} + rescue Timeout::Error + m.reply _('The translator timed out') end - m.reply(if params[:show_provider] - _('%{translation} (provided by %{translator})') % - {:translation => translation, :translator => tname} - else - translation - end) - - rescue Translator::UnsupportedDirectionError - m.reply _("%{translator} doesn't support translating from %{source} to %{target}") % - {:translator => tname, :source => from, :target => to} - rescue Translator::NoTranslationError - m.reply _('%{translator} failed to provide a translation') % - {:translator => tname} - rescue Timeout::Error - m.reply _('The translator timed out') end else m.reply _('No translator called %{name}') % {:name => tname} end end + + # URL translation has nothing to do with Translators so let's make it + # separate, and Google exclusive for now + def cmd_translate_url(m, params) + params[:to] = @bot.config['translator.destination'] if params[:to].nil? + params[:from] ||= 'auto' + + translate_url = "http://translate.google.com/translate?sl=%{from}&tl=%{to}&u=%{url}" % { + :from => params[:from], + :to => params[:to], + :url => CGI.escape(params[:url].to_s) + } + + m.reply(translate_url) + end end plugin = TranslatorPlugin.new -plugin.map 'translator :from [:to] *phrase', - :action => :cmd_translator, :thread => true +req = Hash[*%w(from to).map { |e| [e.to_sym, /#{plugin.languages.join("|")}/] }.flatten] + +plugin.map 'translate [:from] [:to] :url', + :action => :cmd_translate_url, :requirements => req.merge(:url => %r{^https?://[^\s]*}) +plugin.map 'translator [:from] [:to] :url', + :action => :cmd_translate_url, :requirements => req.merge(:url => %r{^https?://[^\s]*}) +plugin.map 'translate [:from] [:to] *phrase', + :action => :cmd_translator, :thread => true, :requirements => req +plugin.map 'translator [:from] [:to] *phrase', + :action => :cmd_translator, :thread => true, :requirements => req