From e3633957a9f115da07127fe97c629bb362b2af58 Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Wed, 11 Jan 2012 14:18:34 +0000 Subject: [PATCH] New .tr syntax for translations --- modules/clock.py | 1 + modules/search.py | 2 ++ modules/translate.py | 48 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/modules/clock.py b/modules/clock.py index f848423..91f2d5b 100755 --- a/modules/clock.py +++ b/modules/clock.py @@ -280,6 +280,7 @@ tock.priority = 'high' def npl(phenny, input): """Shows the time from NPL's SNTP server.""" + # for server in ('ntp1.npl.co.uk', 'ntp2.npl.co.uk'): client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) client.sendto('\x1b' + 47 * '\0', ('ntp1.npl.co.uk', 123)) data, address = client.recvfrom(1024) diff --git a/modules/search.py b/modules/search.py index bfc50bd..13fadbb 100755 --- a/modules/search.py +++ b/modules/search.py @@ -20,6 +20,8 @@ class Grab(web.urllib.URLopener): def google_ajax(query): """Search using AjaxSearch, and return its JSON.""" + if isinstance(query, unicode): + query = query.encode('utf-8') uri = 'http://ajax.googleapis.com/ajax/services/search/web' args = '?v=1.0&safe=off&q=' + web.urllib.quote(query) handler = web.urllib._urlopener diff --git a/modules/translate.py b/modules/translate.py index 968cd63..ec59db0 100755 --- a/modules/translate.py +++ b/modules/translate.py @@ -12,6 +12,11 @@ import re, urllib import web def translate(text, input='auto', output='en'): + raw = False + if output.endswith('-raw'): + output = output[:-4] + raw = True + import urllib2, json opener = urllib2.build_opener() opener.addheaders = [( @@ -31,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 +69,46 @@ def tr(phenny, context): phenny.reply(msg) else: phenny.reply('Language guessing failed, so try suggesting one!') -tr.rule = ('$nick', ur'(?:([a-z]{2}) +)?(?:([a-z]{2}) +)?["“](.+?)["”]\? *$') +tr.rule = ('$nick', ur'(?:([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 context.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).encode('utf-8') for lang in ['fr', 'de', 'es', 'it', 'ja']: