Merge branch 'master' of https://github.com/sbp/phenny
commit
ee6ce7ba3e
|
@ -282,6 +282,7 @@ tock.priority = 'high'
|
||||||
|
|
||||||
def npl(phenny, input):
|
def npl(phenny, input):
|
||||||
"""Shows the time from NPL's SNTP server."""
|
"""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 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
client.sendto('\x1b' + 47 * '\0', ('ntp1.npl.co.uk', 123))
|
client.sendto('\x1b' + 47 * '\0', ('ntp1.npl.co.uk', 123))
|
||||||
data, address = client.recvfrom(1024)
|
data, address = client.recvfrom(1024)
|
||||||
|
@ -290,7 +291,7 @@ def npl(phenny, input):
|
||||||
d = dec('0.0')
|
d = dec('0.0')
|
||||||
for i in range(8):
|
for i in range(8):
|
||||||
d += dec(buf[32 + i]) * dec(str(math.pow(2, (3 - i) * 8)))
|
d += dec(buf[32 + i]) * dec(str(math.pow(2, (3 - i) * 8)))
|
||||||
d -= dec(2208988800)
|
d -= dec(2208988800L)
|
||||||
a, b = str(d).split('.')
|
a, b = str(d).split('.')
|
||||||
f = '%Y-%m-%d %H:%M:%S'
|
f = '%Y-%m-%d %H:%M:%S'
|
||||||
result = datetime.datetime.fromtimestamp(d).strftime(f) + '.' + b[:6]
|
result = datetime.datetime.fromtimestamp(d).strftime(f) + '.' + b[:6]
|
||||||
|
|
|
@ -20,12 +20,14 @@ class Grab(web.urllib.request.URLopener):
|
||||||
|
|
||||||
def google_ajax(query):
|
def google_ajax(query):
|
||||||
"""Search using AjaxSearch, and return its JSON."""
|
"""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'
|
uri = 'http://ajax.googleapis.com/ajax/services/search/web'
|
||||||
args = '?v=1.0&safe=off&q=' + web.quote(query)
|
args = '?v=1.0&safe=off&q=' + web.urllib.quote(query)
|
||||||
handler = web.urllib.request._urlopener
|
handler = web.urllib._urlopener
|
||||||
web.urllib.request._urlopener = Grab()
|
web.urllib._urlopener = Grab()
|
||||||
bytes = web.get(uri + args)
|
bytes = web.get(uri + args)
|
||||||
web.urllib.request._urlopener = handler
|
web.urllib._urlopener = handler
|
||||||
return web.json(bytes)
|
return web.json(bytes)
|
||||||
|
|
||||||
def google_search(query):
|
def google_search(query):
|
||||||
|
|
|
@ -7,6 +7,51 @@ Licensed under the Eiffel Forum License 2.
|
||||||
http://inamidst.com/phenny/
|
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):
|
def startup(phenny, input):
|
||||||
if hasattr(phenny.config, 'serverpass'):
|
if hasattr(phenny.config, 'serverpass'):
|
||||||
phenny.write(('PASS', phenny.config.serverpass))
|
phenny.write(('PASS', phenny.config.serverpass))
|
||||||
|
|
|
@ -13,13 +13,17 @@ import json
|
||||||
import web
|
import web
|
||||||
|
|
||||||
def translate(text, input='auto', output='en'):
|
def translate(text, input='auto', output='en'):
|
||||||
|
raw = False
|
||||||
|
if output.endswith('-raw'):
|
||||||
|
output = output[:-4]
|
||||||
|
raw = True
|
||||||
|
|
||||||
opener = urllib.request.build_opener()
|
opener = urllib.request.build_opener()
|
||||||
opener.addheaders = [(
|
opener.addheaders = [(
|
||||||
'User-Agent', 'Mozilla/5.0' +
|
'User-Agent', 'Mozilla/5.0' +
|
||||||
'(X11; U; Linux i686)' +
|
'(X11; U; Linux i686)' +
|
||||||
'Gecko/20071127 Firefox/2.0.0.11'
|
'Gecko/20071127 Firefox/2.0.0.11'
|
||||||
)]
|
)]
|
||||||
|
|
||||||
input, output = urllib.parse.quote(input), urllib.parse.quote(output)
|
input, output = urllib.parse.quote(input), urllib.parse.quote(output)
|
||||||
text = urllib.parse.quote(text)
|
text = urllib.parse.quote(text)
|
||||||
|
|
||||||
|
@ -32,7 +36,10 @@ def translate(text, input='auto', output='en'):
|
||||||
result = result.replace(',,', ',null,')
|
result = result.replace(',,', ',null,')
|
||||||
data = json.loads(result)
|
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 = '?'
|
except: language = '?'
|
||||||
|
|
||||||
return ''.join(x[0] for x in data[0]), language
|
return ''.join(x[0] for x in data[0]), language
|
||||||
|
@ -61,10 +68,46 @@ def tr(phenny, context):
|
||||||
phenny.reply(msg)
|
phenny.reply(msg)
|
||||||
else: phenny.reply('Language guessing failed, so try suggesting one!')
|
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.example = '$nickname: "mon chien"? or $nickname: fr "mon chien"?'
|
||||||
tr.priority = 'low'
|
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):
|
def mangle(phenny, input):
|
||||||
phrase = input.group(2)
|
phrase = input.group(2)
|
||||||
for lang in ['fr', 'de', 'es', 'it', 'ja']:
|
for lang in ['fr', 'de', 'es', 'it', 'ja']:
|
||||||
|
|
Loading…
Reference in New Issue