-
Notifications
You must be signed in to change notification settings - Fork 0
/
questionnaire_import.py
65 lines (52 loc) · 2.75 KB
/
questionnaire_import.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
import os
import requests
import json
import unicodedata
json_questionnaires_folder = 'json_questionnaires'
# This wrong URL must be handle in the program
# ("Bande dessinée", "Tintin", "https://www.kiwime.com/oqdb/files/2124627384/OpenQuizzDB_124/openquizzdb_124.json"),
#
open_quizz_db_data = (
("Animaux", "Les chats", "https://www.codeavecjonathan.com/res/mission/openquizzdb_50.json"),
("Arts", "Musée du Louvre", "https://www.codeavecjonathan.com/res/mission/openquizzdb_86.json"),
("Bande dessinée", "Tintin", "https://www.kiwime.com/oqdb/files/2124627384/OpenQuizzDB_124/openquizzdb_124.json"),
("Cinéma", "Alien", "https://www.codeavecjonathan.com/res/mission/openquizzdb_241.json"),
("Cinéma", "Star wars", "https://www.codeavecjonathan.com/res/mission/openquizzdb_90.json"),
)
def strip_accents(s):
return ''.join(c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn')
def get_quizz_filename(categorie, titre, difficulte):
return strip_accents(categorie).lower().replace(" ", "") + "_" + strip_accents(titre).lower().replace(" ", "") + "_" + strip_accents(difficulte).lower().replace(" ", "") + ".json"
def generate_json_file(categorie, titre, url):
out_questionnaire_data = {"categorie": categorie, "titre": titre, "questions": []}
out_questions_data = []
response = requests.get(url)
if response.status_code==404:
print(f"Url Not found for title : {titre} , url : {url}")
else:
try:
data = json.loads(response.text)
except:
print(f"Error no data for title : {titre} , url : {url}")
else:
all_quizz = data["quizz"]["fr"]
for quizz_title, quizz_data in all_quizz.items():
out_filename = get_quizz_filename(categorie, titre, quizz_title)
print(out_filename)
out_questionnaire_data["difficulte"] = quizz_title
for question in quizz_data:
question_dict = {}
question_dict["titre"] = question["question"]
question_dict["choix"] = []
for ch in question["propositions"]:
question_dict["choix"].append((ch, ch==question["réponse"]))
out_questions_data.append(question_dict)
out_questionnaire_data["questions"] = out_questions_data
out_json = json.dumps(out_questionnaire_data)
os.makedirs(json_questionnaires_folder, exist_ok=True)
file = open(os.path.join(json_questionnaires_folder,out_filename), "w")
file.write(out_json)
file.close()
print("end")
for quizz_data in open_quizz_db_data:
generate_json_file(quizz_data[0], quizz_data[1], quizz_data[2])