mirror of
https://github.com/oddluck/limnoria-plugins.git
synced 2025-04-25 12:31:07 -05:00
GoogleCloud: unescape html in replies
This commit is contained in:
parent
801ccc5358
commit
55484b7c7d
@ -40,7 +40,7 @@ import imp
|
||||
__version__ = "2020.02.24+git"
|
||||
|
||||
# XXX Replace this with an appropriate author or supybot.Author instance.
|
||||
__author__ = supybot.Author('oddluck', 'oddluck', 'oddluck@riseup.net')
|
||||
__author__ = supybot.Author("oddluck", "oddluck", "oddluck@riseup.net")
|
||||
__maintainer__ = {}
|
||||
|
||||
# This is a dictionary mapping supybot.Author instances to lists of
|
||||
@ -48,12 +48,13 @@ __maintainer__ = {}
|
||||
__contributors__ = {}
|
||||
|
||||
# This is a url where the most recent plugin package can be downloaded.
|
||||
__url__ = 'https://github.com/oddluck/limnoria-plugins/'
|
||||
__url__ = "https://github.com/oddluck/limnoria-plugins/"
|
||||
|
||||
from . import config
|
||||
from . import plugin
|
||||
from imp import reload
|
||||
imp.reload(plugin) # In case we're being reloaded.
|
||||
|
||||
imp.reload(plugin) # In case we're being reloaded.
|
||||
# Add more reloads here if you add third-party modules and want them to be
|
||||
# reloaded when this plugin is reloaded. Don't forget to import them as well!
|
||||
|
||||
|
@ -29,9 +29,11 @@
|
||||
|
||||
import supybot.conf as conf
|
||||
import supybot.registry as registry
|
||||
|
||||
try:
|
||||
from supybot.i18n import PluginInternationalization
|
||||
_ = PluginInternationalization('GoogleCloud')
|
||||
|
||||
_ = PluginInternationalization("GoogleCloud")
|
||||
except:
|
||||
# Placeholder that allows to run the plugin on a bot
|
||||
# without the i18n module
|
||||
@ -44,21 +46,43 @@ def configure(advanced):
|
||||
# user or not. You should effect your configuration by manipulating the
|
||||
# registry as appropriate.
|
||||
from supybot.questions import expect, anything, something, yn
|
||||
conf.registerPlugin('GoogleCloud', True)
|
||||
|
||||
conf.registerPlugin("GoogleCloud", True)
|
||||
|
||||
|
||||
GoogleCloud = conf.registerPlugin('GoogleCloud')
|
||||
GoogleCloud = conf.registerPlugin("GoogleCloud")
|
||||
|
||||
conf.registerGroup(GoogleCloud, 'translate')
|
||||
conf.registerGroup(GoogleCloud, "translate")
|
||||
|
||||
conf.registerGlobalValue(GoogleCloud.translate, 'key',
|
||||
registry.String('', _("""The Google API translation key
|
||||
(required)"""), private=True))
|
||||
conf.registerGlobalValue(
|
||||
GoogleCloud.translate,
|
||||
"key",
|
||||
registry.String(
|
||||
"", _("""The Google API translation key (required)"""), private=True,
|
||||
),
|
||||
)
|
||||
|
||||
conf.registerChannelValue(GoogleCloud.translate, 'target',
|
||||
registry.String('en', _("""The default target language for the
|
||||
translate command."""), private=True))
|
||||
conf.registerChannelValue(
|
||||
GoogleCloud.translate,
|
||||
"target",
|
||||
registry.String(
|
||||
"en",
|
||||
_("""The default target language for the translate command."""),
|
||||
private=True,
|
||||
),
|
||||
)
|
||||
|
||||
conf.registerChannelValue(GoogleCloud.translate, 'source',
|
||||
registry.String('auto', _("""The default source language for the translate
|
||||
command. Default is 'auto' for automatic language detection."""), private=True))
|
||||
conf.registerChannelValue(
|
||||
GoogleCloud.translate,
|
||||
"source",
|
||||
registry.String(
|
||||
"auto",
|
||||
_(
|
||||
"""
|
||||
The default source language for the translate command. Default is 'auto'
|
||||
for automatic language detection.
|
||||
"""
|
||||
),
|
||||
private=True,
|
||||
),
|
||||
)
|
||||
|
@ -37,46 +37,62 @@ import supybot.log as log
|
||||
import supybot.conf as conf
|
||||
import requests
|
||||
import json
|
||||
import html
|
||||
|
||||
|
||||
class GoogleCloud(callbacks.Plugin):
|
||||
|
||||
|
||||
def translate(self, irc, msg, args, optlist, text):
|
||||
"""[--from <source>] [--to <target>] <text>
|
||||
Translate text using Google Translate API. Uses automatic language detection if source not
|
||||
set. No target uses the plugin default.
|
||||
Translate text using Google Translate API. Uses automatic language detection
|
||||
if source not set. No target uses the plugin default.
|
||||
"""
|
||||
optlist = dict(optlist)
|
||||
key = self.registryValue('translate.key')
|
||||
key = self.registryValue("translate.key")
|
||||
if not key:
|
||||
irc.reply("Error: No API key has been set.")
|
||||
return
|
||||
if 'from' in optlist:
|
||||
source = optlist.get('from')
|
||||
if "from" in optlist:
|
||||
source = optlist.get("from")
|
||||
else:
|
||||
source = self.registryValue('translate.source', msg.channel)
|
||||
if 'to' in optlist:
|
||||
target = optlist.get('to')
|
||||
source = self.registryValue("translate.source", msg.channel)
|
||||
if "to" in optlist:
|
||||
target = optlist.get("to")
|
||||
else:
|
||||
target = self.registryValue('translate.target', msg.channel)
|
||||
if source != 'auto':
|
||||
url = 'https://translation.googleapis.com/language/translate/v2?q={0}&target={1}&source={2}&key={3}'.format(text, target, source, key)
|
||||
target = self.registryValue("translate.target", msg.channel)
|
||||
url = "https://translation.googleapis.com/language/translate/v2"
|
||||
if source != "auto":
|
||||
url += "?q={0}&target={1}&source={2}&key={3}".format(
|
||||
text, target, source, key
|
||||
)
|
||||
else:
|
||||
url = 'https://translation.googleapis.com/language/translate/v2?q={0}&target={1}&key={2}'.format(text, target, key)
|
||||
url += "?q={0}&target={1}&key={2}".format(text, target, key)
|
||||
response = requests.get(url, timeout=10)
|
||||
if not response.status_code == 200:
|
||||
log.debug("GoogleCloud: Error accessing {0}: {1}".format(url, response.content.decode()))
|
||||
log.debug(
|
||||
"GoogleCloud: Error accessing {0}: {1}".format(
|
||||
url, response.content.decode()
|
||||
)
|
||||
)
|
||||
return
|
||||
result = json.loads(response.content)
|
||||
if not result.get('data'):
|
||||
if not result.get("data"):
|
||||
log.debug("GoogleCloud: Error opening JSON response")
|
||||
return
|
||||
if result['data']['translations'][0].get('detectedSourceLanguage'):
|
||||
reply = "{0} [{1}~>{2}]".format(result['data']['translations'][0]['translatedText'], result['data']['translations'][0]['detectedSourceLanguage'], target)
|
||||
if result["data"]["translations"][0].get("detectedSourceLanguage"):
|
||||
reply = "{0} [{1}~>{2}]".format(
|
||||
html.unescape(result["data"]["translations"][0]["translatedText"]),
|
||||
result["data"]["translations"][0]["detectedSourceLanguage"],
|
||||
target,
|
||||
)
|
||||
else:
|
||||
reply = "{0} [{1}~>{2}]".format(result['data']['translations'][0]['translatedText'], source, target)
|
||||
reply = "{0} [{1}~>{2}]".format(
|
||||
html.unescape(result["data"]["translations"][0]["translatedText"]),
|
||||
source,
|
||||
target,
|
||||
)
|
||||
irc.reply(reply)
|
||||
translate = wrap(translate, [getopts({'from':'text', 'to':'text'}), 'text'])
|
||||
|
||||
translate = wrap(translate, [getopts({"from": "text", "to": "text"}), "text"])
|
||||
|
||||
|
||||
Class = GoogleCloud
|
||||
|
Loading…
x
Reference in New Issue
Block a user