-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
266 lines (205 loc) · 9.3 KB
/
app.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import json, bs4
import os
import requests
from flask import Flask, flash, jsonify, make_response, redirect, render_template, request, url_for
from scraper import get_course_table, get_graduate_info, get_student_course, get_student_progress, run
app = Flask(__name__)
app.config['SECRET_KEY'] = '3c178af81e8f023e05fc72c56757417158aaeff46e23263df647d9fdc4ad2452'
app.config['JSON_AS_ASCII'] = False
# decode course data from course_data.json
course_data = json.load(open(os.path.join(app.root_path, 'course_data.json'), 'r', encoding='utf-8'))
@app.route('/')
def index():
return render_template('terms_of_service.html')
@app.route('/home')
def home():
cookie_account = request.cookies.get('Account')
cookie_password = request.cookies.get('Password')
cookie_name = request.cookies.get('Name')
user_name = '訪客'
user_id = '-'
has_logged_in = 'False'
if cookie_account is not None and cookie_password is not None:
has_logged_in = 'True'
user_name = cookie_name
user_id = cookie_account
return render_template('home.html',
userName=user_name,
userId=user_id,
hasLoggedIn=has_logged_in,
courseData=course_data)
@app.route('/profile')
def profile():
cookie_account = request.cookies.get('Account')
cookie_password = request.cookies.get('Password')
cookie_name = request.cookies.get('Name')
if cookie_account is None or cookie_password is None:
return redirect(url_for('login'))
user_name = cookie_name
student_course_data = get_student_course(run(cookie_account, cookie_password))
student_course_credits = get_student_progress(
run(cookie_account, cookie_password))
student_graduate_info = get_graduate_info(cookie_account, cookie_password)
user_pic_url = '/static/img/user.png'
# r = requests.get(
# 'http://elearning.nuk.edu.tw/_uploadfiles/stuphoto/' + cookie_account.lower() + '.jpg')
# if r.status_code == 200:
# user_pic_url = 'http://elearning.nuk.edu.tw/_uploadfiles/stuphoto/' + \
# cookie_account.lower() + '.jpg'
return render_template('profile.html',
userName=user_name,
userId=cookie_account,
userPicUrl=user_pic_url,
studentGraduateInfo=student_graduate_info,
studentCourseData=student_course_data,
studentCourseCredits=student_course_credits)
@app.route('/guide')
def guide():
cookie_account = request.cookies.get('Account')
cookie_password = request.cookies.get('Password')
cookie_name = request.cookies.get('Name')
user_name = '訪客'
user_id = '-'
has_logged_in = 'False'
if cookie_account is not None and cookie_password is not None:
has_logged_in = 'True'
user_name = cookie_name
user_id = cookie_account
return render_template('how_to_use.html',
userName=user_name,
userId=user_id,
hasLoggedIn=has_logged_in)
@app.route('/login', methods=['GET', 'POST'])
def login():
cookie_account = request.cookies.get('Account')
cookie_password = request.cookies.get('Password')
if cookie_account is not None and cookie_password is not None:
return redirect(url_for('profile'))
if request.method == 'GET':
return render_template('login.html')
account = request.form['account']
password = request.form['password']
account_name = None
password_name = None
login_response = requests.get("https://aca.nuk.edu.tw/Student2/Menu1.asp")
soup = bs4.BeautifulSoup(login_response.content, 'html.parser')
pos = soup.find('td').find_all('tr',recursive=True)
csrf_token = soup.find('input', {'name': 'CSRFToken'})['value']
if pos[0]: account_name = pos[0].find_all("td",recursive=False)[1].find_all("input",recursive=True)[1]['name']
if pos[1]: password_name = pos[1].find_all("td",recursive=False)[1].find_all("input",recursive=True)[1]['name']
if account_name and password_name:
res = requests.post(
'https://aca.nuk.edu.tw/Student2/Menu1.asp',
{"CSRFToken": csrf_token ,account_name: account, password_name: password, "B1": "%B5n%A1%40%A1%40%A4J"})
res.encoding = 'big5'
if res.headers['Content-Length'] != '969':
student_graduate_info = get_graduate_info(account, password) # 取得學生基本資料
view_response = make_response(redirect(url_for('profile')))
view_response.set_cookie('Name', student_graduate_info['student_name'])
view_response.set_cookie('Account', account)
view_response.set_cookie('Password', password)
return view_response
flash('教務系統說登入錯誤', 'danger')
return redirect(url_for('login'))
@app.route('/logout')
def logout():
if request.cookies.get('Account') is not None:
res = make_response(redirect(url_for('home')))
res.delete_cookie('Account')
res.delete_cookie('Password')
res.delete_cookie('Name')
return res
return redirect(url_for('home'))
# ===== 手機頁面路由 ===== #
@app.route('/m/home')
def mobile_home():
cookie_account = request.cookies.get('Account')
cookie_password = request.cookies.get('Password')
cookie_name = request.cookies.get('Name')
user_name = '訪客'
user_id = '-'
has_logged_in = 'False'
if cookie_account is not None and cookie_password is not None:
has_logged_in = 'True'
user_name = cookie_name
user_id = cookie_account
return render_template('mobile_home.html',
userName=user_name,
userId=user_id,
hasLoggedIn=has_logged_in,
courseData=course_data)
@app.route('/m/profile')
def mobile_profile():
cookie_account = request.cookies.get('Account')
cookie_password = request.cookies.get('Password')
cookie_name = request.cookies.get('Name')
if cookie_account is None or cookie_password is None:
return redirect(url_for('mobile_login'))
user_name = cookie_name
student_course_data = get_student_course(run(cookie_account, cookie_password))
student_course_credits = get_student_progress(
run(cookie_account, cookie_password))
student_graduate_info = get_graduate_info(cookie_account, cookie_password)
user_pic_url = '/static/img/user.png'
# r = requests.get(
# 'http://elearning.nuk.edu.tw/_uploadfiles/stuphoto/' + cookie_account.lower() + '.jpg')
# if r.status_code == 200:
# user_pic_url = 'http://elearning.nuk.edu.tw/_uploadfiles/stuphoto/' + \
# cookie_account.lower() + '.jpg'
return render_template('mobile_profile.html',
userName=user_name,
userId=cookie_account,
userPicUrl=user_pic_url,
studentGraduateInfo=student_graduate_info,
studentCourseData=student_course_data,
studentCourseCredits=student_course_credits)
@app.route('/m/login', methods=['GET', 'POST'])
def mobile_login():
cookie_account = request.cookies.get('Account')
cookie_password = request.cookies.get('Password')
if cookie_account is not None and cookie_password is not None:
return redirect(url_for('mobile_profile'))
if request.method == 'GET':
return render_template('login.html')
account = request.form['account']
password = request.form['password']
res = requests.post(
'https://aca.nuk.edu.tw/Student2/Menu1.asp',
{
'Account': account,
'Password': password
})
res.encoding = 'big5'
if res.headers['Content-Length'] != '969':
student_graduate_info = get_graduate_info(account, password) # 取得學生基本資料
view_response = make_response(redirect(url_for('mobile_profile')))
view_response.set_cookie('Name', student_graduate_info['student_name'])
view_response.set_cookie('Account', account)
view_response.set_cookie('Password', password)
return view_response
flash('教務系統說登入錯誤', 'danger')
return redirect(url_for('mobile_login'))
@app.route('/m/logout')
def mobile_logout():
if request.cookies.get('Account') is not None:
res = make_response(redirect(url_for('mobile_home')))
res.delete_cookie('Account')
res.delete_cookie('Password')
res.delete_cookie('Name')
return res
return redirect(url_for('mobile_home'))
# ===== API ===== #
@app.route('/getInfo/<account>&<password>')
def get_info(account, password):
return jsonify(get_graduate_info(account, password))
@app.route('/getCourse/<account>&<password>')
def get_course(account, password):
return jsonify(get_student_course(run(account, password)))
@app.route('/getProgress/<account>&<password>')
def get_progress(account, password):
return jsonify(get_student_progress(run(account, password)))
@app.route('/getTable/<account>&<password>')
def get_table(account, password):
return jsonify(get_course_table(account, password))
if __name__ == "__main__":
app.run()