Added oblique.py, a new web services module.

master
Sean B. Palmer 2008-09-11 17:18:44 +01:00
parent 31d107c0bd
commit b91de3886e
7 changed files with 81 additions and 4 deletions

View File

@ -70,6 +70,8 @@ def u(phenny, input):
# phenny.msg('#inamidst', '%r' % arg)
if not arg:
return phenny.reply('You gave me zero length input.')
elif not arg.strip(' '):
return phenny.reply('%s SPACES' % len(arg))
# @@ space
if set(arg.upper()) - set(

View File

@ -22,7 +22,7 @@ abbrs = [
'cf', 'lit', 'etc', 'Ger', 'Du', 'Skt', 'Rus', 'Eng', 'Amer.Eng', 'Sp',
'Fr', 'N', 'E', 'S', 'W', 'L', 'Gen', 'J.C', 'dial', 'Gk',
'19c', '18c', '17c', '16c', 'St', 'Capt', 'obs', 'Jan', 'Feb', 'Mar',
'Apr', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
'Apr', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'c'
]
t_sentence = r'^.*?(?<!%s)(?:\.(?= [A-Z0-9]|\Z)|\Z)'
r_sentence = re.compile(t_sentence % ')(?<!'.join(abbrs))

View File

@ -7,7 +7,7 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import re, urllib, urlparse, time
import re, urllib, httplib, urlparse, time
from htmlentitydefs import name2codepoint
import web
from tools import deprecated
@ -24,11 +24,17 @@ def head(phenny, input):
try: uri = phenny.last_seen_uri[input.sender]
except KeyError: return phenny.say('?')
if not uri.startswith('htt'):
uri = 'http://' + uri
try: info = web.head(uri)
except IOError: return phenny.say("Can't connect to %s" % uri)
except httplib.InvalidURL: return phenny.say("Not a valid URI, sorry.")
if not isinstance(info, list):
info = dict(info)
try: info = dict(info)
except TypeError:
return phenny.reply('Try .head http://example.org/ [optional header]')
info['Status'] = '200'
else:
newInfo = dict(info[0])

62
modules/oblique.py Executable file
View File

@ -0,0 +1,62 @@
#!/usr/bin/env python
"""
oblique.py - Web Services Interface
Copyright 2008, Sean B. Palmer, inamidst.com
Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import urllib
import web
def mappings(uri):
result = {}
bytes = web.get(uri)
for line in bytes.splitlines():
if not line.startswith('<li>'): continue
line = line.strip()
if not line.endswith('</li>'): continue
command, template = line[4:-5].split(' ', 1)
if not template.startswith('http://'): continue
result[command] = template
return result
def o(phenny, input):
"""Call a webservice."""
text = input.group(2)
if (not o.services) or (text == 'refresh'):
if hasattr(phenny.config, 'services'):
services = phenny.config.services
else: services = 'http://swhack.jottit.com/services'
o.services = mappings(services)
if text == 'refresh':
return phenny.reply('Okay, found %s services.' % len(o.services))
if ' ' in text:
command, args = text.split(' ', 1)
else: command, args = text, ''
command = command.lower()
args = urllib.quote(args)
if o.services.has_key(command):
template = o.services[command]
template = template.replace('${args}', args)
template = template.replace('${nick}', input.nick)
uri = template.replace('${sender}', input.sender)
bytes = web.get(uri)
lines = bytes.splitlines()
if lines:
phenny.say(lines[0])
else: phenny.reply('Sorry, the service is broken.')
else: phenny.reply('Sorry, no such service. See %s' % services)
o.commands = ['o']
o.example = '.o servicename arg1 arg2 arg3'
o.services = {}
if __name__ == '__main__':
print __doc__.strip()

View File

@ -66,6 +66,7 @@ g.example = '.g swhack'
def gc(phenny, input):
"""Returns the number of Google results for the specified input."""
if input.nick == 'goatov': return
query = input.group(2)
if not query:
return phenny.reply('.gc what?')

View File

@ -24,7 +24,7 @@ r_redirect = re.compile(
abbrs = ['etc', 'ca', 'cf', 'Co', 'Ltd', 'Inc', 'Mt', 'Mr', 'Mrs',
'Dr', 'Ms', 'Rev', 'Fr', 'St', 'Sgt', 'pron', 'approx', 'lit',
'syn', 'transl'] \
'syn', 'transl', 'sess', 'fl'] \
+ list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') \
+ list('abcdefghijklmnopqrstuvwxyz')
t_sentence = r'^.{5,}?(?<!\b%s)(?:\.(?= [A-Z0-9]|\Z)|\Z)'

6
web.py
View File

@ -16,18 +16,24 @@ class Grab(urllib.URLopener):
urllib._urlopener = Grab()
def get(uri):
if not uri.startswith('http'):
return
u = urllib.urlopen(uri)
bytes = u.read()
u.close()
return bytes
def head(uri):
if not uri.startswith('http'):
return
u = urllib.urlopen(uri)
info = u.info()
u.close()
return info
def post(uri, query):
if not uri.startswith('http'):
return
data = urllib.urlencode(query)
u = urllib.urlopen(uri, data)
bytes = u.read()