Services fixes, and a standard JSON API.
parent
ca95157acb
commit
5ebe01386d
|
@ -40,26 +40,31 @@ def service(phenny, input, command, args):
|
||||||
return phenny.reply('Sorry, the service is broken.')
|
return phenny.reply('Sorry, the service is broken.')
|
||||||
phenny.say(lines[0][:350])
|
phenny.say(lines[0][:350])
|
||||||
|
|
||||||
def o(phenny, input):
|
def refresh(phenny):
|
||||||
"""Call a webservice."""
|
|
||||||
text = input.group(2)
|
|
||||||
if hasattr(phenny.config, 'services'):
|
if hasattr(phenny.config, 'services'):
|
||||||
services = phenny.config.services
|
services = phenny.config.services
|
||||||
else: services = definitions
|
else: services = definitions
|
||||||
|
|
||||||
|
old = o.services
|
||||||
|
o.serviceURI = services
|
||||||
|
o.services = mappings(o.serviceURI)
|
||||||
|
return len(o.services), set(o.services) - set(old)
|
||||||
|
|
||||||
|
def o(phenny, input):
|
||||||
|
"""Call a webservice."""
|
||||||
|
text = input.group(2)
|
||||||
|
|
||||||
if (not o.services) or (text == 'refresh'):
|
if (not o.services) or (text == 'refresh'):
|
||||||
old = o.services
|
length, added = refresh(phenny)
|
||||||
o.services = mappings(services)
|
|
||||||
if text == 'refresh':
|
if text == 'refresh':
|
||||||
msg = 'Okay, found %s services.' % len(o.services)
|
msg = 'Okay, found %s services.' % length
|
||||||
added = set(o.services) - set(old)
|
|
||||||
if added:
|
if added:
|
||||||
msg += ' Added: ' + ', '.join(sorted(added)[:5])
|
msg += ' Added: ' + ', '.join(sorted(added)[:5])
|
||||||
if len(added) > 5: msg += ', &c.'
|
if len(added) > 5: msg += ', &c.'
|
||||||
return phenny.reply(msg)
|
return phenny.reply(msg)
|
||||||
|
|
||||||
if not text:
|
if not text:
|
||||||
return phenny.reply('Try %s for details.' % services)
|
return phenny.reply('Try %s for details.' % o.serviceURI)
|
||||||
|
|
||||||
if ' ' in text:
|
if ' ' in text:
|
||||||
command, args = text.split(' ', 1)
|
command, args = text.split(' ', 1)
|
||||||
|
@ -71,7 +76,7 @@ def o(phenny, input):
|
||||||
return phenny.reply(msg)
|
return phenny.reply(msg)
|
||||||
|
|
||||||
if not o.services.has_key(command):
|
if not o.services.has_key(command):
|
||||||
return phenny.reply('Sorry, no such service. See %s' % services)
|
return phenny.reply('Sorry, no such service. See %s' % o.serviceURI)
|
||||||
|
|
||||||
if hasattr(phenny.config, 'external'):
|
if hasattr(phenny.config, 'external'):
|
||||||
default = phenny.config.external.get('*')
|
default = phenny.config.external.get('*')
|
||||||
|
@ -86,13 +91,20 @@ def o(phenny, input):
|
||||||
o.commands = ['o']
|
o.commands = ['o']
|
||||||
o.example = '.o servicename arg1 arg2 arg3'
|
o.example = '.o servicename arg1 arg2 arg3'
|
||||||
o.services = {}
|
o.services = {}
|
||||||
|
o.serviceURI = None
|
||||||
|
|
||||||
def snippet(phenny, input):
|
def snippet(phenny, input):
|
||||||
|
if not o.services:
|
||||||
|
refresh(phenny)
|
||||||
|
|
||||||
|
search = urllib.quote(input.group(2).encode('utf-8'))
|
||||||
py = "BeautifulSoup.BeautifulSoup(re.sub('<.*?>|(?<= ) +', '', " + \
|
py = "BeautifulSoup.BeautifulSoup(re.sub('<.*?>|(?<= ) +', '', " + \
|
||||||
|
"''.join(chr(ord(c)) for c in " + \
|
||||||
"eval(urllib.urlopen('http://ajax.googleapis.com/ajax/serv" + \
|
"eval(urllib.urlopen('http://ajax.googleapis.com/ajax/serv" + \
|
||||||
"ices/search/web?v=1.0&q=" + urllib.quote(input.group(2)) + \
|
"ices/search/web?v=1.0&q=" + search + "').read()" + \
|
||||||
"').read().replace('null', 'None'))['responseData']['resul" + \
|
".replace('null', 'None'))['responseData']['resul" + \
|
||||||
"ts'][0]['content'].decode('unicode-escape')), convertEntities=True)"
|
"ts'][0]['content'].decode('unicode-escape')).replace(" + \
|
||||||
|
"'"', '\x22')), convertEntities=True)"
|
||||||
service(phenny, input, 'py', py)
|
service(phenny, input, 'py', py)
|
||||||
snippet.commands = ['snippet']
|
snippet.commands = ['snippet']
|
||||||
|
|
||||||
|
|
|
@ -10,23 +10,12 @@ http://inamidst.com/phenny/
|
||||||
import re
|
import re
|
||||||
import web
|
import web
|
||||||
|
|
||||||
r_string = re.compile(r'("(\\.|[^"\\])*")')
|
|
||||||
r_json = re.compile(r'^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$')
|
|
||||||
env = {'__builtins__': None, 'null': None, 'true': True, 'false': False}
|
|
||||||
|
|
||||||
def json(text):
|
|
||||||
"""Evaluate JSON text safely (we hope)."""
|
|
||||||
if r_json.match(r_string.sub('', text)):
|
|
||||||
text = r_string.sub(lambda m: 'u' + m.group(1), text)
|
|
||||||
return eval(text.strip(' \t\r\n'), env, {})
|
|
||||||
raise ValueError('Input must be serialised JSON.')
|
|
||||||
|
|
||||||
def search(query):
|
def search(query):
|
||||||
"""Search using AjaxSearch, and return its JSON."""
|
"""Search using AjaxSearch, and return its JSON."""
|
||||||
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.urllib.quote(query.encode('utf-8'))
|
args = '?v=1.0&safe=off&q=' + web.urllib.quote(query.encode('utf-8'))
|
||||||
bytes = web.get(uri + args)
|
bytes = web.get(uri + args)
|
||||||
return json(bytes)
|
return web.json(bytes)
|
||||||
|
|
||||||
def result(query):
|
def result(query):
|
||||||
results = search(query)
|
results = search(query)
|
||||||
|
|
|
@ -11,23 +11,11 @@ http://inamidst.com/phenny/
|
||||||
import re, urllib
|
import re, urllib
|
||||||
import web
|
import web
|
||||||
|
|
||||||
r_json = re.compile(r'^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$')
|
|
||||||
r_string = re.compile(r'("(\\.|[^"\\])*")')
|
|
||||||
env = {'__builtins__': None, 'null': None,
|
|
||||||
'true': True, 'false': False}
|
|
||||||
|
|
||||||
def json(text):
|
|
||||||
"""Evaluate JSON text safely (we hope)."""
|
|
||||||
if r_json.match(r_string.sub('', text)):
|
|
||||||
text = r_string.sub(lambda m: 'u' + m.group(1), text)
|
|
||||||
return eval(text.strip(' \t\r\n'), env, {})
|
|
||||||
raise ValueError('Input must be serialised JSON.')
|
|
||||||
|
|
||||||
def detect(text):
|
def detect(text):
|
||||||
uri = 'http://ajax.googleapis.com/ajax/services/language/detect'
|
uri = 'http://ajax.googleapis.com/ajax/services/language/detect'
|
||||||
q = urllib.quote(text)
|
q = urllib.quote(text)
|
||||||
bytes = web.get(uri + '?q=' + q + '&v=1.0')
|
bytes = web.get(uri + '?q=' + q + '&v=1.0')
|
||||||
result = json(bytes)
|
result = web.json(bytes)
|
||||||
try: return result['responseData']['language']
|
try: return result['responseData']['language']
|
||||||
except Exception: return None
|
except Exception: return None
|
||||||
|
|
||||||
|
@ -36,7 +24,7 @@ def translate(text, input, output):
|
||||||
q = urllib.quote(text)
|
q = urllib.quote(text)
|
||||||
pair = input + '%7C' + output
|
pair = input + '%7C' + output
|
||||||
bytes = web.get(uri + '?q=' + q + '&v=1.0&langpair=' + pair)
|
bytes = web.get(uri + '?q=' + q + '&v=1.0&langpair=' + pair)
|
||||||
result = json(bytes)
|
result = web.json(bytes)
|
||||||
try: return result['responseData']['translatedText'].encode('cp1252')
|
try: return result['responseData']['translatedText'].encode('cp1252')
|
||||||
except Exception: return None
|
except Exception: return None
|
||||||
|
|
||||||
|
@ -59,7 +47,7 @@ def tr(phenny, context):
|
||||||
if input != output:
|
if input != output:
|
||||||
msg = translate(phrase, input, output)
|
msg = translate(phrase, input, output)
|
||||||
if msg:
|
if msg:
|
||||||
msg = msg.replace(''', "'")
|
msg = web.decode(msg) # msg.replace(''', "'")
|
||||||
msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output)
|
msg = '"%s" (%s to %s, translate.google.com)' % (msg, input, output)
|
||||||
else: msg = 'The %s to %s translation failed, sorry!' % (input, output)
|
else: msg = 'The %s to %s translation failed, sorry!' % (input, output)
|
||||||
|
|
||||||
|
|
|
@ -13,18 +13,6 @@ from tools import deprecated
|
||||||
|
|
||||||
r_from = re.compile(r'(?i)([+-]\d+):00 from')
|
r_from = re.compile(r'(?i)([+-]\d+):00 from')
|
||||||
|
|
||||||
r_json = re.compile(r'^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$')
|
|
||||||
r_string = re.compile(r'("(\\.|[^"\\])*")')
|
|
||||||
env = {'__builtins__': None, 'null': None,
|
|
||||||
'true': True, 'false': False}
|
|
||||||
|
|
||||||
def json(text):
|
|
||||||
"""Evaluate JSON text safely (we hope)."""
|
|
||||||
if r_json.match(r_string.sub('', text)):
|
|
||||||
text = r_string.sub(lambda m: 'u' + m.group(1), text)
|
|
||||||
return eval(text.strip(' \t\r\n'), env, {})
|
|
||||||
raise ValueError('Input must be serialised JSON.')
|
|
||||||
|
|
||||||
def location(name):
|
def location(name):
|
||||||
name = urllib.quote(name.encode('utf-8'))
|
name = urllib.quote(name.encode('utf-8'))
|
||||||
uri = 'http://ws.geonames.org/searchJSON?q=%s&maxRows=1' % name
|
uri = 'http://ws.geonames.org/searchJSON?q=%s&maxRows=1' % name
|
||||||
|
@ -34,7 +22,7 @@ def location(name):
|
||||||
bytes = u.read()
|
bytes = u.read()
|
||||||
u.close()
|
u.close()
|
||||||
|
|
||||||
results = json(bytes)
|
results = web.json(bytes)
|
||||||
try: name = results['geonames'][0]['name']
|
try: name = results['geonames'][0]['name']
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return '?', '?', '0', '0'
|
return '?', '?', '0', '0'
|
||||||
|
|
11
web.py
11
web.py
|
@ -56,5 +56,16 @@ def entity(match):
|
||||||
def decode(html):
|
def decode(html):
|
||||||
return r_entity.sub(entity, html)
|
return r_entity.sub(entity, html)
|
||||||
|
|
||||||
|
r_string = re.compile(r'("(\\.|[^"\\])*")')
|
||||||
|
r_json = re.compile(r'^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$')
|
||||||
|
env = {'__builtins__': None, 'null': None, 'true': True, 'false': False}
|
||||||
|
|
||||||
|
def json(text):
|
||||||
|
"""Evaluate JSON text safely (we hope)."""
|
||||||
|
if r_json.match(r_string.sub('', text)):
|
||||||
|
text = r_string.sub(lambda m: 'u' + m.group(1), text)
|
||||||
|
return eval(text.strip(' \t\r\n'), env, {})
|
||||||
|
raise ValueError('Input must be serialised JSON.')
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue