From e3633957a9f115da07127fe97c629bb362b2af58 Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Wed, 11 Jan 2012 14:18:34 +0000 Subject: [PATCH 1/3] 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']: From 25755360786dcafc798d68f147471de0bbfb1056 Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Thu, 12 Jan 2012 14:21:49 +0000 Subject: [PATCH 2/3] Added clsn's ping-pong code --- modules/startup.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/modules/startup.py b/modules/startup.py index 6fc7fae..e650170 100755 --- a/modules/startup.py +++ b/modules/startup.py @@ -7,6 +7,51 @@ Licensed under the Eiffel Forum License 2. http://inamidst.com/phenny/ """ +import threading, time + +def setup(phenny): + # by clsn + phenny.data = {} + refresh_delay = 300.0 + + if hasattr(phenny.config, 'refresh_delay'): + try: refresh_delay = float(phenny.config.refresh_delay) + except: pass + + def close(): + print "Nobody PONGed our PING, restarting" + phenny.handle_close() + + def pingloop(): + timer = threading.Timer(refresh_delay, close, ()) + phenny.data['startup.setup.timer'] = timer + phenny.data['startup.setup.timer'].start() + # print "PING!" + phenny.write(('PING', phenny.config.host)) + phenny.data['startup.setup.pingloop'] = pingloop + + def pong(phenny, input): + try: + # print "PONG!" + phenny.data['startup.setup.timer'].cancel() + time.sleep(refresh_delay + 60.0) + pingloop() + except: pass + pong.event = 'PONG' + pong.thread = True + pong.rule = r'.*' + phenny.variables['pong'] = pong + + # Need to wrap handle_connect to start the loop. + inner_handle_connect = phenny.handle_connect + + def outer_handle_connect(): + inner_handle_connect() + if phenny.data.get('startup.setup.pingloop'): + phenny.data['startup.setup.pingloop']() + + phenny.handle_connect = outer_handle_connect + def startup(phenny, input): if hasattr(phenny.config, 'serverpass'): phenny.write(('PASS', phenny.config.serverpass)) From 436d610ff44a66443aa9417ada7de1fd386e0f6c Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Sat, 14 Jan 2012 22:55:59 +0000 Subject: [PATCH 3/3] Making the PING-loop optional --- modules/startup.py | 56 ++++++++++++++++++++++---------------------- modules/translate.py | 2 +- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/modules/startup.py b/modules/startup.py index e650170..48c5fc9 100755 --- a/modules/startup.py +++ b/modules/startup.py @@ -18,39 +18,39 @@ def setup(phenny): try: refresh_delay = float(phenny.config.refresh_delay) except: pass - def close(): - print "Nobody PONGed our PING, restarting" - phenny.handle_close() + def close(): + print "Nobody PONGed our PING, restarting" + phenny.handle_close() - def pingloop(): - timer = threading.Timer(refresh_delay, close, ()) - phenny.data['startup.setup.timer'] = timer - phenny.data['startup.setup.timer'].start() - # print "PING!" - phenny.write(('PING', phenny.config.host)) - phenny.data['startup.setup.pingloop'] = pingloop + def pingloop(): + timer = threading.Timer(refresh_delay, close, ()) + phenny.data['startup.setup.timer'] = timer + phenny.data['startup.setup.timer'].start() + # print "PING!" + phenny.write(('PING', phenny.config.host)) + phenny.data['startup.setup.pingloop'] = pingloop - def pong(phenny, input): - try: - # print "PONG!" - phenny.data['startup.setup.timer'].cancel() - time.sleep(refresh_delay + 60.0) - pingloop() - except: pass - pong.event = 'PONG' - pong.thread = True - pong.rule = r'.*' - phenny.variables['pong'] = pong + def pong(phenny, input): + try: + # print "PONG!" + phenny.data['startup.setup.timer'].cancel() + time.sleep(refresh_delay + 60.0) + pingloop() + except: pass + pong.event = 'PONG' + pong.thread = True + pong.rule = r'.*' + phenny.variables['pong'] = pong - # Need to wrap handle_connect to start the loop. - inner_handle_connect = phenny.handle_connect + # Need to wrap handle_connect to start the loop. + inner_handle_connect = phenny.handle_connect - def outer_handle_connect(): - inner_handle_connect() - if phenny.data.get('startup.setup.pingloop'): - phenny.data['startup.setup.pingloop']() + def outer_handle_connect(): + inner_handle_connect() + if phenny.data.get('startup.setup.pingloop'): + phenny.data['startup.setup.pingloop']() - phenny.handle_connect = outer_handle_connect + phenny.handle_connect = outer_handle_connect def startup(phenny, input): if hasattr(phenny.config, 'serverpass'): diff --git a/modules/translate.py b/modules/translate.py index ec59db0..94e5f64 100755 --- a/modules/translate.py +++ b/modules/translate.py @@ -90,7 +90,7 @@ def tr2(phenny, input): command = cmd phrase = command - if (len(phrase) > 350) and (not context.admin): + if (len(phrase) > 350) and (not input.admin): return phenny.reply('Phrase must be under 350 characters.') src, dest = args