Skip to content

Instantly share code, notes, and snippets.

@jbaranski
Last active December 28, 2024 00:52
Show Gist options
  • Save jbaranski/5419c049af1989c1808a71bc73c9f3f4 to your computer and use it in GitHub Desktop.
Save jbaranski/5419c049af1989c1808a71bc73c9f3f4 to your computer and use it in GitHub Desktop.
Download all questions from Open Trivia DB (opentdb.com)
import requests
import json
import time
if __name__ == '__main__':
db = {}
token = requests.get('https://opentdb.com/api_token.php?command=request').json()['token']
while True:
r = requests.get(f'https://opentdb.com/api.php?amount=50&token={token}')
r_json = r.json()
if 'results' not in r_json or len(r_json['results']) == 0:
print('finished or rate limited', r.status_code)
break
results = r.json()['results']
for result in results:
db[result['question']] = result
len_db = len(list(db.values()))
# Heavy rate limiting in place: https://forums.pixeltailgames.com/t/download-rate-limited/49325
time.sleep(5)
print(f'Num questions fecthed: {len_db}')
with open('db.json', 'w') as f:
json.dump(list(db.values()), f)
@jbaranski
Copy link
Author

jbaranski commented Dec 28, 2024

A dump of the output this script produces can be found here, which was generated on 12/27/2024: https://gist.github.com/jbaranski/a3c10856b750441663eec71739d49e43

Data license: https://creativecommons.org/licenses/by-sa/4.0/ (from https://opentdb.com/)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment