lib/google_translate/google_translate.rb in google-translate-1.0.1 vs lib/google_translate/google_translate.rb in google-translate-1.1.0

- old
+ new

@@ -1,13 +1,14 @@ #encoding: UTF-8 require 'net/http' require 'json' require 'tempfile' +require 'resource_accessor' class GoogleTranslate - GOOGLE_TRANSLATE_SERVICE_URL = "http://translate.google.com" + GOOGLE_TRANSLATE_SERVICE_URL = "translate.google.com" def supported_languages response = call_service GOOGLE_TRANSLATE_SERVICE_URL from_languages = collect_languages response.body, 0, 'sl', 'gt-sl' @@ -19,21 +20,23 @@ def translate(from_lang, to_lang, text, options={}) raise("Missing 'from' language") unless from_lang raise("Missing 'to' language") unless to_lang raise("Missing text for translation") unless text - result = JSON.parse(call_translate_service(from_lang, to_lang, text)) + r = call_translate_service(from_lang, to_lang, text) + result = JSON.parse(r.gsub('[,', '[')) + raise("Translate Server is down") if (!result || result.empty?) result end def say lang, text speech_content = call_speech_service(lang, text) - file = Tempfile.new('.google_translate_speech-') + file = Tempfile.new('.google_translate_speech---') file.write(speech_content) file.close @@ -43,46 +46,40 @@ end private def translate_url(from_lang, to_lang) - "#{GOOGLE_TRANSLATE_SERVICE_URL}/translate_a/t?client=t&sl=#{from_lang}&tl=#{to_lang}&hl=pl&sc=2&ie=UTF-8&oe=UTF-8&prev=enter&ssel=0&tsel=0&" + "https://#{GOOGLE_TRANSLATE_SERVICE_URL}/translate_a/single?client=t&sl=#{from_lang}&tl=#{to_lang}&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qc&dt=rw&dt=rm&dt=ss&dt=t&dt=at&dt=sw&ie=UTF-8&oe=UTF-8&prev=btn&rom=1&ssel=0&tsel=0" end def speech_url(lang) - "#{GOOGLE_TRANSLATE_SERVICE_URL}/translate_tts?tl=#{lang}&ie=UTF-8&oe=UTF-8" + "http://#{GOOGLE_TRANSLATE_SERVICE_URL}/translate_tts?tl=#{lang}&ie=UTF-8&oe=UTF-8" end def call_translate_service from_lang, to_lang, text url = translate_url(from_lang, to_lang) - response = call_service url, text + response = call_service url + "&q=#{text}" response.body.split(',').collect { |s| s == '' ? "\"\"" : s }.join(",") # fix json object end def call_speech_service lang, text url = speech_url(lang) - response = call_service url, text + response = call_service url + "&q=#{text}" response.body end - def call_service url, text=nil - uri = URI.parse(URI.escape(url)) + def call_service url + accessor = ResourceAccessor.new - http = Net::HTTP.new(uri.host, uri.port) - request = Net::HTTP::Post.new(uri.request_uri) - request.set_form_data(text: text) - - http.request(request) + accessor.get_response url: url end def collect_languages buffer, index, tag_name, tag_id - languages = [] - spaces = '\s?' quote = '(\s|\'|")?' id_part = "id#{spaces}=#{spaces}#{quote}#{tag_id}#{quote}" name_part = "name#{spaces}=#{spaces}#{quote}#{tag_name}#{quote}" @@ -107,13 +104,10 @@ if matches.size == 0 re2 = /<option(\s*)value=([a-z|A-Z|-]*)>([a-z|A-Z|\(|\)|\s]*)<\/option>/ matches = text.gsub(/selected/i, '').squeeze.scan(re2) end - matches.each do |m| languages << Language.new(m[2], m[1]) - end - - languages + matches.map { |m| Language.new(m[2], m[1]) } end end