2008-02-21 07:06:33 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
web.py - Web Facilities
|
|
|
|
Author: Sean B. Palmer, inamidst.com
|
|
|
|
About: http://inamidst.com/phenny/
|
|
|
|
"""
|
|
|
|
|
2011-09-22 14:17:27 -04:00
|
|
|
import re, urllib.request, urllib.parse, urllib.error
|
|
|
|
from html.entities import name2codepoint
|
|
|
|
import json as jsonlib
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2011-09-22 14:17:27 -04:00
|
|
|
class Grab(urllib.request.URLopener):
|
|
|
|
def __init__(self, *args):
|
|
|
|
self.version = 'Mozilla/5.0 (Phenny)'
|
|
|
|
urllib.request.URLopener.__init__(self, *args)
|
|
|
|
def http_error_default(self, url, fp, errcode, errmsg, headers):
|
|
|
|
return urllib.addinfourl(fp, [headers, errcode], "http:" + url)
|
|
|
|
urllib.request._urlopener = Grab()
|
2008-02-21 07:06:33 -05:00
|
|
|
|
|
|
|
def get(uri):
|
2011-09-22 14:17:27 -04:00
|
|
|
if not uri.startswith('http'):
|
|
|
|
return
|
|
|
|
u = urllib.request.urlopen(uri)
|
2011-09-22 17:56:28 -04:00
|
|
|
bytes = u.read()
|
|
|
|
try:
|
|
|
|
bytes = bytes.decode('utf-8')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
bytes = bytes.decode('ISO-8859-1')
|
2011-09-22 14:17:27 -04:00
|
|
|
u.close()
|
|
|
|
return bytes
|
2008-02-21 07:06:33 -05:00
|
|
|
|
|
|
|
def head(uri):
|
2011-09-22 14:17:27 -04:00
|
|
|
if not uri.startswith('http'):
|
|
|
|
return
|
|
|
|
u = urllib.request.urlopen(uri)
|
|
|
|
info = u.info()
|
|
|
|
u.close()
|
|
|
|
return info
|
2008-02-21 07:06:33 -05:00
|
|
|
|
|
|
|
def post(uri, query):
|
2011-09-22 14:17:27 -04:00
|
|
|
if not uri.startswith('http'):
|
|
|
|
return
|
2011-11-30 16:05:09 -05:00
|
|
|
data = urllib.parse.urlencode(query).encode('utf-8')
|
2011-09-22 14:17:27 -04:00
|
|
|
u = urllib.request.urlopen(uri, data)
|
2011-09-22 17:56:28 -04:00
|
|
|
bytes = u.read()
|
|
|
|
try:
|
|
|
|
bytes = bytes.decode('utf-8')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
bytes = bytes.decode('ISO-8859-1')
|
2011-09-22 14:17:27 -04:00
|
|
|
u.close()
|
|
|
|
return bytes
|
2008-02-21 07:06:33 -05:00
|
|
|
|
2010-11-06 08:52:35 -04:00
|
|
|
r_entity = re.compile(r'&([^;\s]+);')
|
|
|
|
|
|
|
|
def entity(match):
|
2011-09-22 14:17:27 -04:00
|
|
|
value = match.group(1).lower()
|
|
|
|
if value.startswith('#x'):
|
|
|
|
return chr(int(value[2:], 16))
|
|
|
|
elif value.startswith('#'):
|
|
|
|
return chr(int(value[1:]))
|
|
|
|
elif value in name2codepoint:
|
|
|
|
return chr(name2codepoint[value])
|
|
|
|
return '[' + value + ']'
|
|
|
|
|
|
|
|
def quote(text):
|
|
|
|
return urllib.parse.quote(text)
|
2010-11-06 08:52:35 -04:00
|
|
|
|
|
|
|
def decode(html):
|
2011-09-22 14:17:27 -04:00
|
|
|
return r_entity.sub(entity, html)
|
2010-11-06 08:52:35 -04:00
|
|
|
|
2010-11-13 06:55:04 -05:00
|
|
|
r_string = re.compile(r'("(\\.|[^"\\])*")')
|
|
|
|
r_json = re.compile(r'^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$')
|
|
|
|
env = {'__builtins__': None, 'null': None, 'true': True, 'false': False}
|
|
|
|
|
|
|
|
def json(text):
|
2011-09-22 14:17:27 -04:00
|
|
|
"""Evaluate JSON text safely (we hope)."""
|
|
|
|
return jsonlib.loads(text)
|
2010-11-13 06:55:04 -05:00
|
|
|
|
2008-02-21 07:06:33 -05:00
|
|
|
if __name__=="__main__":
|
2011-09-22 14:17:27 -04:00
|
|
|
main()
|