mirror of
https://github.com/oddluck/limnoria-plugins.git
synced 2025-04-29 06:51:12 -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"
|
__version__ = "2020.02.24+git"
|
||||||
|
|
||||||
# XXX Replace this with an appropriate author or supybot.Author instance.
|
# 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__ = {}
|
__maintainer__ = {}
|
||||||
|
|
||||||
# This is a dictionary mapping supybot.Author instances to lists of
|
# This is a dictionary mapping supybot.Author instances to lists of
|
||||||
@ -48,11 +48,12 @@ __maintainer__ = {}
|
|||||||
__contributors__ = {}
|
__contributors__ = {}
|
||||||
|
|
||||||
# This is a url where the most recent plugin package can be downloaded.
|
# 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 config
|
||||||
from . import plugin
|
from . import plugin
|
||||||
from imp import reload
|
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
|
# 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!
|
# 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.conf as conf
|
||||||
import supybot.registry as registry
|
import supybot.registry as registry
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from supybot.i18n import PluginInternationalization
|
from supybot.i18n import PluginInternationalization
|
||||||
_ = PluginInternationalization('GoogleCloud')
|
|
||||||
|
_ = PluginInternationalization("GoogleCloud")
|
||||||
except:
|
except:
|
||||||
# Placeholder that allows to run the plugin on a bot
|
# Placeholder that allows to run the plugin on a bot
|
||||||
# without the i18n module
|
# without the i18n module
|
||||||
@ -44,21 +46,43 @@ def configure(advanced):
|
|||||||
# user or not. You should effect your configuration by manipulating the
|
# user or not. You should effect your configuration by manipulating the
|
||||||
# registry as appropriate.
|
# registry as appropriate.
|
||||||
from supybot.questions import expect, anything, something, yn
|
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',
|
conf.registerGlobalValue(
|
||||||
registry.String('', _("""The Google API translation key
|
GoogleCloud.translate,
|
||||||
(required)"""), private=True))
|
"key",
|
||||||
|
registry.String(
|
||||||
|
"", _("""The Google API translation key (required)"""), private=True,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
conf.registerChannelValue(GoogleCloud.translate, 'target',
|
conf.registerChannelValue(
|
||||||
registry.String('en', _("""The default target language for the
|
GoogleCloud.translate,
|
||||||
translate command."""), private=True))
|
"target",
|
||||||
|
registry.String(
|
||||||
|
"en",
|
||||||
|
_("""The default target language for the translate command."""),
|
||||||
|
private=True,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
conf.registerChannelValue(GoogleCloud.translate, 'source',
|
conf.registerChannelValue(
|
||||||
registry.String('auto', _("""The default source language for the translate
|
GoogleCloud.translate,
|
||||||
command. Default is 'auto' for automatic language detection."""), private=True))
|
"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 supybot.conf as conf
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
|
import html
|
||||||
|
|
||||||
|
|
||||||
class GoogleCloud(callbacks.Plugin):
|
class GoogleCloud(callbacks.Plugin):
|
||||||
|
|
||||||
|
|
||||||
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 Google Translate API. Uses automatic language detection if source not
|
Translate text using Google Translate API. Uses automatic language detection
|
||||||
set. No target uses the plugin default.
|
if source not set. No target uses the plugin default.
|
||||||
"""
|
"""
|
||||||
optlist = dict(optlist)
|
optlist = dict(optlist)
|
||||||
key = self.registryValue('translate.key')
|
key = self.registryValue("translate.key")
|
||||||
if not key:
|
if not key:
|
||||||
irc.reply("Error: No API key has been set.")
|
irc.reply("Error: No API key has been set.")
|
||||||
return
|
return
|
||||||
if 'from' in optlist:
|
if "from" in optlist:
|
||||||
source = optlist.get('from')
|
source = optlist.get("from")
|
||||||
else:
|
else:
|
||||||
source = self.registryValue('translate.source', msg.channel)
|
source = self.registryValue("translate.source", msg.channel)
|
||||||
if 'to' in optlist:
|
if "to" in optlist:
|
||||||
target = optlist.get('to')
|
target = optlist.get("to")
|
||||||
else:
|
else:
|
||||||
target = self.registryValue('translate.target', msg.channel)
|
target = self.registryValue("translate.target", msg.channel)
|
||||||
if source != 'auto':
|
url = "https://translation.googleapis.com/language/translate/v2"
|
||||||
url = 'https://translation.googleapis.com/language/translate/v2?q={0}&target={1}&source={2}&key={3}'.format(text, target, source, key)
|
if source != "auto":
|
||||||
|
url += "?q={0}&target={1}&source={2}&key={3}".format(
|
||||||
|
text, target, source, key
|
||||||
|
)
|
||||||
else:
|
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)
|
response = requests.get(url, timeout=10)
|
||||||
if not response.status_code == 200:
|
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
|
return
|
||||||
result = json.loads(response.content)
|
result = json.loads(response.content)
|
||||||
if not result.get('data'):
|
if not result.get("data"):
|
||||||
log.debug("GoogleCloud: Error opening JSON response")
|
log.debug("GoogleCloud: Error opening JSON response")
|
||||||
return
|
return
|
||||||
if result['data']['translations'][0].get('detectedSourceLanguage'):
|
if result["data"]["translations"][0].get("detectedSourceLanguage"):
|
||||||
reply = "{0} [{1}~>{2}]".format(result['data']['translations'][0]['translatedText'], result['data']['translations'][0]['detectedSourceLanguage'], target)
|
reply = "{0} [{1}~>{2}]".format(
|
||||||
|
html.unescape(result["data"]["translations"][0]["translatedText"]),
|
||||||
|
result["data"]["translations"][0]["detectedSourceLanguage"],
|
||||||
|
target,
|
||||||
|
)
|
||||||
else:
|
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)
|
irc.reply(reply)
|
||||||
translate = wrap(translate, [getopts({'from':'text', 'to':'text'}), 'text'])
|
|
||||||
|
translate = wrap(translate, [getopts({"from": "text", "to": "text"}), "text"])
|
||||||
|
|
||||||
|
|
||||||
Class = GoogleCloud
|
Class = GoogleCloud
|
||||||
|
Loading…
x
Reference in New Issue
Block a user