-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEduAnswer.py
More file actions
516 lines (362 loc) · 17.5 KB
/
Copy pathEduAnswer.py
File metadata and controls
516 lines (362 loc) · 17.5 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
from curl_cffi import requests
import json
import os
import customtkinter as tk
from google import genai
from google.genai import types
uPassFromGUI,uNameFromGUI,urlFromGUI,apiKeyFromGUI="","","",""
#----------
# FRONT-END
#----------
# Root window
root=tk.CTk()
root.geometry("400x620")
root.title("EduAnswer 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 EduAnswer",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=300,corner_radius=30,border_color="purple",border_width=2)
searchAnswerLabel=tk.CTkLabel(searchAnswerLabelBorder,height=25,width=350,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")
# API key prompt window
apiKeyWindow=tk.CTkToplevel()
apiKeyWindow.geometry("300x130")
apiKeyWindow.title("")
apiKeyEntered=tk.BooleanVar(value=False)
apiKeyEntrySect=tk.CTkFrame(apiKeyWindow, width=475, fg_color="transparent")
keyWindowLabel=tk.CTkLabel(apiKeyWindow, text="Gemini API key (read Github page):", font=("default",15,"bold"))
apiKeyLabel=tk.CTkLabel(apiKeyEntrySect, text_color="white", text="API Key ")
keyWindowEntry=tk.CTkEntry(apiKeyEntrySect, text_color="white",fg_color="gray20",border_color="purple", border_width=2,width=130)
keyWindowPromptConfirmBtn=tk.CTkButton(apiKeyWindow,fg_color="gray20",border_color="purple",border_width=2,width=50,text="Confirm",hover_color="purple")
# API key prompt window packing
keyWindowLabel.pack(pady=7)
apiKeyEntrySect.pack()
apiKeyLabel.pack(side="left",pady=7)
keyWindowEntry.pack(side="right",pady=7)
keyWindowPromptConfirmBtn.pack(pady=7)
# 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()
apiKeyWindow.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 apiKeyPrompt():
global apiKeyFromGUI
apiKeyEntered.set(value=False)
apiKeyWindow.deiconify()
root.wait_variable(apiKeyEntered)
return apiKeyFromGUI
# TODO- Make function for button confirm
def apiKeyPromptConfirmFunc():
global apiKeyFromGUI
apiKeyFromGUI=keyWindowEntry.get()
apiKeyEntered.set(value=True)
apiKeyWindow.withdraw()
keyWindowPromptConfirmBtn.configure(command=apiKeyPromptConfirmFunc)
def promptConfirmFunc():
global uPassFromGUI, uNameFromGUI
uNameFromGUI=uNameEntry.get()
uPassFromGUI=uPassEntry.get()
credsEntered.set(value=True)
promptWindow.withdraw()
promptConfirmBtn.configure(command=promptConfirmFunc)
def searchForAnswer(event=None):
number=searchQuestionEntry.get()
global answersDict
try:searchAnswerLabel.configure(text=(result:= next((i["answer"] for i in answersDict if i["question_number"] == int(number)), None)))
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
answersDict={}
# 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
def addEntryToGUI(entry,cNumOfEntries):
global answersDict
newQuestionFrame=tk.CTkFrame(scrollableArea,width=360,height=28,border_color='purple',fg_color="gray20",border_width=2,corner_radius=30)
newQuestionLabel=tk.CTkLabel(newQuestionFrame,width=340,height=23,text=entry,fg_color="transparent",bg_color="transparent",anchor="w",padx=15)
newQuestionFrame.pack_propagate(False)
newQuestionFrame.pack(pady=2)
newQuestionLabel.pack(expand=True)
searchQuestionLabel.configure(text=f"There are {cNumOfEntries} questions in this quiz.{"".join([" " for i in range(3-len(str(cNumOfEntries+1)))])}Search for question ")
searchQuestionEntry.bind("<KeyRelease>",searchForAnswer)
root.update()
def getAPIKey():
global loginPayload
APIKey='None'
try:
credFile= open("educakeCredentials.txt","r")
path=os.path.abspath(credFile.name)
print(path)
for i,line in enumerate(credFile, start=1):
if i==3:
APIKey=line.strip('\n')
break
credFile.close()
if APIKey=='None':
hi=1/0
geminiClient=genai.Client(api_key=APIKey)
response = geminiClient.models.generate_content(
model="gemini-3.1-flash-lite",
contents="Return the word 'hello'. Do not add anything else"
)
except:
print("No previous valid Gemini API Key was found...\n")
#APIKey=str(input("Please read the instructions for getting an API key on the Github page, then paste your API key here:\t"))
APIKey=apiKeyPrompt()
apiKeyEntered.set(value=False)
credFile=open("educakeCredentials.txt","w")
uName=loginPayload["username"]
uPass=loginPayload["password"]
credFile.write(f"{uName}\n{uPass}\n{APIKey}")
geminiClient=genai.Client(api_key=APIKey)
try:
response = geminiClient.models.generate_content(
model="gemini-3.5-flash",
contents="Return the word 'hello'. Do not add anything else"
)
except:
print("API Key is invalid, read through the github page instructions and paste in a valid API key")
getAPIKey()
return APIKey
confirmButton.configure(command=rootConfirmBtn)
#---------
# BACK-END
#---------
def fetchQuizAnswers(verbose=False):
global answersDict
geminiPayload=[]
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)
attemptID= urlToGoTo.split("/")[-1]
# 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
responseAsJSON=urlResponse.json()
questions= responseAsJSON['attempt'][attemptID]['questionMap'] # Gets questions as JSON
for i in questions.items():
questionType=["","",""]
curQuestionText=i[1]['question'] # Assigning text of questions to var
curQuestionImage=i[1]['image'] # Assigning images of questions to var (assigns None if no image)
curQuestionChoices="Free Answer"
isChoice=False
try: # If there is a 'choice' field assign curQuestionChoices to it's val
curQuestionChoices=i[1]['choices']
isChoice=True
except:
isChoice=False
# Setting values for each index in questionType
if curQuestionImage!=None:
questionType[0]="Image"
else:
questionType[0]="Text"
if isChoice:
questionType[1]="Choice"
questionType[2]=curQuestionChoices
else:
questionType[1]="Free Answer"
questionType[2]="None"
geminiPayload.append([questionType, curQuestionText, curQuestionImage])
# Creating the prompt to give to gemini
geminiPrompt= f"""
You are a GCSE and A-Level tutor.
The Question Data that you are being sent are in the form:
[
[[question_reference_type, question_answer_type, choices_if_applicable],question_text, image_url],
...
]
If question_reference_type is 'Image', you must view the image at the url provided at image_url as well as reading the text to answer the question.
If question_reference_type is 'Text' then there is no image needed for the question and you must answer the question using only the text provided.
If question_answer_type is 'Choice' then you must provide the correct answer choice for the question, with these choices listed in choices_if_applicable
If question_answer_type is 'Free Answer' then you must write the correct answer. ONLY PROVIDE THE ANSWER, DO NOT GIVE REASONING. YOUR ANSWER MUST BE AS SHORT AS POSSIBLE WHILE OBEYING QUESTION CONDITIONS
You must answer all questions, considering the previous stated rules and requirements, and send the answers back in this JSON format:
[
{{
"question_number":1,
"answer": "answer_text_here"
}},
...
]
Question data:
{geminiPayload}
"""
# Get API Key, pretty self-explanatory
APIKey=getAPIKey()
# Create client
geminiClient=genai.Client(api_key=APIKey)
# Send prompt off to model
geminiClientResponse=geminiClient.models.generate_content(model="gemini-2.5-flash", contents=geminiPrompt)
# Get response as text and remove some markdown BS
geminiClientResponseAsText=(geminiClientResponse.text).replace("```json", "").replace("```","").strip()
print(f"\n\n\n\n\n\n {geminiClientResponseAsText}")
# Convert text to JSON/dict
answersDict=json.loads(geminiClientResponseAsText)
cNumOfEntries=0
for i in answersDict:
cNumOfEntries+=1
textForEntry=f"Q{i['question_number']}.) {i['answer']}"
addEntryToGUI(textForEntry,cNumOfEntries)
# Run program
fetchQuizAnswers(verbose=True)
# Continue GUI loop
root.mainloop()