This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/plugins/translate.lua

100 lines
2.9 KiB
Lua
Raw Normal View History

2016-01-11 19:43:17 +01:00
-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION
2015-04-26 13:28:31 +02:00
do
2016-01-11 19:43:17 +01:00
local mime = require("mime")
2015-04-26 13:28:31 +02:00
2016-01-11 19:43:17 +01:00
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
2015-04-26 13:28:31 +02:00
2016-01-11 19:43:17 +01:00
local trans = json:decode(table.concat(response_body)).d.results[1].Text
2015-04-26 13:28:31 +02:00
2015-06-08 21:53:37 +02:00
return trans
2015-04-26 13:28:31 +02:00
end
2016-01-11 19:43:17 +01:00
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
2015-04-26 13:28:31 +02:00
-- Third pattern
if #matches == 1 then
print("First")
local text = matches[1]
2016-01-11 19:43:17 +01:00
local language = detect_language(text)
return translate(language, nil, text)
2015-04-26 13:28:31 +02:00
end
-- Second pattern
2016-01-11 19:43:17 +01:00
if #matches == 3 and matches[1] == "to:" then
2015-04-26 13:28:31 +02:00
print("Second")
2016-01-11 19:43:17 +01:00
local target = matches[2]
local text = matches[3]
local language = detect_language(text)
return translate(language, target, text)
2015-04-26 13:28:31 +02:00
end
2015-06-08 21:53:37 +02:00
-- First pattern
2015-04-26 13:28:31 +02:00
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 = "<EFBFBD>bersetze Text",
2016-01-11 19:43:17 +01:00
usage = {
"/translate [Text]: <20>bersetze Text in deutsch",
"/translate to:Zielsprache [Text]: <20>bersetze Text in Zielsprache",
"/translate Quellsprache,Zielsprache [Text]: <20>bersetze Text von beliebiger Sprache in beliebige Sprache",
"/whatlang [Text]: Gibt erkannte Sprache zur<75>ck"
},
patterns = {
"^/translate ([%w]+),([%a]+) (.+)",
"^/translate (to%:)([%w]+) (.+)",
"^/translate (.+)",
"^/(whatlang) (.+)"
},
2015-04-26 13:28:31 +02:00
run = run
}
end