-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
executable file
·102 lines (85 loc) · 3.53 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#! /usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer, SimpleHTTPRequestHandler
from schedule import get_schedule
from datetime import datetime
from base64 import b64encode
from urllib import parse
from dateutil.parser import parse as date_parse
import traceback
# Fall semester, 2017
default_start_date = "2018-01-08"
with open('template.html', 'r') as f:
template = f.read()
class Server(SimpleHTTPRequestHandler):
def write_template(self, output):
self.wfile.write(template.replace(
'{CONTENTS}', output).encode('utf-8'))
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
if self.path == '/':
self._set_headers()
with open('homepage.html', 'r') as f:
homepage = f.read()
self.write_template(homepage)
else:
SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
self.send_response(200)
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
post_data = {k: v for k, v in (
x.split('=') for x in post_data.decode('utf-8').split('&'))}
error, schedule, warnings = None, None, None
try:
# Given start date or left empty
start_date = date_parse(post_data.get("start_date", default_start_date))
except ValueError as e:
# Given start date but not valid string; use default
traceback.print_exc()
start_date = date_parse(default_start_date)
try:
schedule, warnings = get_schedule(
post_data['username'], parse.unquote(post_data['password']), start_date)
except Exception as e:
traceback.print_exc()
error = e
if schedule:
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
encoded_schedule = 'data:application/octet-stream;base64,' + \
b64encode(str(schedule).encode('utf-8')).decode('utf-8')
output = '<h1>Schedule created '
if warnings:
output += 'with %s warning%s!</h1><ul>' % (
len(warnings), '' if len(warnings) == 1 else 's')
for warning in warnings:
output += '<li>' + warning + '</li>'
output += '</ul>'
else:
output += 'successfully!</h1>'
output += '<h2><a class="button" download="%s" href="%s" title="Download Schedule">Download Schedule</a></h2>' % (
post_data['username'] + '.ics', encoded_schedule)
output += '<a href="/"">go back</a>'
self.write_template(output)
else:
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
if not error:
output = '<h1>Incorrect username or password!</h1>'
else:
output = "<h1>An internal error occured</h1><pre>" + str(error) + "</pre>"
print(error)
output += '<p><a href="/"">go back</a></p>'
self.write_template(output)
def run(server_class=HTTPServer, handler_class=Server, port=8080):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print('Starting httpd...')
httpd.serve_forever()
if __name__ == '__main__':
run()