2017-11-22 19:26:40 -05:00
|
|
|
# coding=utf-8
|
|
|
|
"""
|
|
|
|
translate.py - Sopel Translation Module
|
|
|
|
Copyright 2008, Sean B. Palmer, inamidst.com
|
|
|
|
Copyright © 2013-2014, Elad Alfassa <elad@fedoraproject.org>
|
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://sopel.chat
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
import random
|
|
|
|
import sys
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from module import rule, commands, priority, example
|
|
|
|
|
|
|
|
mangle_lines = {}
|
|
|
|
if sys.version_info.major >= 3:
|
2017-11-26 13:54:54 -05:00
|
|
|
unicode = str
|
2017-11-22 19:26:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
def translate(text, in_lang='auto', out_lang='en', verify_ssl=True):
|
2017-11-26 13:54:54 -05:00
|
|
|
raw = False
|
|
|
|
if unicode(out_lang).endswith('-raw'):
|
|
|
|
out_lang = out_lang[:-4]
|
|
|
|
raw = True
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'User-Agent': 'Mozilla/5.0' +
|
|
|
|
'(X11; U; Linux i686)' +
|
|
|
|
'Gecko/20071127 Firefox/2.0.0.11'
|
|
|
|
}
|
|
|
|
|
|
|
|
query = {
|
|
|
|
"client": "gtx",
|
|
|
|
"sl": in_lang,
|
|
|
|
"tl": out_lang,
|
|
|
|
"dt": "t",
|
|
|
|
"q": text,
|
|
|
|
}
|
|
|
|
url = "http://translate.googleapis.com/translate_a/single"
|
|
|
|
result = requests.get(url, params=query, timeout=40, headers=headers,
|
|
|
|
verify=verify_ssl).text
|
|
|
|
|
|
|
|
if result == '[,,""]':
|
|
|
|
return None, in_lang
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
while ',,' in result:
|
|
|
|
result = result.replace(',,', ',null,')
|
|
|
|
result = result.replace('[,', '[null,')
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
data = json.loads(result)
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
if raw:
|
|
|
|
return str(data), 'en-raw'
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
try:
|
|
|
|
language = data[2] # -2][0][0]
|
|
|
|
except:
|
|
|
|
language = '?'
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
return ''.join(x[0] for x in data[0]), language
|
2017-11-22 19:26:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
@commands('translate', 'tr')
|
|
|
|
@example('.tr :en :fr my dog', '"mon chien" (en to fr, translate.google.com)')
|
|
|
|
@example('.tr היי', '"Hey" (iw to en, translate.google.com)')
|
|
|
|
@example('.tr mon chien', '"my dog" (fr to en, translate.google.com)')
|
|
|
|
def tr2(bot, trigger):
|
2017-11-26 13:54:54 -05:00
|
|
|
"""Translates a phrase, with an optional language hint."""
|
|
|
|
command = trigger.group(2)
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
if not command:
|
|
|
|
return bot.reply('You did not give me anything to translate')
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
def langcode(p):
|
|
|
|
return p.startswith(':') and (2 < len(p) < 10) and p[1:].isalpha()
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
args = ['auto', 'en']
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
for i in range(2):
|
|
|
|
if ' ' not in command:
|
|
|
|
break
|
|
|
|
prefix, cmd = command.split(' ', 1)
|
|
|
|
if langcode(prefix):
|
|
|
|
args[i] = prefix[1:]
|
|
|
|
command = cmd
|
|
|
|
phrase = command
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
if (len(phrase) > 350) and (not trigger.admin):
|
|
|
|
return bot.reply('Phrase must be under 350 characters.')
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
if phrase.strip() == '':
|
|
|
|
return bot.reply('You need to specify a string for me to translate!')
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
src, dest = args
|
|
|
|
if src != dest:
|
|
|
|
msg, src = translate(phrase, src, dest,
|
|
|
|
verify_ssl=bot.config.core.verify_ssl)
|
|
|
|
if sys.version_info.major < 3 and isinstance(msg, str):
|
|
|
|
msg = msg.decode('utf-8')
|
|
|
|
if msg:
|
|
|
|
#msg = web.decode(msg) # msg.replace(''', "'")
|
|
|
|
msg = '"%s" (%s to %s, translate.google.com)' % (msg, src, dest)
|
|
|
|
else:
|
|
|
|
msg = 'The %s to %s translation failed, are you sure you specified valid language abbreviations?' % (src, dest)
|
2017-11-22 19:26:40 -05:00
|
|
|
|
2017-11-26 13:54:54 -05:00
|
|
|
bot.reply(msg)
|
|
|
|
else:
|
|
|
|
bot.reply('Language guessing failed, so try suggesting one!')
|
2017-11-22 19:26:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
def get_random_lang(long_list, short_list):
|
2017-11-26 13:54:54 -05:00
|
|
|
random_index = random.randint(0, len(long_list) - 1)
|
|
|
|
random_lang = long_list[random_index]
|
|
|
|
if random_lang not in short_list:
|
|
|
|
short_list.append(random_lang)
|
|
|
|
else:
|
|
|
|
return get_random_lang(long_list, short_list)
|
|
|
|
return short_list
|
2017-11-22 19:26:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
@commands('mangle', 'mangle2')
|
|
|
|
def mangle(bot, trigger):
|
2017-11-26 13:54:54 -05:00
|
|
|
"""Repeatedly translate the input until it makes absolutely no sense."""
|
|
|
|
verify_ssl = bot.config.core.verify_ssl
|
|
|
|
global mangle_lines
|
|
|
|
long_lang_list = ['fr', 'de', 'es', 'it', 'no', 'he', 'la', 'ja', 'cy', 'ar', 'yi', 'zh', 'nl', 'ru', 'fi', 'hi', 'af', 'jw', 'mr', 'ceb', 'cs', 'ga', 'sv', 'eo', 'el', 'ms', 'lv']
|
|
|
|
lang_list = []
|
|
|
|
for __ in range(0, 8):
|
|
|
|
lang_list = get_random_lang(long_lang_list, lang_list)
|
|
|
|
random.shuffle(lang_list)
|
|
|
|
if trigger.group(2) is None:
|
|
|
|
try:
|
|
|
|
phrase = (mangle_lines[trigger.sender.lower()], '')
|
|
|
|
except:
|
|
|
|
bot.reply("What do you want me to mangle?")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
phrase = (trigger.group(2).strip(), '')
|
|
|
|
if phrase[0] == '':
|
|
|
|
bot.reply("What do you want me to mangle?")
|
|
|
|
return
|
|
|
|
for lang in lang_list:
|
|
|
|
backup = phrase
|
|
|
|
try:
|
|
|
|
phrase = translate(phrase[0], 'en', lang,
|
|
|
|
verify_ssl=verify_ssl)
|
|
|
|
except:
|
|
|
|
phrase = False
|
|
|
|
if not phrase:
|
|
|
|
phrase = backup
|
|
|
|
break
|
|
|
|
|
|
|
|
try:
|
|
|
|
phrase = translate(phrase[0], lang, 'en', verify_ssl=verify_ssl)
|
|
|
|
except:
|
|
|
|
phrase = backup
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not phrase:
|
|
|
|
phrase = backup
|
|
|
|
break
|
|
|
|
bot.reply(phrase[0])
|
2017-11-22 19:26:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
@rule('(.*)')
|
|
|
|
@priority('low')
|
|
|
|
def collect_mangle_lines(bot, trigger):
|
2017-11-26 13:54:54 -05:00
|
|
|
global mangle_lines
|
|
|
|
mangle_lines[trigger.sender.lower()] = "%s said '%s'" % (trigger.nick, (trigger.group(0).strip()))
|
2017-11-22 19:26:40 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2017-11-26 13:54:54 -05:00
|
|
|
from test_tools import run_example_tests
|
|
|
|
run_example_tests(__file__)
|