translate nutzt nun Bing

This commit is contained in:
Akamaru 2016-01-11 19:43:17 +01:00
parent 3662784c7c
commit c1ae727c3e
2 changed files with 132 additions and 32 deletions

View File

@ -0,0 +1,67 @@
--[[
-- Translate text using Google Translate.
-- http://translate.google.com/translate_a/single?client=t&ie=UTF-8&oe=UTF-8&hl=en&dt=t&tl=en&sl=auto&text=hello
--]]
do
function translate(source_lang, target_lang, text)
local path = "http://translate.google.com/translate_a/single"
-- URL query parameters
local params = {
client = "t",
ie = "UTF-8",
oe = "UTF-8",
hl = "de",
dt = "t",
tl = target_lang or "de",
sl = source_lang or "auto",
text = URL.escape(text)
}
local query = format_http_params(params, true)
local url = path..query
local res, code = https.request(url)
-- Return nil if error
if code > 200 then return nil end
local trans = res:gmatch("%[%[%[\"(.*)\"")():gsub("\"(.*)", "")
return trans
end
function run(msg, matches)
-- Third pattern
if #matches == 1 then
print("First")
local text = matches[1]
return translate(nil, nil, text)
end
-- Second pattern
if #matches == 2 then
print("Second")
local target = matches[1]
local text = matches[2]
return translate(nil, target, text)
end
-- First pattern
if #matches == 3 then
print("Third")
local source = matches[1]
local target = matches[2]
local text = matches[3]
return translate(source, target, text)
end
end
return {
description = "Übersetze Text",
usage = {"/translate [Text]"},
patterns = {"^/translate ([%w]+),([%a]+) (.+)","^/translate ([%w]+) (.+)","^/translate (.+)",},
run = run
}
end

View File

@ -1,49 +1,72 @@
--[[
-- Translate text using Google Translate.
-- http://translate.google.com/translate_a/single?client=t&ie=UTF-8&oe=UTF-8&hl=en&dt=t&tl=en&sl=auto&text=hello
--]]
-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION
do
function translate(source_lang, target_lang, text)
local path = "http://translate.google.com/translate_a/single"
-- URL query parameters
local params = {
client = "t",
ie = "UTF-8",
oe = "UTF-8",
hl = "de",
dt = "t",
tl = target_lang or "de",
sl = source_lang or "auto",
text = URL.escape(text)
local mime = require("mime")
local bing_key = cred_data.bing_key
local accountkey = mime.b64(bing_key..':'..bing_key)
local function translate(source_lang, target_lang, text)
if not target_lang then target_lang = 'de' end
local url = 'https://api.datamarket.azure.com/Bing/MicrosoftTranslator/Translate?$format=json&Text=%27'..URL.escape(text)..'%27&To=%27'..target_lang..'%27&From=%27'..source_lang..'%27'
local response_body = {}
local request_constructor = {
url = url,
method = "GET",
sink = ltn12.sink.table(response_body),
headers = {
Authorization = "Basic "..accountkey
}
}
local ok, response_code, response_headers, response_status_line = https.request(request_constructor)
if not ok or response_code ~= 200 then return 'Ein Fehler ist aufgetreten.' end
local query = format_http_params(params, true)
local url = path..query
local res, code = https.request(url)
-- Return nil if error
if code > 200 then return nil end
local trans = res:gmatch("%[%[%[\"(.*)\"")():gsub("\"(.*)", "")
local trans = json:decode(table.concat(response_body)).d.results[1].Text
return trans
end
function run(msg, matches)
local function detect_language(text)
local url = 'https://api.datamarket.azure.com/Bing/MicrosoftTranslator/Detect?$format=json&Text=%27'..URL.escape(text)..'%27'
local response_body = {}
local request_constructor = {
url = url,
method = "GET",
sink = ltn12.sink.table(response_body),
headers = {
Authorization = "Basic "..accountkey
}
}
local ok, response_code, response_headers, response_status_line = https.request(request_constructor)
if not ok or response_code ~= 200 then return 'en' end
local language = json:decode(table.concat(response_body)).d.results[1].Code
return language
end
local function run(msg, matches)
if matches[1] == 'whatlang' and matches[2] then
local text = matches[2]
local lang = detect_language(text)
return 'Erkannte Sprache: '..lang
end
-- Third pattern
if #matches == 1 then
print("First")
local text = matches[1]
return translate(nil, nil, text)
local language = detect_language(text)
return translate(language, nil, text)
end
-- Second pattern
if #matches == 2 then
if #matches == 3 and matches[1] == "to:" then
print("Second")
local target = matches[1]
local text = matches[2]
return translate(nil, target, text)
local target = matches[2]
local text = matches[3]
local language = detect_language(text)
return translate(language, target, text)
end
-- First pattern
@ -59,8 +82,18 @@ end
return {
description = "Übersetze Text",
usage = {"/translate [Text]"},
patterns = {"^/translate ([%w]+),([%a]+) (.+)","^/translate ([%w]+) (.+)","^/translate (.+)",},
usage = {
"/translate [Text]: Übersetze Text in deutsch",
"/translate to:Zielsprache [Text]: Übersetze Text in Zielsprache",
"/translate Quellsprache,Zielsprache [Text]: Übersetze Text von beliebiger Sprache in beliebige Sprache",
"/whatlang [Text]: Gibt erkannte Sprache zurück"
},
patterns = {
"^/translate ([%w]+),([%a]+) (.+)",
"^/translate (to%:)([%w]+) (.+)",
"^/translate (.+)",
"^/(whatlang) (.+)"
},
run = run
}