2008-09-11 12:18:44 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
oblique.py - Web Services Interface
|
2009-06-07 05:08:49 -04:00
|
|
|
Copyright 2008-9, Sean B. Palmer, inamidst.com
|
2008-09-11 12:18:44 -04:00
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
|
|
|
"""
|
|
|
|
|
2008-10-29 12:03:12 -04:00
|
|
|
import re, urllib
|
2008-09-11 12:18:44 -04:00
|
|
|
import web
|
|
|
|
|
2010-11-06 08:09:03 -04:00
|
|
|
definitions = 'https://github.com/nslater/oblique/wiki'
|
2008-10-29 12:03:12 -04:00
|
|
|
|
|
|
|
r_item = re.compile(r'(?i)<li>(.*?)</li>')
|
|
|
|
r_tag = re.compile(r'<[^>]+>')
|
|
|
|
|
2008-09-11 12:18:44 -04:00
|
|
|
def mappings(uri):
|
|
|
|
result = {}
|
|
|
|
bytes = web.get(uri)
|
2008-10-29 12:03:12 -04:00
|
|
|
for item in r_item.findall(bytes):
|
|
|
|
item = r_tag.sub('', item).strip(' \t\r\n')
|
|
|
|
if not ' ' in item: continue
|
2008-09-11 12:18:44 -04:00
|
|
|
|
2008-10-29 12:03:12 -04:00
|
|
|
command, template = item.split(' ', 1)
|
2009-06-18 09:20:12 -04:00
|
|
|
if not command.isalnum(): continue
|
2008-09-11 12:18:44 -04:00
|
|
|
if not template.startswith('http://'): continue
|
2008-11-20 05:18:21 -05:00
|
|
|
result[command] = template.replace('&', '&')
|
2008-09-11 12:18:44 -04:00
|
|
|
return result
|
|
|
|
|
2009-06-07 05:05:37 -04:00
|
|
|
def service(phenny, input, command, args):
|
|
|
|
t = o.services[command]
|
|
|
|
template = t.replace('${args}', urllib.quote(args.encode('utf-8')))
|
|
|
|
template = template.replace('${nick}', urllib.quote(input.nick))
|
|
|
|
uri = template.replace('${sender}', urllib.quote(input.sender))
|
|
|
|
|
|
|
|
bytes = web.get(uri)
|
|
|
|
lines = bytes.splitlines()
|
|
|
|
if not lines:
|
|
|
|
return phenny.reply('Sorry, the service is broken.')
|
|
|
|
phenny.say(lines[0][:350])
|
|
|
|
|
2008-09-11 12:18:44 -04:00
|
|
|
def o(phenny, input):
|
|
|
|
"""Call a webservice."""
|
|
|
|
text = input.group(2)
|
2008-10-29 12:03:12 -04:00
|
|
|
if hasattr(phenny.config, 'services'):
|
|
|
|
services = phenny.config.services
|
|
|
|
else: services = definitions
|
2008-09-11 12:18:44 -04:00
|
|
|
|
|
|
|
if (not o.services) or (text == 'refresh'):
|
2008-10-29 12:03:12 -04:00
|
|
|
old = o.services
|
2008-09-11 12:18:44 -04:00
|
|
|
o.services = mappings(services)
|
|
|
|
if text == 'refresh':
|
2008-10-29 12:03:12 -04:00
|
|
|
msg = 'Okay, found %s services.' % len(o.services)
|
|
|
|
added = set(o.services) - set(old)
|
|
|
|
if added:
|
|
|
|
msg += ' Added: ' + ', '.join(sorted(added)[:5])
|
|
|
|
if len(added) > 5: msg += ', &c.'
|
|
|
|
return phenny.reply(msg)
|
|
|
|
|
|
|
|
if not text:
|
|
|
|
return phenny.reply('Try %s for details.' % services)
|
2008-09-11 12:18:44 -04:00
|
|
|
|
|
|
|
if ' ' in text:
|
|
|
|
command, args = text.split(' ', 1)
|
|
|
|
else: command, args = text, ''
|
|
|
|
command = command.lower()
|
|
|
|
|
2008-12-28 04:42:48 -05:00
|
|
|
if command == 'service':
|
|
|
|
msg = o.services.get(args, 'No such service!')
|
|
|
|
return phenny.reply(msg)
|
|
|
|
|
2009-06-07 04:57:41 -04:00
|
|
|
if not o.services.has_key(command):
|
|
|
|
return phenny.reply('Sorry, no such service. See %s' % services)
|
|
|
|
|
|
|
|
if hasattr(phenny.config, 'external'):
|
|
|
|
default = phenny.config.external.get('*')
|
|
|
|
manifest = phenny.config.external.get(input.sender, default)
|
|
|
|
if manifest:
|
|
|
|
commands = set(manifest)
|
|
|
|
if (command not in commands) and (manifest[0] != '!'):
|
|
|
|
return phenny.reply('Sorry, %s is not whitelisted' % command)
|
|
|
|
elif (command in commands) and (manifest[0] == '!'):
|
|
|
|
return phenny.reply('Sorry, %s is blacklisted' % command)
|
2009-06-07 05:05:37 -04:00
|
|
|
service(phenny, input, command, args)
|
2008-09-11 12:18:44 -04:00
|
|
|
o.commands = ['o']
|
|
|
|
o.example = '.o servicename arg1 arg2 arg3'
|
|
|
|
o.services = {}
|
|
|
|
|
2010-11-06 08:09:03 -04:00
|
|
|
def snippet(phenny, input):
|
|
|
|
py = "BeautifulSoup.BeautifulSoup(re.sub('<.*?>|(?<= ) +', '', " + \
|
|
|
|
"eval(urllib.urlopen('http://ajax.googleapis.com/ajax/serv" + \
|
|
|
|
"ices/search/web?v=1.0&q=" + urllib.quote(input.group(2)) + \
|
|
|
|
"').read().replace('null', 'None'))['responseData']['resul" + \
|
|
|
|
"ts'][0]['content'].decode('unicode-escape')), convertEntities=True)"
|
|
|
|
service(phenny, input, 'py', py)
|
|
|
|
snippet.commands = ['snippet']
|
|
|
|
|
2008-09-11 12:18:44 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
print __doc__.strip()
|