This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKahootClass.py
103 lines (86 loc) · 6.07 KB
/
KahootClass.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
from urllib.request import urlopen
from urllib.error import HTTPError
from json import load
from http.client import InvalidURL
import re
api = "https://play.kahoot.it/rest/kahoots/"
class Kahoot:
def __init__(self, uuid):
self.uuid = uuid
try:
# if [' ', '/', '\\'] in str(uuid):
if not re.fullmatch(r"^[A-Za-z0-9-]*$", uuid):
self.data = False
else:
self.data = load(urlopen(f"https://play.kahoot.it/rest/kahoots/{uuid}"))
except HTTPError or InvalidURL:
self.data = False
def get_quiz_details(self):
return {
"uuid": self.data["uuid"],
"creator_username": self.data["creator_username"],
"title": self.data["title"],
"description": self.data["description"],
"cover": self.data["cover"]}
def get_questions(self):
return self.data["questions"]
def get_question_names(self):
questions = []
for i in range(self.get_quiz_length()):
if self.get_question_details(i)["type"] == "content":
questions.append(self.get_question_details(i)["title"])
else:
questions.append(self.get_question_details(i)["question"])
return questions
def get_quiz_length(self):
return len(self.data["questions"])
def get_question_details(self, question):
if self.data["questions"][question]["type"] == "content":
data = {
"type": "content",
"title": self.data["questions"][question]["title"],
"description": self.data["questions"][question]["description"]
}
else:
data = {
"type": self.data["questions"][question]["type"],
"question": str(self.data["questions"][question]["question"]).replace('"', '\\"').replace("<p>", "").replace("</p>", "").replace("<strong>", "").replace("</strong>", "").replace("<br/>", "\n").replace("</span>", "").replace("</mo>", "").replace("</mrow>", "").replace("<mn>", "").replace("</mn>", "").replace("</annotation>", "").replace("</semantics>", "").replace("</math>", "").replace("<span>", "").replace("<math>", "").replace("<semantics>", "").replace("<mrow>", "").replace("<mo>", "").replace("<msup>", "").replace("<mi>", "").replace("</mi>", "").replace("</msup>", "").replace("<b>", "").replace("</b>", ""),
"choices": self.data["questions"][question]["choices"],
"amount_of_answers": len(self.data["questions"][question]["choices"]),
"amount_of_correct_answers": 0}
for i in range(len(self.data["questions"][question]["choices"])):
self.data["questions"][question]["choices"][i]["answer"] = self.data["questions"][question]["choices"][i]["answer"].replace('"', '\\"').replace("<p>", "").replace("</p>", "").replace("<strong>", "").replace("</strong>", "").replace("<br/>", "\n").replace("</span>", "").replace("</mo>", "").replace("</mrow>", "").replace("<mn>", "").replace("</mn>", "").replace("</annotation>", "").replace("</semantics>", "").replace("</math>", "").replace("<span>", "").replace("<math>", "").replace("<semantics>", "").replace("<mrow>", "").replace("<mo>", "").replace("<msup>", "").replace("<mi>", "").replace("</mi>", "").replace("</msup>", "").replace("<b>", "").replace("</b>", "")
for i in range(len(self.data["questions"][question]["choices"])):
if self.data["questions"][question]["choices"][i]["correct"]:
data["amount_of_correct_answers"] += 1
if "layout" in self.data["questions"][question]:
data["layout"] = self.data["questions"][question]["layout"]
else:
data["layout"] = None
if "image" in self.data["questions"][question]:
data["image"] = self.data["questions"][question]["image"]
else:
data["image"] = None
if "pointsMultiplier" in self.data["questions"][question]:
data["pointsMultiplier"] = self.data["questions"][question]["pointsMultiplier"]
else:
data["pointsMultiplier"] = None
if "time" in self.data["questions"][question]:
data["time"] = self.data["questions"][question]["time"]
else:
data["time"] = None
return data
def get_answer(self, question):
answers = []
if self.get_question_details(question)["type"] == "content":
answers = None
elif self.get_question_details(question)["type"] == "jumble":
for i in self.get_question_details(question)["choices"]:
answers.append(str(i["answer"]).replace('"', '\\"').replace("<p>", "").replace("</p>", "").replace("<strong>", "").replace("</strong>", "").replace("<br/>", "\n").replace("</span>", "").replace("</mo>", "").replace("</mrow>", "").replace("<mn>", "").replace("</mn>", "").replace("</annotation>", "").replace("</semantics>", "").replace("</math>", "").replace("<span>", "").replace("<math>", "").replace("<semantics>", "").replace("<mrow>", "").replace("<mo>", "").replace("<msup>", "").replace("<mi>", "").replace("</mi>", "").replace("</msup>", "").replace("<b>", "").replace("</b>", ""))
else:
for i in self.get_question_details(question)["choices"]:
if i["correct"]:
answers.append(str(i["answer"]).replace('"', '\\"').replace("<p>", "").replace("</p>", "").replace("<strong>", "").replace("</strong>", "").replace("<br/>", "\n").replace("</span>", "").replace("</mo>", "").replace("</mrow>", "").replace("<mn>", "").replace("</mn>", "").replace("</annotation>", "").replace("</semantics>", "").replace("</math>", "").replace("<span>", "").replace("<math>", "").replace("<semantics>", "").replace("<mrow>", "").replace("<mo>", "").replace("<msup>", "").replace("<mi>", "").replace("</mi>", "").replace("</msup>", "").replace("<b>", "").replace("</b>", ""))
if len(answers) == 0:
answers = None
return answers