Merge branch 'master' of https://github.com/sbp/phenny
This commit is contained in:
@@ -13,13 +13,17 @@ import json
|
||||
import web
|
||||
|
||||
def translate(text, input='auto', output='en'):
|
||||
raw = False
|
||||
if output.endswith('-raw'):
|
||||
output = output[:-4]
|
||||
raw = True
|
||||
|
||||
opener = urllib.request.build_opener()
|
||||
opener.addheaders = [(
|
||||
'User-Agent', 'Mozilla/5.0' +
|
||||
'(X11; U; Linux i686)' +
|
||||
'Gecko/20071127 Firefox/2.0.0.11'
|
||||
)]
|
||||
|
||||
input, output = urllib.parse.quote(input), urllib.parse.quote(output)
|
||||
text = urllib.parse.quote(text)
|
||||
|
||||
@@ -32,7 +36,10 @@ def translate(text, input='auto', output='en'):
|
||||
result = result.replace(',,', ',null,')
|
||||
data = json.loads(result)
|
||||
|
||||
try: language = data[-2][0][0]
|
||||
if raw:
|
||||
return str(data), 'en-raw'
|
||||
|
||||
try: language = data[2] # -2][0][0]
|
||||
except: language = '?'
|
||||
|
||||
return ''.join(x[0] for x in data[0]), language
|
||||
@@ -61,10 +68,46 @@ def tr(phenny, context):
|
||||
phenny.reply(msg)
|
||||
else: phenny.reply('Language guessing failed, so try suggesting one!')
|
||||
|
||||
tr.rule = ('$nick', r'(?:([a-z]{2}) +)?(?:([a-z]{2}) +)?["“](.+?)["”]\? *$')
|
||||
tr.rule = ('$nick', r'(?:([a-z]{2}) +)?(?:([a-z]{2}|en-raw) +)?["“](.+?)["”]\? *$')
|
||||
tr.example = '$nickname: "mon chien"? or $nickname: fr "mon chien"?'
|
||||
tr.priority = 'low'
|
||||
|
||||
def tr2(phenny, input):
|
||||
"""Translates a phrase, with an optional language hint."""
|
||||
command = input.group(2).encode('utf-8')
|
||||
|
||||
def langcode(p):
|
||||
return p.startswith(':') and (2 < len(p) < 10) and p[1:].isalpha()
|
||||
|
||||
args = ['auto', 'en']
|
||||
|
||||
for i in xrange(2):
|
||||
if not ' ' in command: break
|
||||
prefix, cmd = command.split(' ', 1)
|
||||
if langcode(prefix):
|
||||
args[i] = prefix[1:]
|
||||
command = cmd
|
||||
phrase = command
|
||||
|
||||
if (len(phrase) > 350) and (not input.admin):
|
||||
return phenny.reply('Phrase must be under 350 characters.')
|
||||
|
||||
src, dest = args
|
||||
if src != dest:
|
||||
msg, src = translate(phrase, src, dest)
|
||||
if 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, sorry!' % (src, dest)
|
||||
|
||||
phenny.reply(msg)
|
||||
else: phenny.reply('Language guessing failed, so try suggesting one!')
|
||||
|
||||
tr2.commands = ['tr']
|
||||
tr2.priority = 'low'
|
||||
|
||||
def mangle(phenny, input):
|
||||
phrase = input.group(2)
|
||||
for lang in ['fr', 'de', 'es', 'it', 'ja']:
|
||||
|
||||
Reference in New Issue
Block a user