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/
|
|
|
|
"""
|
|
|
|
|
2011-09-22 14:17:27 -04:00
|
|
|
import re, urllib.request, urllib.parse, urllib.error
|
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):
|
2012-01-03 14:09:34 -05:00
|
|
|
result = {}
|
|
|
|
bytes = web.get(uri)
|
|
|
|
for item in r_item.findall(bytes):
|
|
|
|
item = r_tag.sub('', item).strip(' \t\r\n')
|
|
|
|
if not ' ' in item: continue
|
|
|
|
|
|
|
|
command, template = item.split(' ', 1)
|
|
|
|
if not command.isalnum(): continue
|
|
|
|
if not template.startswith('http://'): continue
|
|
|
|
result[command] = template.replace('&', '&')
|
|
|
|
return result
|
2008-09-11 12:18:44 -04:00
|
|
|
|
2009-06-07 05:05:37 -04:00
|
|
|
def service(phenny, input, command, args):
|
2012-01-03 14:09:34 -05:00
|
|
|
t = o.services[command]
|
|
|
|
template = t.replace('${args}', urllib.parse.quote(args, ''))
|
|
|
|
template = template.replace('${nick}', urllib.parse.quote(input.nick, ''))
|
|
|
|
uri = template.replace('${sender}', urllib.parse.quote(input.sender, ''))
|
|
|
|
|
|
|
|
info = web.head(uri)
|
|
|
|
if isinstance(info, list):
|
|
|
|
info = info[0]
|
|
|
|
if not 'text/plain' in info.get('content-type', '').lower():
|
|
|
|
return phenny.reply("Sorry, the service didn't respond in plain text.")
|
|
|
|
bytes = web.get(uri)
|
|
|
|
lines = bytes.splitlines()
|
|
|
|
if not lines:
|
|
|
|
return phenny.reply("Sorry, the service didn't respond any output.")
|
2012-02-17 22:10:05 -05:00
|
|
|
try: line = lines[0].encode('utf-8')[:350]
|
|
|
|
except: line = lines[0][:250]
|
2012-01-03 14:09:34 -05:00
|
|
|
phenny.say(lines[0][:350])
|
2009-06-07 05:05:37 -04:00
|
|
|
|
2010-11-13 06:55:04 -05:00
|
|
|
def refresh(phenny):
|
2012-01-03 14:09:34 -05:00
|
|
|
if hasattr(phenny.config, 'services'):
|
|
|
|
services = phenny.config.services
|
|
|
|
else: services = definitions
|
2008-09-11 12:18:44 -04:00
|
|
|
|
2012-01-03 14:09:34 -05:00
|
|
|
old = o.services
|
|
|
|
o.serviceURI = services
|
|
|
|
o.services = mappings(o.serviceURI)
|
|
|
|
return len(o.services), set(o.services) - set(old)
|
2010-11-13 06:55:04 -05:00
|
|
|
|
|
|
|
def o(phenny, input):
|
2012-01-03 14:09:34 -05:00
|
|
|
"""Call a webservice."""
|
|
|
|
text = input.group(2)
|
|
|
|
|
|
|
|
if (not o.services) or (text == 'refresh'):
|
|
|
|
length, added = refresh(phenny)
|
|
|
|
if text == 'refresh':
|
|
|
|
msg = 'Okay, found %s services.' % length
|
|
|
|
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.' % o.serviceURI)
|
|
|
|
|
|
|
|
if ' ' in text:
|
|
|
|
command, args = text.split(' ', 1)
|
|
|
|
else: command, args = text, ''
|
|
|
|
command = command.lower()
|
|
|
|
|
|
|
|
if command == 'service':
|
|
|
|
msg = o.services.get(args, 'No such service!')
|
|
|
|
return phenny.reply(msg)
|
|
|
|
|
|
|
|
if command not in o.services:
|
|
|
|
return phenny.reply('Service not found in %s' % o.serviceURI)
|
|
|
|
|
|
|
|
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)
|
|
|
|
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-13 06:55:04 -05:00
|
|
|
o.serviceURI = None
|
2008-09-11 12:18:44 -04:00
|
|
|
|
2010-11-06 08:09:03 -04:00
|
|
|
def snippet(phenny, input):
|
2012-01-03 14:09:34 -05:00
|
|
|
if not o.services:
|
|
|
|
refresh(phenny)
|
|
|
|
|
|
|
|
search = urllib.parse.quote(input.group(2))
|
|
|
|
py = "BeautifulSoup.BeautifulSoup(re.sub('<.*?>|(?<= ) +', '', " + \
|
|
|
|
"''.join(chr(ord(c)) for c in " + \
|
|
|
|
"eval(urllib.urlopen('http://ajax.googleapis.com/ajax/serv" + \
|
|
|
|
"ices/search/web?v=1.0&q=" + search + "').read()" + \
|
|
|
|
".replace('null', 'None'))['responseData']['resul" + \
|
|
|
|
"ts'][0]['content'].decode('unicode-escape')).replace(" + \
|
|
|
|
"'"', '\x22')), convertEntities=True)"
|
|
|
|
service(phenny, input, 'py', py)
|
2010-11-06 08:09:03 -04:00
|
|
|
snippet.commands = ['snippet']
|
|
|
|
|
2008-09-11 12:18:44 -04:00
|
|
|
if __name__ == '__main__':
|
2012-01-03 14:09:34 -05:00
|
|
|
print(__doc__.strip())
|