2008-02-21 07:06:33 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# coding=utf-8
|
|
|
|
"""
|
|
|
|
translate.py - Phenny Translation Module
|
|
|
|
Copyright 2008, Sean B. Palmer, inamidst.com
|
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
|
|
|
"""
|
|
|
|
|
2011-09-22 14:17:27 -04:00
|
|
|
import re, urllib.request, urllib.parse, urllib.error
|
2011-12-27 12:18:05 -05:00
|
|
|
import json
|
2008-02-21 07:06:33 -05:00
|
|
|
import web
|
|
|
|
|
2011-12-27 09:48:24 -05:00
|
|
|
def translate(text, input='auto', output='en'):
|
2011-12-27 12:18:05 -05:00
|
|
|
opener = urllib.request.build_opener()
|
2011-12-27 09:48:24 -05:00
|
|
|
opener.addheaders = [(
|
|
|
|
'User-Agent', 'Mozilla/5.0' +
|
|
|
|
'(X11; U; Linux i686)' +
|
|
|
|
'Gecko/20071127 Firefox/2.0.0.11'
|
|
|
|
)]
|
|
|
|
|
2011-12-27 12:18:05 -05:00
|
|
|
input, output = urllib.parse.quote(input), urllib.parse.quote(output)
|
|
|
|
text = urllib.parse.quote(text)
|
2011-12-27 09:48:24 -05:00
|
|
|
|
|
|
|
result = opener.open('http://translate.google.com/translate_a/t?' +
|
|
|
|
('client=t&hl=en&sl=%s&tl=%s&multires=1' % (input, output)) +
|
|
|
|
('&otf=1&ssel=0&tsel=0&uptl=en&sc=1&text=%s' % text)).read()
|
2011-12-27 12:18:05 -05:00
|
|
|
result = result.decode('utf-8')
|
2011-12-27 09:48:24 -05:00
|
|
|
|
|
|
|
while ',,' in result:
|
|
|
|
result = result.replace(',,', ',null,')
|
|
|
|
data = json.loads(result)
|
|
|
|
|
|
|
|
try: language = data[-2][0][0]
|
|
|
|
except: language = '?'
|
|
|
|
|
|
|
|
return ''.join(x[0] for x in data[0]), language
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2008-05-23 14:16:38 -04:00
|
|
|
def tr(phenny, context):
|
2008-02-23 07:16:43 -05:00
|
|
|
"""Translates a phrase, with an optional language hint."""
|
2008-05-23 14:16:38 -04:00
|
|
|
input, output, phrase = context.groups()
|
|
|
|
|
2011-09-22 14:17:27 -04:00
|
|
|
phrase = phrase
|
2008-05-23 14:16:38 -04:00
|
|
|
|
|
|
|
if (len(phrase) > 350) and (not context.admin):
|
2008-02-21 07:06:33 -05:00
|
|
|
return phenny.reply('Phrase must be under 350 characters.')
|
|
|
|
|
2011-12-27 09:48:24 -05:00
|
|
|
input = input or 'auto'
|
2008-02-29 10:36:18 -05:00
|
|
|
input = input.encode('utf-8')
|
|
|
|
output = (output or 'en').encode('utf-8')
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2008-05-23 14:16:38 -04:00
|
|
|
if input != output:
|
2011-12-27 09:50:21 -05:00
|
|
|
msg, input = translate(phrase, input, output)
|
2011-12-27 12:18:05 -05:00
|
|
|
output = output.decode('utf-8')
|
2008-05-31 06:46:48 -04:00
|
|
|
if msg:
|
2010-11-13 06:55:04 -05:00
|
|
|
msg = web.decode(msg) # msg.replace(''', "'")
|
2008-05-31 06:46:48 -04:00
|
|
|
msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output)
|
|
|
|
else: msg = 'The %s to %s translation failed, sorry!' % (input, output)
|
|
|
|
|
2008-05-23 14:16:38 -04:00
|
|
|
phenny.reply(msg)
|
2008-06-19 13:58:24 -04:00
|
|
|
else: phenny.reply('Language guessing failed, so try suggesting one!')
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2011-09-22 14:17:27 -04:00
|
|
|
tr.rule = ('$nick', r'(?:([a-z]{2}) +)?(?:([a-z]{2}) +)?["“](.+?)["”]\? *$')
|
2008-02-23 07:16:43 -05:00
|
|
|
tr.example = '$nickname: "mon chien"? or $nickname: fr "mon chien"?'
|
2008-02-21 07:06:33 -05:00
|
|
|
tr.priority = 'low'
|
|
|
|
|
2008-05-31 06:46:48 -04:00
|
|
|
def mangle(phenny, input):
|
2011-09-22 14:17:27 -04:00
|
|
|
phrase = input.group(2)
|
2008-05-31 06:46:48 -04:00
|
|
|
for lang in ['fr', 'de', 'es', 'it', 'ja']:
|
|
|
|
backup = phrase
|
|
|
|
phrase = translate(phrase, 'en', lang)
|
|
|
|
if not phrase:
|
|
|
|
phrase = backup
|
|
|
|
break
|
|
|
|
__import__('time').sleep(0.5)
|
|
|
|
|
|
|
|
backup = phrase
|
|
|
|
phrase = translate(phrase, lang, 'en')
|
|
|
|
if not phrase:
|
|
|
|
phrase = backup
|
|
|
|
break
|
|
|
|
__import__('time').sleep(0.5)
|
|
|
|
|
|
|
|
phenny.reply(phrase or 'ERRORS SRY')
|
|
|
|
mangle.commands = ['mangle']
|
|
|
|
|
2008-02-21 07:06:33 -05:00
|
|
|
if __name__ == '__main__':
|
2011-09-22 14:17:27 -04:00
|
|
|
print(__doc__.strip())
|