Convert to V3.2 translation module
This commit is contained in:
parent
9b5c1d2e60
commit
06145c732a
@ -6,67 +6,92 @@ import socket
|
|||||||
import json
|
import json
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
nemubotversion = 3.0
|
nemubotversion = 3.2
|
||||||
|
|
||||||
import module_states_file as xmlparser
|
import module_states_file as xmlparser
|
||||||
|
|
||||||
LANG = ["ar", "zh", "cz", "en", "fr", "gr", "it", "ja", "ko", "pl", "pt", "ro", "es", "tr"]
|
LANG = ["ar", "zh", "cz", "en", "fr", "gr", "it",
|
||||||
|
"ja", "ko", "pl", "pt", "ro", "es", "tr"]
|
||||||
|
|
||||||
def parseanswer(msg):
|
def load(context):
|
||||||
global LANG
|
from hooks import Hook
|
||||||
if msg.cmd[0] == "translate":
|
context.hooks.add_hook(context.hooks.cmd_hook,
|
||||||
|
Hook(cmd_translate, "translate"))
|
||||||
|
context.hooks.add_hook(context.hooks.cmd_hook,
|
||||||
|
Hook(cmd_translate, "traduction"))
|
||||||
|
context.hooks.add_hook(context.hooks.cmd_hook,
|
||||||
|
Hook(cmd_translate, "traduit"))
|
||||||
|
context.hooks.add_hook(context.hooks.cmd_hook,
|
||||||
|
Hook(cmd_translate, "traduire"))
|
||||||
|
|
||||||
|
|
||||||
|
def cmd_translate(data, msg):
|
||||||
|
global LANG
|
||||||
startWord = 1
|
startWord = 1
|
||||||
if msg.cmd[startWord] in LANG:
|
if msg.cmd[startWord] in LANG:
|
||||||
langTo = msg.cmd[startWord]
|
langTo = msg.cmd[startWord]
|
||||||
startWord += 1
|
startWord += 1
|
||||||
else:
|
else:
|
||||||
langTo = "fr"
|
langTo = "fr"
|
||||||
if msg.cmd[startWord] in LANG:
|
if msg.cmd[startWord] in LANG:
|
||||||
langFrom = langTo
|
langFrom = langTo
|
||||||
langTo = msg.cmd[startWord]
|
langTo = msg.cmd[startWord]
|
||||||
startWord += 1
|
startWord += 1
|
||||||
else:
|
else:
|
||||||
if langTo == "en":
|
if langTo == "en":
|
||||||
langFrom = "fr"
|
langFrom = "fr"
|
||||||
else:
|
else:
|
||||||
langFrom = "en"
|
langFrom = "en"
|
||||||
|
|
||||||
(res, page) = getPage(' '.join(msg.cmd[startWord:]), langFrom, langTo)
|
(res, page) = getPage(' '.join(msg.cmd[startWord:]), langFrom, langTo)
|
||||||
if res == http.client.OK:
|
if res == http.client.OK:
|
||||||
wres = json.loads(page.decode())
|
wres = json.loads(page.decode())
|
||||||
if "Error" in wres:
|
if "Error" in wres:
|
||||||
msg.send_chn(wres["Note"])
|
msg.send_chn(wres["Note"])
|
||||||
else:
|
else:
|
||||||
start = "Traduction de %s : "%' '.join(msg.cmd[startWord:])
|
start = "Traduction de %s : "%' '.join(msg.cmd[startWord:])
|
||||||
if "Entries" in wres["term0"]:
|
if "Entries" in wres["term0"]:
|
||||||
if "SecondTranslation" in wres["term0"]["Entries"]["0"]:
|
if "SecondTranslation" in wres["term0"]["Entries"]["0"]:
|
||||||
msg.send_chn(start + wres["term0"]["Entries"]["0"]["FirstTranslation"]["term"] + " ; " + wres["term0"]["Entries"]["0"]["SecondTranslation"]["term"])
|
msg.send_chn(start +
|
||||||
else:
|
wres["term0"]["Entries"]["0"]["FirstTranslation"]["term"] +
|
||||||
msg.send_chn(start + wres["term0"]["Entries"]["0"]["FirstTranslation"]["term"])
|
" ; " +
|
||||||
elif "PrincipalTranslations" in wres["term0"]:
|
wres["term0"]["Entries"]["0"]["SecondTranslation"]["term"])
|
||||||
if "1" in wres["term0"]["PrincipalTranslations"]:
|
else:
|
||||||
msg.send_chn(start + wres["term0"]["PrincipalTranslations"]["0"]["FirstTranslation"]["term"] + " ; " + wres["term0"]["PrincipalTranslations"]["1"]["FirstTranslation"]["term"])
|
msg.send_chn(start +
|
||||||
else:
|
wres["term0"]["Entries"]["0"]["FirstTranslation"]["term"])
|
||||||
msg.send_chn(start + wres["term0"]["PrincipalTranslations"]["0"]["FirstTranslation"]["term"])
|
elif "PrincipalTranslations" in wres["term0"]:
|
||||||
else:
|
if "1" in wres["term0"]["PrincipalTranslations"]:
|
||||||
msg.send_chn("Une erreur s'est produite durant la recherche d'une traduction de %s" % ' '.join(msg.cmd[startWord:]))
|
msg.send_chn(start +
|
||||||
|
wres["term0"]["PrincipalTranslations"]["0"]["FirstTranslation"]["term"] +
|
||||||
|
" ; " +
|
||||||
|
wres["term0"]["PrincipalTranslations"]["1"]["FirstTranslation"]["term"])
|
||||||
|
else:
|
||||||
|
msg.send_chn(start +
|
||||||
|
wres["term0"]["PrincipalTranslations"]["0"]["FirstTranslation"]["term"])
|
||||||
|
else:
|
||||||
|
msg.send_chn("Une erreur s'est produite durant la recherche"
|
||||||
|
" d'une traduction de %s"
|
||||||
|
% ' '.join(msg.cmd[startWord:]))
|
||||||
return True
|
return True
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def getPage(terms, langfrom="fr", langto="en"):
|
def getPage(terms, langfrom="fr", langto="en"):
|
||||||
conn = http.client.HTTPConnection("api.wordreference.com", timeout=5)
|
conn = http.client.HTTPConnection("api.wordreference.com", timeout=5)
|
||||||
try:
|
try:
|
||||||
conn.request("GET", "/0.8/%s/json/%s%s/%s" % (CONF.getNode("wrapi")["key"], langfrom, langto, quote(terms)))
|
conn.request("GET", "/0.8/%s/json/%s%s/%s" % (
|
||||||
except socket.gaierror:
|
CONF.getNode("wrapi")["key"], langfrom, langto, quote(terms)))
|
||||||
print ("impossible de récupérer la page WordReference.")
|
except socket.gaierror:
|
||||||
return (http.client.INTERNAL_SERVER_ERROR, None)
|
print ("impossible de récupérer la page WordReference.")
|
||||||
except (TypeError, KeyError):
|
return (http.client.INTERNAL_SERVER_ERROR, None)
|
||||||
print ("You need a WordReference API key in order to use this module. Add it to the module configuration file:\n<wrapi key=\"XXXXX\" />\nRegister at http://www.wordreference.com/docs/APIregistration.aspx")
|
except (TypeError, KeyError):
|
||||||
return (http.client.INTERNAL_SERVER_ERROR, None)
|
print ("You need a WordReference API key in order to use this module."
|
||||||
|
" Add it to the module configuration file:\n<wrapi key=\"XXXXX\""
|
||||||
|
" />\nRegister at "
|
||||||
|
"http://www.wordreference.com/docs/APIregistration.aspx")
|
||||||
|
return (http.client.INTERNAL_SERVER_ERROR, None)
|
||||||
|
|
||||||
res = conn.getresponse()
|
res = conn.getresponse()
|
||||||
data = res.read()
|
data = res.read()
|
||||||
|
|
||||||
conn.close()
|
conn.close()
|
||||||
return (res.status, data)
|
return (res.status, data)
|
||||||
|
Loading…
Reference in New Issue
Block a user