Added oblique.py, a new web services module.
parent
31d107c0bd
commit
b91de3886e
|
@ -70,6 +70,8 @@ def u(phenny, input):
|
||||||
# phenny.msg('#inamidst', '%r' % arg)
|
# phenny.msg('#inamidst', '%r' % arg)
|
||||||
if not arg:
|
if not arg:
|
||||||
return phenny.reply('You gave me zero length input.')
|
return phenny.reply('You gave me zero length input.')
|
||||||
|
elif not arg.strip(' '):
|
||||||
|
return phenny.reply('%s SPACES' % len(arg))
|
||||||
|
|
||||||
# @@ space
|
# @@ space
|
||||||
if set(arg.upper()) - set(
|
if set(arg.upper()) - set(
|
||||||
|
|
|
@ -22,7 +22,7 @@ abbrs = [
|
||||||
'cf', 'lit', 'etc', 'Ger', 'Du', 'Skt', 'Rus', 'Eng', 'Amer.Eng', 'Sp',
|
'cf', 'lit', 'etc', 'Ger', 'Du', 'Skt', 'Rus', 'Eng', 'Amer.Eng', 'Sp',
|
||||||
'Fr', 'N', 'E', 'S', 'W', 'L', 'Gen', 'J.C', 'dial', 'Gk',
|
'Fr', 'N', 'E', 'S', 'W', 'L', 'Gen', 'J.C', 'dial', 'Gk',
|
||||||
'19c', '18c', '17c', '16c', 'St', 'Capt', 'obs', 'Jan', 'Feb', 'Mar',
|
'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)'
|
t_sentence = r'^.*?(?<!%s)(?:\.(?= [A-Z0-9]|\Z)|\Z)'
|
||||||
r_sentence = re.compile(t_sentence % ')(?<!'.join(abbrs))
|
r_sentence = re.compile(t_sentence % ')(?<!'.join(abbrs))
|
||||||
|
|
|
@ -7,7 +7,7 @@ Licensed under the Eiffel Forum License 2.
|
||||||
http://inamidst.com/phenny/
|
http://inamidst.com/phenny/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re, urllib, urlparse, time
|
import re, urllib, httplib, urlparse, time
|
||||||
from htmlentitydefs import name2codepoint
|
from htmlentitydefs import name2codepoint
|
||||||
import web
|
import web
|
||||||
from tools import deprecated
|
from tools import deprecated
|
||||||
|
@ -24,11 +24,17 @@ def head(phenny, input):
|
||||||
try: uri = phenny.last_seen_uri[input.sender]
|
try: uri = phenny.last_seen_uri[input.sender]
|
||||||
except KeyError: return phenny.say('?')
|
except KeyError: return phenny.say('?')
|
||||||
|
|
||||||
|
if not uri.startswith('htt'):
|
||||||
|
uri = 'http://' + uri
|
||||||
|
|
||||||
try: info = web.head(uri)
|
try: info = web.head(uri)
|
||||||
except IOError: return phenny.say("Can't connect to %s" % 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):
|
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'
|
info['Status'] = '200'
|
||||||
else:
|
else:
|
||||||
newInfo = dict(info[0])
|
newInfo = dict(info[0])
|
||||||
|
|
|
@ -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()
|
|
@ -66,6 +66,7 @@ g.example = '.g swhack'
|
||||||
|
|
||||||
def gc(phenny, input):
|
def gc(phenny, input):
|
||||||
"""Returns the number of Google results for the specified input."""
|
"""Returns the number of Google results for the specified input."""
|
||||||
|
if input.nick == 'goatov': return
|
||||||
query = input.group(2)
|
query = input.group(2)
|
||||||
if not query:
|
if not query:
|
||||||
return phenny.reply('.gc what?')
|
return phenny.reply('.gc what?')
|
||||||
|
|
|
@ -24,7 +24,7 @@ r_redirect = re.compile(
|
||||||
|
|
||||||
abbrs = ['etc', 'ca', 'cf', 'Co', 'Ltd', 'Inc', 'Mt', 'Mr', 'Mrs',
|
abbrs = ['etc', 'ca', 'cf', 'Co', 'Ltd', 'Inc', 'Mt', 'Mr', 'Mrs',
|
||||||
'Dr', 'Ms', 'Rev', 'Fr', 'St', 'Sgt', 'pron', 'approx', 'lit',
|
'Dr', 'Ms', 'Rev', 'Fr', 'St', 'Sgt', 'pron', 'approx', 'lit',
|
||||||
'syn', 'transl'] \
|
'syn', 'transl', 'sess', 'fl'] \
|
||||||
+ list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') \
|
+ list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') \
|
||||||
+ list('abcdefghijklmnopqrstuvwxyz')
|
+ list('abcdefghijklmnopqrstuvwxyz')
|
||||||
t_sentence = r'^.{5,}?(?<!\b%s)(?:\.(?= [A-Z0-9]|\Z)|\Z)'
|
t_sentence = r'^.{5,}?(?<!\b%s)(?:\.(?= [A-Z0-9]|\Z)|\Z)'
|
||||||
|
|
6
web.py
6
web.py
|
@ -16,18 +16,24 @@ class Grab(urllib.URLopener):
|
||||||
urllib._urlopener = Grab()
|
urllib._urlopener = Grab()
|
||||||
|
|
||||||
def get(uri):
|
def get(uri):
|
||||||
|
if not uri.startswith('http'):
|
||||||
|
return
|
||||||
u = urllib.urlopen(uri)
|
u = urllib.urlopen(uri)
|
||||||
bytes = u.read()
|
bytes = u.read()
|
||||||
u.close()
|
u.close()
|
||||||
return bytes
|
return bytes
|
||||||
|
|
||||||
def head(uri):
|
def head(uri):
|
||||||
|
if not uri.startswith('http'):
|
||||||
|
return
|
||||||
u = urllib.urlopen(uri)
|
u = urllib.urlopen(uri)
|
||||||
info = u.info()
|
info = u.info()
|
||||||
u.close()
|
u.close()
|
||||||
return info
|
return info
|
||||||
|
|
||||||
def post(uri, query):
|
def post(uri, query):
|
||||||
|
if not uri.startswith('http'):
|
||||||
|
return
|
||||||
data = urllib.urlencode(query)
|
data = urllib.urlencode(query)
|
||||||
u = urllib.urlopen(uri, data)
|
u = urllib.urlopen(uri, data)
|
||||||
bytes = u.read()
|
bytes = u.read()
|
||||||
|
|
Loading…
Reference in New Issue