Commit 2b94b8b0 authored by Sébastien Dufromentel's avatar Sébastien Dufromentel
Browse files

Nickname improvements + & unescape.

parent 64bfd2af
......@@ -22,6 +22,7 @@
######
import re
import cgi
import ssl
import json
import signal
......@@ -57,13 +58,17 @@ def translate(message, target):
if message == "\o/":
return message
num, words, repls = 1, [], {}
for word in re.split("(\W+)", message):
for word in re.split("([a-zA-Z0-9_\[\]\-]+)", message):
if irc.strings.IRCFoldedCase(word) in bots.keys():
repls["*"+str(num)] = usernick(word, target)
words.append("*"+str(num))
num += 1
nick = usernick(word, target)
if nick is None: # Don't translate.
words.append(word)
else: # Okay, let's tranlate it.
repls["*"+str(num)] = nick
words.append("*"+str(num))
num += 1
else: # The opposite?
nick = botnick(word, target)
nick = botnick(word, "fr" if target == "en" else "en")
if nick in bots.keys():
repls["*"+str(num)] = nick
words.append("*"+str(num))
......@@ -74,7 +79,7 @@ def translate(message, target):
try: # The server may have troubles to translate.
with urllib.request.urlopen(trans, urllib.parse.urlencode({ "source":
"auto", "target": target, "q": message }).encode()) as f:
message = json.loads(f.read())["translatedText"]
message = cgi.html.unescape(json.loads(f.read())["translatedText"])
except Exception: # Probably some 500.
pass # Does it help to report it?
for key, repl in repls.items():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment