Shadow19

Bing Translator filter for DPE

Aug 20th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.19 KB | None | 0 0
  1. if direction == "<"
  2.     # https://en.wikipedia.org/wiki/ISO_639-1 on how to set your target lang
  3.     # Default: lang="en" which means to english
  4.     def translateIt(words, check=false, lang="en")
  5.         return File.binread("_translate") if File.file?("_translate") and !words and check
  6.         error = false
  7.         if Thread.main["bing_aid"] and !check
  8.             appId = Thread.main["bing_aid"]
  9.             return nil if words.strip == ""
  10.             words = URI::encode(words)
  11.             language = eat("https://api.microsofttranslator.com/V2/Ajax.Svc/Detect?text=\"#{words}\"&appId=#{appId}").gsub(/[\W]/,"") rescue (error = true)
  12.             return nil if !language
  13.             return translateIt(words,true) if language["ArgumentException: The token has expired"]
  14.             transl = eat("https://api.microsofttranslator.com/V2/Ajax.Svc/Translate?text=\"#{words}\"&from=\"#{language}\"&to=\"#{lang}\"&appId=#{appId}") rescue (error = true)
  15.             return !error ? transl.gsub(/[^\w\s\']/,"") : nil
  16.        elsif check
  17.            appId = eat("https://ssl.microsofttranslator.com/ajax/v3/WidgetV3.ashx?siteData=ueOIGRSKkd965FeEGM5JtQ**")[/\{appId\:\'(.*?)'/,1] rescue (error = true)
  18.             File.binwrite("_translate",appId) if !error
  19.             translateIt(words,false) if words and !error
  20.             return !error ? appId : nil
  21.         end
  22.         return nil
  23.     end
  24.     if decoy["done"]
  25.         Thread.main["bing_aid"] = translateIt(false,true) if !Thread.main["bing_aid"]
  26.         if data.getAttr("t").class == String
  27.             if data.tag?("m")
  28.                 if data.getAttr("t")[0] != "/"
  29.                     Thread.new(decoy) do |decoy|
  30.                         ndata = deepCopy(data)
  31.                         trans = ndata.getAttr("t").gsub(/\([^()]*?\)/,"").strip
  32.                         trans = translateIt(CGI::unescapeHTML(trans)) if trans != ""
  33.                         ndata.setAttr("t","[Bing Translate] #{trans}") if trans.class == String and trans != ""
  34.                         decoy["<"].print(ndata) if trans.class == String and trans != ""
  35.                         ndata = nil
  36.                     end
  37.                 end
  38.             end
  39.         end
  40.     end
  41. end
Advertisement
Add Comment
Please, Sign In to add comment