-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (60 loc) · 2.34 KB
/
main.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
import json
import time
import requests
from exam import headers, exam_id, exam_url
# Get exam as JSON
def get_exam_details(url):
response = requests.request("GET", url, headers=headers)
with open('exam_details.json', 'w') as json_file:
json.dump(response.json(), json_file)
# Return the number of total questions in the exam
def get_num_of_questions():
with open('exam_details.json', 'r') as json_file:
data = json.load(json_file)
exam = data['exam']
print("Total exam questions:", exam['total_questions'])
return exam['total_questions']
# Make a request for each question from 1 to number of questions, save JSON response
def get_exam_questions():
questions = []
with open('exam_questions.json', 'w') as json_file:
json.dump([], json_file)
for question_id in range(1, get_num_of_questions() + 1):
question_url = f"https://www.boardvitals.com/api/rc/exams/{exam_id}/questions/{question_id}.json"
response = requests.request("GET", question_url, headers=headers)
questions.append({"id": question_id, "data": response.json()})
print("Question: ", question_id)
time.sleep(5)
with open('exam_questions.json', 'w') as json_file:
json.dump(questions, json_file)
print("Finished fetching exam questions")
# Get question/answer pairs into JSON
def get_exam_qa():
qa = []
with open('exam_qa.json', 'w') as json_file:
json.dump([], json_file)
with open('exam_questions.json', 'r') as json_file:
data = json.load(json_file)
for record in data:
question = record['data']['data']['question']
question = {
"qid": record['id'],
"question": question['name'],
"answer": get_question_answer(question),
"explanation": question['explanation']['name']
}
qa.append(question)
with open('exam_qa.json', 'w') as json_file:
json.dump(qa, json_file)
# Return the correct answer object from a question
def get_question_answer(question):
for answer in question['answers']:
if answer['type'] == "CorrectAnswer":
return answer
# Query exam URL and create question/answer JSON
def start():
get_exam_details(exam_url)
get_exam_questions()
get_exam_qa()
print("Finished")
start()