-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestionLoader.py
More file actions
100 lines (77 loc) · 2.87 KB
/
Copy pathquestionLoader.py
File metadata and controls
100 lines (77 loc) · 2.87 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
###################################################
# Alejandro de los Santos Puerto
# alejandrodlsp.com
###################################################
import requests
import json
# Current session token, 0 if nil
session_token = 0
#
# requests data from API and returns it as a string
def request_data(amount, category, difficulty, question_type):
if session_token == 0 or session_token == None:
get_session()
url = "https://opentdb.com/api.php?amount=" + amount + "&category=" + category + "&difficulty=" + difficulty + "&type=" + question_type + "&token=" + str(session_token)
response = requests.get(url=url)
data = response.text
return data
#
# requests a session to the opentdb API and saves it
def get_session():
url = "https://opentdb.com/api_token.php?command=request" # API token request url
response = requests.get(url = url)
data = json.loads(response.text)
# parse response code
if data["response_code"] == 0:
session_token = data["token"]
print("Session token: " + str(session_token) + "\n")
else:
show_error("Session error", "Could not retrieve a session token")
#
# shows an error message
def show_error(title, error):
print(title + " : " + error)
sys.exit()
###
# question set class
class question_set():
#
# class constructor
def __init__(self, amount, category, difficulty, question_type):
while True:
self.data = request_data(amount, category, difficulty, question_type) # request data to api
self.__dict__ = json.loads(self.data) # deserialize raw json data into itself
if self.response_code == 1: # parse response code
show_error("No Results", "Request could not return results")
elif self.response_code == 2:
show_error("Invalid Parameter", "Request contains an invalid parameter")
elif self.response_code == 3:
show_error("Token Not Found", "Request session token could not be found, attempting to find another session")
get_session()
elif self.response_code == 4:
show_error("Empty Session error", "Session is empty, attempting to find another session")
get_session()
elif self.response_code == 0:
break
# save request serialized results
self.set = self.Results
#
# returns a list of the question objects
def get(self):
questions = []
# loop through all the request result objects and create a question object out of each one
for i in range(0,len(self.set)):
question_text = self.set[i]["question"]
correct_answer = self.set[i]["correct_answer"]
incorrect_answers = self.set[i]["incorrect_answers"]
qs = question(question_text, correct_answer, incorrect_answers)
questions.append(qs)
#return the list of question objects
return questions
###
# question class
class question():
def __init__(self, question, correct_answer, incorrect_answers):
self.question = question
self.correct_answer = correct_answer
self.incorrect_answers = incorrect_answers