This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·88 lines (73 loc) · 2.53 KB
/
server.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
#!/usr/bin/env python3
from urllib import parse
import datetime as dt
import logging
import flask
import threading
import webbrowser
import json
from manager import ElectSysManager
from utils import *
logger = logging.getLogger('lesson2cal')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
app = flask.Flask(__name__)
manager = ElectSysManager()
threading.Timer(2, lambda: webbrowser.open('http://localhost:5000/')).start()
@app.route('/')
def index_view():
manager.new_session()
manager.store_variables()
return load_text_file(find_data_file('templates', 'index.html'))
@app.route('/captcha')
def captcha_view():
contenttype, captcha = manager.get_captcha()
response = flask.make_response(captcha)
response.headers['Content-Type'] = contenttype
return response
@app.route('/login', methods=['POST'])
def login_view():
try:
user = flask.request.form['user']
passwd = flask.request.form['passwd']
captcha = flask.request.form['captcha']
except (KeyError, ValueError, TypeError):
return flask.jsonify({'success': False, 'message': '参数错误'})
error = manager.post_credentials(user, passwd, captcha)
if not error:
sems = manager.get_semesters()
return flask.jsonify({
'success': True,
'data': manager.get_raw_data(sems[0]),
'sems': sems
})
else:
return flask.jsonify({'success': False, 'message': error})
@app.route('/data')
def raw_data_view():
try:
arg = {
'xnm': flask.request.args['xnm'],
'xqm': flask.request.args['xqm']
}
except (KeyError, ValueError, TypeError):
return flask.jsonify({'success': False, 'message': '参数错误'})
return flask.jsonify({
'success': True,
'data': manager.get_raw_data(arg)
})
@app.route('/post', methods=['POST'])
def post_view():
try:
firstday_raw = flask.request.form['firstday']
firstday = dt.date(*[int(i) for i in firstday_raw.split('/')])
calendar_style = json.loads(flask.request.form['style'])
except (KeyError, ValueError, TypeError):
return flask.jsonify({'success': False, 'message': '参数错误'})
cal = manager.convert_lessons_to_ics(firstday, calendar_style)
response = flask.make_response(cal.serialize())
response.headers['Content-Type'] = 'text/calendar; charset=utf-8'
response.headers['Content-Disposition'] = 'attachment; filename="output.ics"'
return response
if __name__ == '__main__':
app.run()