-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEduFetch.py
More file actions
347 lines (253 loc) · 13 KB
/
EduFetch.py
File metadata and controls
347 lines (253 loc) · 13 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
from curl_cffi import requests
import customtkinter as tk
from customtkinter import END
uPassFromGUI,uNameFromGUI,urlFromGUI="","",""
#----------
# FRONT-END
#----------
# Root window
root=tk.CTk()
root.geometry("400x620")
root.title("EduFetch v1.0.1")
urlEntered=tk.BooleanVar(value=False)
urlEntryAndButton=tk.CTkFrame(root,width=375,fg_color="transparent")
searchQuestionEntryAndButton=tk.CTkFrame(root,width=375,fg_color="transparent")
nameLabel=tk.CTkLabel(root,width=350,height=1,corner_radius=30,text="Welcome to EduFetch",font=("default",23,"bold"))
writeLabel=tk.CTkLabel(root,width=350,height=40,text="Enter the Educake quiz URL...")
blankSpace=tk.CTkLabel(root,height=10,text="\n")
blankSpace1=tk.CTkLabel(root,height=5,text="\n")
inputEntry=tk.CTkEntry(urlEntryAndButton,width=300,corner_radius=30,border_color="purple",text_color="white",border_width=2,fg_color="gray20")
confirmButton=tk.CTkButton(urlEntryAndButton,width=75,corner_radius=30,border_color="purple",text="Confirm",fg_color="gray22",border_width=2,hover_color="purple")
searchQuestionLabel=tk.CTkLabel(searchQuestionEntryAndButton,width=300,text="No questions found. Enter a valid URL above first.")
searchQuestionEntry=tk.CTkEntry(searchQuestionEntryAndButton,width=50,height=15,corner_radius=30,border_color="purple",border_width=1)
searchAnswerLabelBorder=tk.CTkFrame(root,width=369,corner_radius=30,border_color="purple",border_width=2,fg_color="purple")
searchAnswerLabel=tk.CTkLabel(searchAnswerLabelBorder,width=365,corner_radius=30,text="",fg_color="gray20")
scrollableAreaBorder=tk.CTkFrame(root,width=369,height=354,border_color="purple",border_width=2,fg_color="purple")
scrollableArea=tk.CTkScrollableFrame(scrollableAreaBorder,fg_color="gray20",width=365,height=350,scrollbar_button_hover_color="purple")
# Password and username prompt window
promptWindow=tk.CTkToplevel()
promptWindow.geometry("300x160")
promptWindow.title("")
credsEntered=tk.BooleanVar(value=False)
userNameSect=tk.CTkFrame(promptWindow,width=475,fg_color="transparent")
userPassSect=tk.CTkFrame(promptWindow,width=475,fg_color="transparent")
promptWriteLabel=tk.CTkLabel(promptWindow,text="Enter your username and password",font=("default",15,"bold"))
blankSpacePromptWindow=tk.CTkLabel(promptWindow,height=1,text="")
uNameLabel=tk.CTkLabel(userNameSect,text="Username")
uNameEntry=tk.CTkEntry(userNameSect,text_color="white",fg_color="gray20",border_color="purple",border_width=2,width=130)
uPassEntry=tk.CTkEntry(userPassSect,text_color="white",fg_color="gray20",border_color="purple",border_width=2,width=130)
uPassLabel=tk.CTkLabel(userPassSect,text="Password")
promptConfirmBtn=tk.CTkButton(promptWindow,fg_color="gray20",border_color="purple",border_width=2,width=50,text="Confirm",hover_color="purple")
# Prompt window packing
promptWriteLabel.pack(pady=7)
userNameSect.pack()
userPassSect.pack(pady=10)
uNameLabel.pack(side="left",padx=7)
uNameEntry.pack(side="right")
uPassLabel.pack(side="left",padx=7)
uPassEntry.pack(side="right")
promptConfirmBtn.pack()
# Root window packing
nameLabel.pack(pady=5)
writeLabel.pack()
urlEntryAndButton.pack()
inputEntry.pack(side="left",padx=3)
confirmButton.pack(side="right",padx=3)
blankSpace.pack()
scrollableArea.pack(padx=2,pady=2)
scrollableAreaBorder.pack(anchor="s")
blankSpace1.pack()
searchQuestionEntryAndButton.pack()
searchQuestionLabel.pack(side="left",padx=2)
searchQuestionEntry.pack(side="right",padx=2)
searchAnswerLabel.pack(padx=2,pady=2)
searchAnswerLabelBorder.pack(pady=5)
promptWindow.withdraw()
doNothing = lambda: None
def output(message,timeDisplayed=0):
writeLabel.configure(text=message)
root.after(timeDisplayed,doNothing)
def usernameAndPassPrompt():
global uPassFromGUI, uNameFromGUI
credsEntered.set(False)
promptWindow.deiconify()
root.wait_variable(credsEntered)
print(uPassFromGUI)
return [uNameFromGUI,uPassFromGUI]
def promptConfirmFunc():
global uPassFromGUI, uNameFromGUI
uNameFromGUI=uNameEntry.get()
uPassFromGUI=uPassEntry.get()
credsEntered.set(True)
promptWindow.withdraw()
promptConfirmBtn.configure(command=promptConfirmFunc)
def searchForAnswer(event=None):
number=searchQuestionEntry.get()
global correctAnswersDict
try:searchAnswerLabel.configure(text=correctAnswersDict[f"Q{number}"])
except:searchAnswerLabel.configure(text="Question number could not be found...")
# --------
# BACK-END
# --------
# Declaring session variables
# Define payload
loginPayload={"username" : "", "password" : "", "lastname" : "", "userType" : "student","allowEmailForUsername" : True}
# Define request headers for GET and POST HTTPS requests
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:146.0) Gecko/20100101 Firefox/146.0",
"Content-Type": "application/json",
"Accept": "application/json, text/plain, */*",
"Accept-Language": "en-GB,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Origin": "https://my.educake.co.uk",
"Referer": "https://my.educake.co.uk/student-login",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"Sec-CH-UA": '"Chromium";v="120", "Google Chrome";v="120"',
"Sec-CH-UA-Mobile": "?0",
"Sec-CH-UA-Platform": '"Linux"',
"Connection": "keep-alive",
}
# Define dictionary that will contain correctAnswers of quiz
correctAnswersDict={}
# Create session impersonating a chrome browser request
loginSession=requests.Session(impersonate="chrome120")
# Functions
def getTokens(loginPayload,verbose=False):# Function to get session XSRF-TOKEN, get/generate JWT-TOKEN and accumulate session cookies
global loginHeaders,loginSession
#URLs
frontEndLoginURL="https://my.educake.co.uk/student-login"
loginURL="https://my.educake.co.uk/login"
sessionTokenURL="https://my.educake.co.uk/session-token"
# Step 1- Send GET to front-end login page (just used to get XSRF-TOKEN instance, could be any page, but this one has no redirects)
frontEndLoginPageResponse=loginSession.get(frontEndLoginURL,headers=headers)#send GET request
if verbose:print(f"Front-end page request finished with code {frontEndLoginPageResponse.status_code}, starting api-based login page request...\n\n\n")
oXSRFTOKEN=(loginSession.cookies.get_dict())['XSRF-TOKEN']#get site cookies stored in current session, extract XSRF-TOKEN
headers["X-XSRF-TOKEN"]=oXSRFTOKEN#Add to login headers
# Step 2- Send POST to the real login API, by sending Educake the login details in loginPayload, and Educake will refresh XSRF-TOKEN
# giving access/authorization to get a JWT token from my.educake.co.uk/session-token
apiLoginPageResponse = loginSession.post(loginURL,headers=headers,json=loginPayload)# send POST request with payload/login info
if verbose:print(f"Finished API login page response with code {apiLoginPageResponse.status_code}\n\n\n")
XSRF_TOKEN=(apiLoginPageResponse.cookies.get_dict())['XSRF-TOKEN']# Get new XSRF-TOKEN
headers['X-XSRF-TOKEN']= XSRF_TOKEN# Replace old XSRF-TOKEN with new, permanent one
# Step 3- Take new XSRF-TOKEN and permissions, send GET to Educake session-token generator to create session-token (aka. JWT-TOKEN)
sessionTokenPageResponse = loginSession.get(sessionTokenURL,headers=headers)# send GET request
if verbose:print(f"Session token URL finished with code {sessionTokenPageResponse.status_code}")
sessionToken=sessionTokenPageResponse.json();sessionToken=sessionToken['accessToken']# Get the sent .json file, extract JWT-TOKEN
JWT_TOKEN=f"Bearer {sessionToken}"# Add 'Bearer' tag
if verbose:print("TOKEN fetching is done! Moving onto answer fetching...\n\n\n")
return [XSRF_TOKEN,JWT_TOKEN]
def getUserCredentialsAndAddToHeader(forceRewrite=False,verbose=False):# Function to get user credentials, checks validity, adds to request header and saves to file
global loginPayload
uName,uPass="",""
try: # If credentials file is found, and credentials are valid, assign the credentials to the login payload
if forceRewrite:iKnowThisIsAWeirdWayToDoThisButICantReallyBeBotheredTBH=1/0
userCredFile=open("educakeCredentials.txt","r")
credentials=userCredFile.read().split("\n")
uName=credentials[0]
uPass=credentials[1]
loginPayload['username']=uName
loginPayload['password']=uPass
getTokens(loginPayload)
except:
print("No credentials file was found, or was invalid. Creating one now...\n\n")
validCredentials=False
while not(validCredentials):
guiCreds=usernameAndPassPrompt()
getUsername=guiCreds[0]
getPassword=guiCreds[1]
try:
loginPayload["username"]=getUsername
loginPayload["password"]=getPassword
getTokens(loginPayload,verbose=verbose)
print("\n\nCredentials authorized, saving them now...")
uName=getUsername
uPass=getPassword
validCredentials=True
except KeyError:
promptWriteLabel.configure(text="Credentials invalid, try again")
userCredFile=open("educakeCredentials.txt","w")
userCredFile.write(f"{uName}\n{uPass}")
def getQuizURL(browserURL):# Pretty self explanitory
splitURL=browserURL.split("/");quizID=splitURL[-1]
return f"https://my.educake.co.uk/api/student/quiz/{quizID}"
#----------
# FRONT-END
#----------
def rootConfirmBtn():
global urlFromGUI
urlFromGUI=inputEntry.get()
urlEntered.set(value=True)
def getUrlFromGUI():
global urlFromGUI
urlEntered.set(value=False)
output("Enter the Educake quiz URL...")
root.wait_variable(urlEntered)
return urlFromGUI
confirmButton.configure(command=rootConfirmBtn)
#---------
# BACK-END
#---------
def fetchQuizAnswers(verbose=False):
global correctAnswersDict
root.update()
# Get username and password, add to request headers
getUserCredentialsAndAddToHeader()
# Defining security tokens for request headers
tokens=getTokens(loginPayload)
XSRF_TOKEN=tokens[0]
JWT_TOKEN=tokens[1]
# Updating headers with JWT-TOKEN and XSRF-TOKEN
headers['Authorization']= JWT_TOKEN
headers['X-XSRF-TOKEN']= XSRF_TOKEN
# Get quiz URL
quizBrowserURL=getUrlFromGUI()
urlToGoTo=getQuizURL(quizBrowserURL)
# Send GET request to questionIDs URL
urlResponse=loginSession.get(urlToGoTo,headers=headers)
if verbose:print(f"Got question IDs with code{urlResponse.status_code}, reason {urlResponse.reason}")
# Records text of URL
responseAsText=urlResponse.text
# Sets start and end of where to look (starts at 'questions', finishes at end of questionIDs list)
start = responseAsText.find("\"questions\":[")
end = responseAsText.find(",\"questionMap\"")
# Puts into iterable list format
questionIDs=((responseAsText[start:end]).replace("\"questions\":[","")).split(",")# Gets questionIDs in list form
# Defining base answer URL template
baseAnswerURL="https://my.educake.co.uk/api/course/question/"
# Adding quiz length to answer dictionary
correctAnswersDict["length"]=len(questionIDs)
# Iterating through questionsIDs and getting answers
for i in range(len(questionIDs)):
# Filling in URL with QuestionID
answerURL=f"{baseAnswerURL}{questionIDs[i]}/mark"
# Defining dummy answer to send via POST
sendPrompt={"givenAnswer" : "-1"}
# Send POST request to URL, with dummy answer and headers, then get text reponse from page
answerURLresponse=loginSession.post(answerURL,headers=headers,json=sendPrompt)
answerResponseAsText=answerURLresponse.text
# Define start point of 'correctAnswers'
nstart=answerResponseAsText.find("\"correctAnswers\":[")
# Define end point of 'correctAnswers'
nend=answerResponseAsText.find("],\"reasoning\":")
# Extract answer
correctAnswer=((answerResponseAsText[nstart:nend]).replace("\"correctAnswers\":[","")).replace("\"","").split(",")[0]
# If answer is present (mostly prevents 1 label showing when URL is invalid), display and save it
if correctAnswer!='':
# Display answer, add to dictionary
correctAnswersDict[f"Q{i+1}"]= correctAnswer
newQuestionLabel=tk.CTkLabel(scrollableArea,width=360,text=f"Q{i+1}.) {correctAnswer.capitalize()}",anchor="w",border_color="purple",border_width=1,corner_radius=30)
newQuestionLabel.pack(pady=2)
searchQuestionLabel.configure(text=f"There are {i+1} questions in this quiz.{"".join([" " for i in range(3-len(str(i+1)))])}Search for question")
searchQuestionEntry.bind("<KeyRelease>", searchForAnswer)
root.update()
# If it failed (in which 2 rows will be there, quiz length and failed first question), delete URL in Entry and send message to retry, recall program
if len(correctAnswersDict)<=2:
inputEntry.delete(0,END)
output("Invalid URL...",timeDisplayed=3000)
fetchQuizAnswers()
# Run program
fetchQuizAnswers()
# Continue GUI loop
root.mainloop()