mirror of
https://github.com/oddluck/limnoria-plugins.git
synced 2025-04-29 15:01:11 -05:00
Azure: add template, language names.
This commit is contained in:
parent
d8ced96b72
commit
83c35b5b37
@ -60,8 +60,9 @@ conf.registerGlobalValue(
|
|||||||
registry.String(
|
registry.String(
|
||||||
"",
|
"",
|
||||||
_(
|
_(
|
||||||
"""The Azure API translation key
|
"""
|
||||||
(required)"""
|
The Azure API translation key (required)
|
||||||
|
"""
|
||||||
),
|
),
|
||||||
private=True,
|
private=True,
|
||||||
),
|
),
|
||||||
@ -73,10 +74,10 @@ conf.registerChannelValue(
|
|||||||
registry.String(
|
registry.String(
|
||||||
"en",
|
"en",
|
||||||
_(
|
_(
|
||||||
"""The default target language for the
|
"""
|
||||||
translate command."""
|
The default target language for the translate command.
|
||||||
|
"""
|
||||||
),
|
),
|
||||||
private=True,
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -86,9 +87,24 @@ conf.registerChannelValue(
|
|||||||
registry.String(
|
registry.String(
|
||||||
"auto",
|
"auto",
|
||||||
_(
|
_(
|
||||||
"""The default source language for the translate
|
"""
|
||||||
command. Default is 'auto' for automatic language detection."""
|
The default source language for the translate command.
|
||||||
|
Default is 'auto' for automatic language detection.
|
||||||
|
"""
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
conf.registerChannelValue(
|
||||||
|
Azure.translate,
|
||||||
|
"template",
|
||||||
|
registry.String(
|
||||||
|
"$text [$sourceName to $targetName]",
|
||||||
|
_(
|
||||||
|
"""
|
||||||
|
The default reply template for the translate command.
|
||||||
|
Variables are $text, $sourceISO, $sourceName, $targetISO, $targetName.
|
||||||
|
"""
|
||||||
),
|
),
|
||||||
private=True,
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -29,16 +29,28 @@
|
|||||||
|
|
||||||
|
|
||||||
import supybot.utils as utils
|
import supybot.utils as utils
|
||||||
from supybot.commands import *
|
|
||||||
import supybot.plugins as plugins
|
import supybot.plugins as plugins
|
||||||
import supybot.ircutils as ircutils
|
import supybot.ircutils as ircutils
|
||||||
import supybot.callbacks as callbacks
|
import supybot.callbacks as callbacks
|
||||||
import supybot.conf as conf
|
import supybot.conf as conf
|
||||||
import requests
|
import supybot.log as log
|
||||||
|
from supybot.commands import *
|
||||||
|
from string import Template
|
||||||
import json
|
import json
|
||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
class Azure(callbacks.Plugin):
|
class Azure(callbacks.Plugin):
|
||||||
|
def __init__(self, irc):
|
||||||
|
self.__parent = super(Azure, self)
|
||||||
|
self.__parent.__init__(irc)
|
||||||
|
r = requests.get(
|
||||||
|
"https://api.cognitive.microsofttranslator.com/languages?api-version=3.0&scope=translation",
|
||||||
|
timeout=10,
|
||||||
|
)
|
||||||
|
self.languages = json.loads(r.content.decode())
|
||||||
|
self.languages = self.languages["translation"]
|
||||||
|
|
||||||
def translate(self, irc, msg, args, optlist, text):
|
def translate(self, irc, msg, args, optlist, text):
|
||||||
"""[--from <source>] [--to <target>] <text>
|
"""[--from <source>] [--to <target>] <text>
|
||||||
Translate text using Microsoft Azure. Uses automatic language detection if source not
|
Translate text using Microsoft Azure. Uses automatic language detection if source not
|
||||||
@ -54,14 +66,34 @@ class Azure(callbacks.Plugin):
|
|||||||
target = optlist.get("to")
|
target = optlist.get("to")
|
||||||
else:
|
else:
|
||||||
target = self.registryValue("translate.target", msg.channel)
|
target = self.registryValue("translate.target", msg.channel)
|
||||||
|
|
||||||
|
url = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&"
|
||||||
if source != "auto":
|
if source != "auto":
|
||||||
url = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to={0}&from={1}".format(
|
if self.languages.get(source) and self.languages.get(target):
|
||||||
target, source
|
url += "to={0}&from={1}".format(target, source)
|
||||||
)
|
else:
|
||||||
|
for lang in self.languages:
|
||||||
|
if source.lower() in self.languages[lang]["name"].lower():
|
||||||
|
source = lang
|
||||||
|
if target.lower() in self.languages[lang]["name"].lower():
|
||||||
|
target = lang
|
||||||
|
if self.languages.get(source) and self.languages.get(target):
|
||||||
|
url += "to={0}&from={1}".format(target, source)
|
||||||
|
else:
|
||||||
|
irc.reply("Invalid language selection.")
|
||||||
|
return
|
||||||
else:
|
else:
|
||||||
url = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to={0}".format(
|
if self.languages.get(target):
|
||||||
target
|
url += "to={0}".format(target)
|
||||||
)
|
else:
|
||||||
|
for lang in self.languages:
|
||||||
|
if target.lower() in self.languages[lang]["name"].lower():
|
||||||
|
target = lang
|
||||||
|
if self.languages.get(target):
|
||||||
|
url += "to={0}".format(target)
|
||||||
|
else:
|
||||||
|
irc.reply("Invalid language selection.")
|
||||||
|
return
|
||||||
key = self.registryValue("translate.key")
|
key = self.registryValue("translate.key")
|
||||||
headers = {"Ocp-Apim-Subscription-Key": key, "Content-type": "application/json"}
|
headers = {"Ocp-Apim-Subscription-Key": key, "Content-type": "application/json"}
|
||||||
body = [{"text": text}]
|
body = [{"text": text}]
|
||||||
@ -71,18 +103,21 @@ class Azure(callbacks.Plugin):
|
|||||||
"Azure: Error accessing {0}: {1}".format(url, response.content.decode())
|
"Azure: Error accessing {0}: {1}".format(url, response.content.decode())
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
result = json.loads(response.content)
|
result = json.loads(response.content.decode())
|
||||||
if result[0].get("detectedLanguage"):
|
if result[0].get("translations"):
|
||||||
reply = "{0} [{1}~>{2}]".format(
|
template = Template(self.registryValue("translate.template", msg.channel))
|
||||||
result[0]["translations"][0]["text"],
|
results = {
|
||||||
result[0]["detectedLanguage"]["language"],
|
"text": result[0]["translations"][0]["text"],
|
||||||
target,
|
"targetName": self.languages[target]["name"],
|
||||||
)
|
"targetISO": target,
|
||||||
else:
|
}
|
||||||
reply = "{0} [{1}~>{2}]".format(
|
if result[0].get("detectedLanguage"):
|
||||||
result[0]["translations"][0]["text"], source, target
|
results["sourceName"] = self.languages[
|
||||||
)
|
result[0]["detectedLanguage"]["language"]
|
||||||
irc.reply(reply)
|
]["name"]
|
||||||
|
else:
|
||||||
|
results["sourceName"] = self.languages[source]["name"]
|
||||||
|
irc.reply(template.safe_substitute(results))
|
||||||
|
|
||||||
translate = wrap(translate, [getopts({"from": "text", "to": "text"}), "text"])
|
translate = wrap(translate, [getopts({"from": "text", "to": "text"}), "text"])
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user