Skip to content

Instantly share code, notes, and snippets.

@fizyk20
Created January 11, 2016 23:02
Show Gist options
  • Save fizyk20/c53fb7089b6b23668d1e to your computer and use it in GitHub Desktop.
Save fizyk20/c53fb7089b6b23668d1e to your computer and use it in GitHub Desktop.
WSGI script allowing substitution of the Google Calendar stylesheet in embedded calendars
from HTMLParser import HTMLParser
import requests
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
css_path = '/path/to/css'
class StyleReplacer(HTMLParser):
def __init__(self):
self.result = ''
self.insideStyle = False
HTMLParser.__init__(self)
def handle_starttag(self, tag, attrs):
if tag == 'style':
self.insideStyle = True
return
if tag == 'link':
d = dict(attrs)
if 'rel' in d and d['rel'] == 'stylesheet':
return
self.result += self.get_starttag_text()
if tag == 'head':
self.result += '<link href="%s" rel="stylesheet" type="text/css">' % css_path
def handle_endtag(self, tag):
if tag != 'style':
self.result += '</%s>' % tag
else:
self.insideStyle = False
def handle_startendtag(self, tag, attrs):
self.handle_starttag(tag, attrs)
def handle_data(self, data):
if not self.insideStyle:
self.result += data
def handle_entityref(self, ref):
self.result += '&%s;' % ref
def handle_charref(self, ref):
self.result += '&#%s;' % ref
def handle_decl(self, decl):
self.result += '<!%s>' % decl
def application(environ, start_response):
if not environ['QUERY_STRING']:
output = 'HEY KID I\'MMA COMPUTER'
start_response("404 Not Found", [('Content-type', 'text/plain'), ('Content-Length', str(len(output.encode('utf-8'))))])
return [output.encode('utf-8')]
params = environ['QUERY_STRING']
cal = requests.get('https://calendar.google.com/calendar/embed?%s' % params)
parser = StyleReplacer()
parser.feed(cal.text)
output = parser.result
start_response("200 OK", [('Content-type', 'text/html'),
('Content-Length', str(len(output.encode('utf-8'))))])
return [output.encode('utf-8')]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment