This repository has been archived by the owner on Jun 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
kahootAuto.py
196 lines (156 loc) · 6.5 KB
/
kahootAuto.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
from configparser import ConfigParser
from logger import init_logging
from logging import getLogger
import time
from selenium import webdriver, common
init_logging()
def initWebDriver(logger):
logger.info("Initalizing Automated Browser Session")
try:
driver = webdriver.Chrome("chromedriver_76.exe") # TODO: Allow user to select chromedriver version
except common.exceptions.WebDriverException as err:
logger.error("ERROR: {}".format(err))
logger.critical("FAILED TO START AUTOMATED BROWSER SESSION; UNABLE TO CONTINUE\nPress Enter to exit")
input(); raise SystemExit(1);
logger.info("Automated Browser Session Initalized")
return driver
class GetAnswers():
def __init__(self, quizID, pageLoadTime):
self.logger = getLogger("bot")
self.quizID = quizID
self.logger.info("Got Quiz id: {}".format(quizID))
self.driver = initWebDriver(self.logger)
self.pageLoadTime = pageLoadTime
self.CVT_INT_SHAPE = {
3: "TRIANGLE",
5: "DIAMOND",
7: "CIRCLE",
9: "SQUARE"
}
def getQuizAnswers(self) -> dict:
self.logger.info("Entering Get Quiz Answers")
answerDict = {}
questions = self.driver.find_elements_by_class_name("question-list__item")
self.logger.info("Found {} Questions".format(len(questions)))
self.logger.info("Parsing Questions")
for question in questions:
questionTextSplit = question.text.split("\n")
questionAnswerIndex = questionTextSplit.index("This is a correct answer") - 1
questionNumber = int(questionTextSplit[0].split("-")[0])
answerDict.update({questionNumber: self.CVT_INT_SHAPE.get(questionAnswerIndex)})
self.logger.debug({questionNumber: self.CVT_INT_SHAPE.get(questionAnswerIndex)})
self.logger.info("Got All Answers")
self.logger.info("Destroying Automated Browser Session")
self.driver.quit()
return answerDict
def getQuizPage(self):
quizAnswersURL = "https://create.kahoot.it/details/{}".format(self.quizID)
self.logger.info("Constructed Quiz Answer URL: {}".format(quizAnswersURL))
self.driver.get(quizAnswersURL)
self.logger.info("Clicking on Show Answers Button")
self.logger.info("Webpage Load complete; Wait constant amt({}s) for Js to load".format(self.pageLoadTime))
time.sleep(self.pageLoadTime) # Pause and make javascript do the work
showAnswersBtn = self.driver.find_element_by_class_name("question-list__group-toggle")
showAnswersBtn.click()
class AutoPlay():
def __init__(self, gamePin, username, answerDelay, answerKey, pageLoadTime):
self.gamePin = gamePin
self.username = username
self.answerDelay = answerDelay
self.answerDict = answerKey
self.pageLoadTime = pageLoadTime
self.logger = getLogger("bot")
self.logger.warning("Answer Delay is set to {}s".format(answerDelay))
self.driver = initWebDriver(self.logger)
self.CVT_SHAPE_CLASSNAME = {
"SQUARE": "bDfINc",
"CIRCLE": "eYFENK",
"TRIANGLE": "eRSCLD",
"DIAMOND": "fabXZJ"
}
def enterGame(self):
gameURL = "https://kahoot.it/"
self.driver.get(gameURL)
self.logger.info("Entering Game PIN")
self.driver.find_element_by_id("game-input").send_keys(self.gamePin)
self.driver.find_element_by_class_name("enter-button__EnterButton-sc-1o9b9va-0").click()
self.logger.info("Pressing Enter Button")
self.logger.info("Webpage Load complete; Wait constant amt({}s) for Js to load".format(self.pageLoadTime))
time.sleep(self.pageLoadTime)
self.driver.find_element_by_id("nickname").send_keys(self.username)
self.logger.info("Entering Username")
self.driver.find_element_by_class_name("enter-button__EnterButton-sc-1o9b9va-0").click()
self.logger.info("Entering Game")
def doGameLoop(self):
while True:
if (self.driver.current_url == "https://kahoot.it/v2/gameblock"):
try:
questionNumberText = self.driver.find_element_by_class_name("question-top-bar__QuestionNumber-sc-1pwisow-3").text
self.logger.debug("Question number raw text form: {}".format(questionNumberText))
questionNumber = int(questionNumberText.split("of")[0])
self.logger.info("Current Question is: {} | Answer Shape: {}".format(questionNumber, self.answerDict.get(questionNumber)))
time.sleep(self.answerDelay)
self.driver.find_element_by_class_name(self.CVT_SHAPE_CLASSNAME.get(self.answerDict.get(questionNumber))).click()
except Exception:
self.logger.exception("Failed to answer question number: {}; Supposed Answer: {}. Execution will continue".format(questionNumber, self.answerDict.get(questionNumber)))
else:
self.logger.debug("Not In Question stage. waiting")
time.sleep(0.5)
self.logger.info("Destorying Session")
self.driver.quit()
def validateOptions(quizId, pin, username, delay, pgloadDelay):
if not quizId or len(quizId) != 36:
raise ValueError("Invalid UUID; Either UUID does not exist, or too short/long")
try:
int(pin)
except ValueError:
raise ValueError("Invalid Pin, Pin must be all digits")
try:
float(delay)
float(pgloadDelay)
except ValueError:
raise ValueError("Invalid Answer Delay/Page Load Delay, It must be a positive decimal")
def loadSettingsFromIni():
cfg = ConfigParser()
cfg.read("kahoot.ini")
mainCfg = cfg["config"]
uuid = mainCfg["QuizUUID"]
pin = mainCfg["GamePIN"]
username = mainCfg["Username"]
delay = mainCfg["AnswerDelay"]
pageLoadDelay = mainCfg["pgLoadDelay"]
validateOptions(uuid, pin, username, delay, pageLoadDelay)
return (pin, username, float(delay), uuid, float(pageLoadDelay))
def loadSettingsFromUserInput():
quizUUID = input("Game UUID: ")
gamePin = input("Game Pin: ")
username = input("Username: ")
delay = input("Answer Delay: ")
pgLoadDelay = input("Click Element delay after page load: ")
validateOptions(quizUUID, gamePin, username, delay, pgLoadDelay);
return (gamePin, username, float(delay), quizUUID, float(pgLoadDelay))
def getQuizAnswers(quizUUID, loadDelay):
GA = GetAnswers(quizUUID, loadDelay)
GA.getQuizPage()
return GA.getQuizAnswers()
def doGame(pin:str, username:str, delay:float, uuid:str, pageLoadDelay:float):
AP = AutoPlay(pin, username, delay, getQuizAnswers(uuid, pageLoadDelay), pageLoadDelay)
AP.enterGame()
AP.doGameLoop()
def main():
try:
with open("kahoot.ini"): pass
use = input("Kahoot.ini configuration found. use it? (y/n)")
if (use.lower() == "y"):
doGame(*loadSettingsFromIni())
else:
doGame(*loadSettingsFromUserInput())
except (FileNotFoundError, PermissionError):
print("Unable to find kahoot.ini; defaulting to user input;")
doGame(*loadSettingsFromUserInput())
try:
main()
except Exception as e:
print("Failed to run; Exception: {}".format(e))
print("Press Enter to exit")
input(); raise SystemExit(1);