Merge branch 'master' into develop
This commit is contained in:
commit
538d6ca5b0
3 changed files with 19 additions and 20 deletions
|
@ -1,19 +1,16 @@
|
||||||
#--depends-on commands
|
#--depends-on commands
|
||||||
#--require-config google-api-key
|
|
||||||
|
|
||||||
import json, re
|
import json, re
|
||||||
from src import ModuleManager, utils
|
from src import ModuleManager, utils
|
||||||
|
|
||||||
URL_TRANSLATE = "https://translation.googleapis.com/language/translate/v2"
|
URL_TRANSLATE = "http://translate.googleapis.com/translate_a/single"
|
||||||
URL_LANGUAGES = "https://cloud.google.com/translate/docs/languages"
|
URL_LANGUAGES = "https://cloud.google.com/translate/docs/languages"
|
||||||
REGEX_LANGUAGES = re.compile("(\w+)?:(\w+)? ")
|
REGEX_LANGUAGES = re.compile("(\w+)?:(\w+)? ")
|
||||||
|
|
||||||
|
|
||||||
class Module(ModuleManager.BaseModule):
|
class Module(ModuleManager.BaseModule):
|
||||||
|
|
||||||
@utils.hook("received.command.tr", alias_of="translate")
|
@utils.hook("received.command.tr", alias_of="translate")
|
||||||
@utils.hook("received.command.translate")
|
@utils.hook("received.command.translate")
|
||||||
@utils.spec("?<from:to>lstring !<phrase>lstring")
|
@utils.spec("!<phrase>lstring")
|
||||||
def translate(self, event):
|
def translate(self, event):
|
||||||
"""
|
"""
|
||||||
:help: Translate the provided phrase or the last line in thie current
|
:help: Translate the provided phrase or the last line in thie current
|
||||||
|
@ -32,19 +29,21 @@ class Module(ModuleManager.BaseModule):
|
||||||
target_language = language_match.group(2)
|
target_language = language_match.group(2)
|
||||||
phrase = phrase.split(" ", 1)[1]
|
phrase = phrase.split(" ", 1)[1]
|
||||||
|
|
||||||
page = utils.http.request(URL_TRANSLATE,
|
page = utils.http.request(URL_TRANSLATE, get_params={
|
||||||
method="POST",
|
"client": "gtx", "dt": "t", "q": phrase,
|
||||||
post_data={
|
"sl": source_language, "tl": target_language})
|
||||||
"q": phrase,
|
|
||||||
"format": "text",
|
|
||||||
"source": source_language,
|
|
||||||
"target": target_language,
|
|
||||||
"key": self.bot.config["google-api-key"]
|
|
||||||
}).json()
|
|
||||||
|
|
||||||
if "data" in page:
|
if page and not page.data.startswith(b"[null,null,"):
|
||||||
translation = page["data"]["translations"][0]["translatedText"]
|
data = page.decode("utf8")
|
||||||
event["stdout"].write("(%s -> %s) %s" % (source_language, target_language, translation))
|
while ",," in data:
|
||||||
|
data = data.replace(",,", ",null,")
|
||||||
|
data = data.replace("[,", "[null,")
|
||||||
|
data_json = json.loads(data)
|
||||||
|
detected_source = data_json[2]
|
||||||
|
event["stdout"].write("(%s -> %s) %s" % (
|
||||||
|
detected_source, target_language.lower(),
|
||||||
|
data_json[0][0][0]))
|
||||||
else:
|
else:
|
||||||
event["stderr"].write("Failed to translate, try checking "
|
event["stderr"].write("Failed to translate, try checking "
|
||||||
"source/target languages (" + URL_LANGUAGES + ")")
|
"source/target languages (" + URL_LANGUAGES + ")")
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
import datetime, re, urllib.parse
|
import datetime, re, urllib.parse
|
||||||
from src import EventManager, ModuleManager, utils
|
from src import EventManager, ModuleManager, utils
|
||||||
|
|
||||||
REGEX_YOUTUBE = re.compile("https?://(?:www\.|m\.)?(?:youtu.be/|youtube.com/)\\S+", re.I)
|
REGEX_YOUTUBE = re.compile("https?://(?:www\.|m\.|music\.)?(?:youtu.be/|youtube.com/)\\S+", re.I)
|
||||||
|
|
||||||
URL_YOUTUBESEARCH = "https://www.googleapis.com/youtube/v3/search"
|
URL_YOUTUBESEARCH = "https://www.googleapis.com/youtube/v3/search"
|
||||||
URL_YOUTUBEVIDEO = "https://www.googleapis.com/youtube/v3/videos"
|
URL_YOUTUBEVIDEO = "https://www.googleapis.com/youtube/v3/videos"
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
beautifulsoup4 ==4.8.0
|
beautifulsoup4 ==4.8.0
|
||||||
cryptography ==2.7
|
cryptography ==3.2
|
||||||
dataclasses ==0.6;python_version<'3.7'
|
dataclasses ==0.6;python_version<'3.7'
|
||||||
dnspython ==2.0.0
|
dnspython ==2.0.0
|
||||||
feedparser ==5.2.1
|
feedparser ==5.2.1
|
||||||
|
|
Loading…
Reference in a new issue