35 lines
975 B
Python
35 lines
975 B
Python
from json import dump
|
|
from time import sleep
|
|
from base64 import b64decode
|
|
import requests
|
|
|
|
api_url = "https://opentdb.com/api.php"
|
|
token_url = "https://opentdb.com/api_token.php?command=request"
|
|
|
|
file_path = "trivia.questions"
|
|
|
|
questions = []
|
|
|
|
def run():
|
|
global questions
|
|
token = requests.get(token_url).json()["token"]
|
|
params = {
|
|
"amount": 50,
|
|
"type": "boolean",
|
|
"encode": "base64",
|
|
"token": token
|
|
}
|
|
while True:
|
|
r = requests.get(api_url, params=params).json()
|
|
if r["response_code"] != 0:
|
|
print(r)
|
|
break
|
|
for question in r["results"]:
|
|
question_text = b64decode(question["question"]).decode("utf-8")
|
|
question_answer = b64decode(question["correct_answer"]).decode("utf-8").lower()
|
|
questions.append((question_text, question_answer))
|
|
print(len(questions))
|
|
sleep(6)
|
|
with open(file_path, "w") as f:
|
|
dump(questions, f)
|