Skip to content
Newer
Older
100755 72 lines (60 sloc) 1.82 KB
7931fab Phenny2, now being tested on Freenode as the main phenny.
Sean B. Palmer authored Feb 21, 2008
1 #!/usr/bin/env python
2 """
3 web.py - Web Facilities
4 Author: Sean B. Palmer, inamidst.com
5 About: http://inamidst.com/phenny/
6 """
7
9ae58d0 New calculator function, as tested by yano and jasondavies!
Sean B. Palmer authored Nov 6, 2010
8 import re, urllib
9 from htmlentitydefs import name2codepoint
7931fab Phenny2, now being tested on Freenode as the main phenny.
Sean B. Palmer authored Feb 21, 2008
10
11 class Grab(urllib.URLopener):
12 def __init__(self, *args):
13 self.version = 'Mozilla/5.0 (Phenny)'
14 urllib.URLopener.__init__(self, *args)
15 def http_error_default(self, url, fp, errcode, errmsg, headers):
16 return urllib.addinfourl(fp, [headers, errcode], "http:" + url)
17 urllib._urlopener = Grab()
18
19 def get(uri):
b91de38 Added oblique.py, a new web services module.
Sean B. Palmer authored Sep 11, 2008
20 if not uri.startswith('http'):
21 return
7931fab Phenny2, now being tested on Freenode as the main phenny.
Sean B. Palmer authored Feb 21, 2008
22 u = urllib.urlopen(uri)
23 bytes = u.read()
24 u.close()
25 return bytes
26
27 def head(uri):
b91de38 Added oblique.py, a new web services module.
Sean B. Palmer authored Sep 11, 2008
28 if not uri.startswith('http'):
29 return
7931fab Phenny2, now being tested on Freenode as the main phenny.
Sean B. Palmer authored Feb 21, 2008
30 u = urllib.urlopen(uri)
31 info = u.info()
32 u.close()
33 return info
34
35 def post(uri, query):
b91de38 Added oblique.py, a new web services module.
Sean B. Palmer authored Sep 11, 2008
36 if not uri.startswith('http'):
37 return
7931fab Phenny2, now being tested on Freenode as the main phenny.
Sean B. Palmer authored Feb 21, 2008
38 data = urllib.urlencode(query)
39 u = urllib.urlopen(uri, data)
40 bytes = u.read()
41 u.close()
42 return bytes
43
9ae58d0 New calculator function, as tested by yano and jasondavies!
Sean B. Palmer authored Nov 6, 2010
44 r_entity = re.compile(r'&([^;\s]+);')
45
46 def entity(match):
47 value = match.group(1).lower()
48 if value.startswith('#x'):
49 return unichr(int(value[2:], 16))
50 elif value.startswith('#'):
51 return unichr(int(value[1:]))
52 elif name2codepoint.has_key(value):
53 return unichr(name2codepoint[value])
54 return '[' + value + ']'
55
56 def decode(html):
57 return r_entity.sub(entity, html)
58
5ebe013 Services fixes, and a standard JSON API.
Sean B. Palmer authored Nov 13, 2010
59 r_string = re.compile(r'("(\\.|[^"\\])*")')
60 r_json = re.compile(r'^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$')
61 env = {'__builtins__': None, 'null': None, 'true': True, 'false': False}
62
63 def json(text):
64 """Evaluate JSON text safely (we hope)."""
65 if r_json.match(r_string.sub('', text)):
66 text = r_string.sub(lambda m: 'u' + m.group(1), text)
67 return eval(text.strip(' \t\r\n'), env, {})
68 raise ValueError('Input must be serialised JSON.')
69
7931fab Phenny2, now being tested on Freenode as the main phenny.
Sean B. Palmer authored Feb 21, 2008
70 if __name__=="__main__":
71 main()
Something went wrong with that request. Please try again.