-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
274 lines (229 loc) · 8.35 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
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
267
268
269
270
271
272
273
274
from flask import Flask, render_template, request, redirect
import requests
from datetime import date, datetime
start_time = datetime.now()
import creds
import re
import json
# TODO
# add a function to check if the cookie is still valid if not make it redirect to login page
# create a browser extention to automate the cookie process
# Bugs
# more dropdown dosent work when in subpage
# All user data
session_id = '0'
user_id = 0
prefix = '0'
logged_in = False
debug = True
if debug:
session_id = creds.session_id
user_id = creds.user_id
prefix = creds.prefix
logged_in = True
app = Flask(__name__)
# check on each requst if user is logged in.
@app.before_request
def check_login():
global logged_in
# Pages user is allowed to view when not logged in
if not logged_in and request.endpoint not in ['login', 'static', 'faq', 'mainLogin', 'howto']:
return redirect('/login')
@app.route('/')
def index():
# fetch classes for the current day
try:
response = requests.post(
f'https://{prefix}.compass.education/Services/Calendar.svc/GetCalendarEventsByUser?sessionstate=readonly',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0',
'Content-Type': 'application/json',
},
cookies={
'ASP.NET_SessionId': session_id
},
json={
"userId": user_id,
"startDate": str(date.today()), # YYYY-MM-DD
"endDate": str(date.today()), # YYYY-MM-DD
"start": 0,
}).json()['d']
user = requests.post(
f'https://{prefix}.compass.education/Services/User.svc/GetUserDetailsBlobByUserId',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0',
'Content-Type': 'application/json',
},
cookies={
'ASP.NET_SessionId': session_id
},
json={
"userId": user_id,
"targetUserId": user_id,
}).json()['d']
# Sort classes by start time
sorted_events = sorted(response, key=lambda x: x['start'])
except:
print("error in class fetch")
sorted_events = []
user = []
return render_template('index.html', sessions=sorted_events, user=user)
@app.route('/faq') # New route for /faq
def faq():
return render_template('faq.html')
@app.route('/howto') # New route for /howto
def howto():
return render_template('howto.html')
@app.route('/about')
def about():
return render_template('about.html', uptime=calculate_uptime())
# Function to calculate the uptime in seconds
def calculate_uptime():
current_time = datetime.now()
uptime = current_time - start_time
return uptime.total_seconds()
@app.route('/news')
def news():
try:
news_data = requests.post(
f'https://{prefix}.compass.education/Services/NewsFeed.svc/GetMyNewsFeedPaged?sessionstate=readonly',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0',
'Content-Type': 'application/json',
},
cookies={
'ASP.NET_SessionId': session_id
},
json={
"userId": user_id,
"start": 0,
"limit": 10,
}
).json()['d']['data']
except Exception as e:
print(f"Error in fetching news: {e}")
news_data = []
return render_template('news.html', news_data=news_data)
@app.route('/reports')
def get_reports():
try:
reports = []
# fetch the report list with the name and id
data = requests.post(
f'https://{creds.prefix}.compass.education/Services/Reports.svc/GetMyReportsList',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0',
},
cookies={
'ASP.NET_SessionId': creds.session_id
},
json={
"userId": creds.user_id,
}
).json()
# use the id's to get the url for the report, then append it to an array.
for report in data['d']:
url = requests.post(
f'https://{creds.prefix}.compass.education/Services/LongRunningFileRequest.svc/QueueTask',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0',
},
cookies={
'ASP.NET_SessionId': creds.session_id
},
json={
"type": "9",
"parameters": json.dumps({
"cycleId": report['cycleId'],
"schoolId": report['schoolId'],
"userId": report['userIdForSchool']
})
}
).json()
reports.append({
'desc': report['t'],
'url': f"https://{creds.prefix}.compass.education/Services/Reports/Download?cycleId={report['cycleId']}&schoolId={report['schoolId']}&userId={report['userIdForSchool']}&generationSessionId={url['d']}"
})
except:
print("error in report fetch")
# Render the reports.html template with the response content
return render_template('reports.html', reports=reports)
@app.route('/staff')
def staff():
response = requests.post(
f'https://{prefix}.compass.education/Services/User.svc/GetAllStaff',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0',
'Content-Type': 'application/json',
}, cookies={
'ASP.NET_SessionId': session_id
},
json={
"userId": user_id,
}
)
try:
# Try to access the expected structure
staff_data = response.json()['d']
except KeyError:
# If the expected structure is not found, handle it accordingly
staff_data = []
return render_template('staff.html', staff_data=staff_data, prefix=prefix)
@app.route('/tasks')
def tasks():
response = requests.post(
f'https://{prefix}.compass.education/Services/LearningTasks.svc/GetAllLearningTasksByUserId?sessionstate=readonly',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0',
'Content-Type': 'application/json',
}, cookies={
'ASP.NET_SessionId': session_id
},
json={
"userId": user_id,
"forceTaskId": 0,
"showHiddenTasks": False,
"page": 1,
"start": 0,
"limit": 100,
}
)
try:
# Try to access the expected structure
tasks = response.json()['d']['data']
sorted_tasks = sorted(tasks, key=lambda x: x['students'][0]['submissionStatus'])
except KeyError:
# If the expected structure is not found, handle it accordingly
sorted_tasks = []
return render_template('tasks.html', tasks=sorted_tasks)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
global session_id, prefix, user_id
global logged_in
session_id = request.form['session_id']
prefix = request.form['school_prefix']
data = requests.get(
f'https://{prefix}.compass.education/Records/User.aspx',
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0',
'Content-Type': 'application/json',
},
cookies={
'ASP.NET_SessionId': session_id
},
json={
}
).text
match = re.search(r'targetUserId: (\w+),', data)
if match:
user_id = int(match.group(1))
logged_in = True
print(session_id, prefix, user_id)
return redirect('/')
return render_template('login.html')
@app.route('/mainLogin')
def mainLogin():
return render_template('mainLogin.html')
if __name__ == '__main__':
app.run(debug=True)