WTE: Use a better, more stable API (uses source from stock Limnoria's Google plugin)

This commit is contained in:
GLolol 2014-12-05 15:54:06 -08:00
parent 19133d6888
commit 7ea511ce48

View File

@ -73,8 +73,15 @@ class WTE(callbacks.Plugin):
'cs', 'uk', 'ja', 'hr', 'kn', 'gl', 'mk', 'fa', 'sk', 'mn', 'es', 'cs', 'uk', 'ja', 'hr', 'kn', 'gl', 'mk', 'fa', 'sk', 'mn', 'es',
'ur', 'pl', 'eo', 'yo', 'en', 'yi') 'ur', 'pl', 'eo', 'yo', 'en', 'yi')
def _jsonRepair(self, data):
while ',,' in data:
data = data.replace(',,', ',null,')
while '[,' in data:
data = data.replace('[,', '[')
return data
def getTranslation(self, irc, sourceLang, targetLang, text): def getTranslation(self, irc, sourceLang, targetLang, text):
args = {"client": "p", "sl":sourceLang, "tl":targetLang} args = {"client": "t", "sl": sourceLang, "tl": targetLang}
if version_info[0] < 3: if version_info[0] < 3:
# Python 2's Unicode handling is just horrible. I've tried a # Python 2's Unicode handling is just horrible. I've tried a
# dozen different combinations of encoding and decoding and they # dozen different combinations of encoding and decoding and they
@ -84,17 +91,18 @@ class WTE(callbacks.Plugin):
args['q'] = filter(lambda x: x in printable, text) args['q'] = filter(lambda x: x in printable, text)
else: else:
args['q'] = text args['q'] = text
url = "http://translate.google.com/translate_a/t?"+ \ url = "https://translate.google.com/translate_a/t?"+ \
urlencode(args) urlencode(args)
self.log.debug("WTE: Using URL %s" % url) self.log.debug("WTE: Using URL %s" % url)
headers = {'User-Agent': ('Mozilla/5.0 (X11; Linux i586; rv:31.0) '
'Gecko/20100101 Firefox/31.0')}
try: try:
data = json.loads(utils.web.getUrl(url).decode("utf-8")) data = utils.web.getUrlFd(url, headers).read().decode("utf-8")
except utils.web.Error as e: except utils.web.Error as e:
irc.error(str(e), Raise=True) irc.error(str(e), Raise=True)
if "dict" in data: data = self._jsonRepair(data)
return data["dict"][0]["entry"][0]["word"] data = json.loads(data)
else: return ''.join(x[0] for x in data[0])
return data["sentences"][0]["trans"]
def wte(self, irc, msg, args, text): def wte(self, irc, msg, args, text):
"""wte <text> """wte <text>
@ -106,7 +114,7 @@ class WTE(callbacks.Plugin):
if outlang not in self.langs: if outlang not in self.langs:
irc.error("Unrecognized output language. Please set " irc.error("Unrecognized output language. Please set "
"'config plugins.wte.language' correctly.", Raise=True) "'config plugins.wte.language' correctly.", Raise=True)
ll = random.sample(self.langs, random.randint(6,12)) ll = random.sample(self.langs, random.randint(5,10))
self.log.debug(format("WTE: Using %i languages: %L " self.log.debug(format("WTE: Using %i languages: %L "
"(outlang %s)", len(ll), ll, outlang)) "(outlang %s)", len(ll), ll, outlang))
for targetlang in ll: for targetlang in ll: