-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestions.py
49 lines (35 loc) · 1.06 KB
/
questions.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
import csv
import json
import os.path
import random
from flask import Flask
from flask_cors import CORS
USED_FILENAME = "used.json"
app = Flask(__name__)
CORS(app)
def load_questions():
with open("questions.csv") as csvfile:
reader = csv.DictReader(csvfile)
questions = list(reader)
for i, q in enumerate(questions):
questions[i]["id"] = i
return questions
def load_used():
return json.load(open(USED_FILENAME))
def mark_used(i):
used = load_used()
used.append(i)
json.dump(used, open(USED_FILENAME, "w"))
def filter_used(questions):
used = set(load_used())
return [i for i in questions if i["id"] not in used]
@app.route("/question", methods=["GET"])
def question():
if not os.path.exists(USED_FILENAME):
json.dump([], open(USED_FILENAME, "w"))
questions = filter_used(load_questions())
print(f"questions left {len(questions)}")
# random.shuffle(questions)
q = questions.pop(0)
mark_used(q["id"])
return json.dumps({"question": q["question"], "answer": q["answer"]})