forked from the-trivia-api/vue-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpythonscrapingScript.py
More file actions
103 lines (89 loc) · 3.08 KB
/
Copy pathpythonscrapingScript.py
File metadata and controls
103 lines (89 loc) · 3.08 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
import requests
import json
from time import sleep
# url="https://the-trivia-api.com/v2/questions?tags=food&limit=1"
# questions=[]
# i = 0
# while i < 300:
# r= requests.get(url, timeout=5)
# question= r.json()[0]
# print(question)
# if question in questions:
# print("it's already here")
# else:
# print("new question, adding it to the list if questions")
# questions.append(question)
# i+=1
# print(i)
# sleep(2)
# # write the set of questions to a json file
# json_questions=json.dumps(questions, indent=4)
# with open ("questions.json", "w") as file:
# file.write(json_questions)
# with open ("public/questions/italianTranslation.json", "r") as jsonfile:
# data=json.load(jsonfile)
# with open("domande.txt", "w") as textfile:
# textfile.write("totale domande: "+ str(len(data))+ "\n")
# for question in data:
# textfile.write("\n")
# textfile.write(f"{data.index(question)+ 1}: {question['question']['text']} \n")
# textfile.write(f" risposta corretta: {question['correctAnswer']} \n")
# textfile.write(f" risposte sbagliate: {question['incorrectAnswers']} \n")
Lokal_questions=[]
question_blueprint={
"category": "società_e_cultura",
"id": "622a1c3d7cc59eab6f951c17",
"correctAnswer": "Rapa",
"incorrectAnswers": [
"zucche",
"Patata",
"Carota"
],
"question": {
"text": "Da cosa furono realizzate le prime Jack-o-Lantern?"
},
"tags": [
"cibo",
"Halloween",
"tradizioni",
"società_e_cultura"
],
"type": "scelta_testo",
"difficulty": "difficile",
"regions": [],
"isNiche": False
}
r=requests.get("https://server.opexams.com/quiz-data/EkUQ0vM8wX2", timeout=5)
data=r.json()
generated_questions=data["questions"]
for question in generated_questions:
question.pop("id")
domanda=question["question"]
risposta_corretta=question["answer"]
options=question["options"]
wrong_answers=[option["value"] for option in options]
wrong_answers.remove(risposta_corretta)
# build the json object:
question_object={
"category": "any string is fine",
"id": "any string is fine",
"correctAnswer": risposta_corretta,
"incorrectAnswers": wrong_answers,
"question": {
"text": domanda
},
"tags": [
"cibo",
"whatever",
"you ",
"want"
],
"type": "scelta_testo",
"difficulty": "difficile",
"regions": [],
"isNiche": False
}
Lokal_questions.append(question_object)
Lokal_questions_json=json.dumps(Lokal_questions, indent=4)
with open("lokal-questions.json", "w") as file:
file.write(Lokal_questions_json)