Last active
May 1, 2020 07:29
-
-
Save zeevox/e67830e8c05913459b1e80faa6a82118 to your computer and use it in GitHub Desktop.
Convert the BBC Quiz of the Week into an XLSX for Quizizz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import html | |
import xlsxwriter # pip install XlsxWriter | |
import dateutil.parser as parser | |
from datetime import date | |
flag_print_questions_to_console = False | |
def get_answer(options, options_keys): | |
options_keys = [str(k) for k in options_keys] | |
for k, v in options.items(): | |
if v == '1': | |
return options_keys.index(k) + 1 | |
return 0 | |
inp = input("BBC QotW date: ") | |
if inp.lower() == "today": | |
date = date.today() | |
else: | |
date = parser.parse(inp, dayfirst=True) | |
date_bbc = date.strftime("%d_%m_%y") | |
request = requests.get(f"https://www.bbc.co.uk/indepthtoolkit/quizzes/qotw_{date_bbc}") | |
questions = json.loads([line for line in request.text.split("\n") if "data:" in line][0].strip()[6:-1])['questions'] | |
to_write = [ | |
[ # column headers | |
"Question Text", | |
"Question Type", | |
"Option 1", | |
"Option 2", | |
"Option 3", | |
"Option 4", | |
"Option 5", | |
"Correct Answer", | |
"Time in seconds", | |
"Image Link" | |
] | |
] | |
date_formatted = date.strftime("%d %b %Y") | |
print(f"BBC Quiz of the Week - {date_formatted}") | |
print(f"Header image: {questions[0]['questionImg']}") | |
for question in questions: | |
if flag_print_questions_to_console: | |
print("\n") | |
print(question['questionImg']) | |
print(html.unescape(question['question'])[3:-4]) # convert html escape codes to text and remove <p> & </p> tags | |
print(*question['optionsKeys'], sep="\n") | |
print(f"Correct answer: {get_answer(question['options'], question['optionsKeys'])}") | |
to_write.append([ | |
html.unescape(question['question'])[3:-4], | |
"Multiple Choice", | |
question['optionsKeys'][0], | |
question['optionsKeys'][1], | |
question['optionsKeys'][2], | |
"", | |
"", | |
get_answer(question['options'], question['optionsKeys']), | |
300.0, | |
question['questionImg'] | |
]) | |
date_filename = date.strftime("%y_%m_%d") | |
workbook = xlsxwriter.Workbook(f'BBC_QotW_{date_filename}_Quizizz.xlsx') | |
worksheet = workbook.add_worksheet() | |
for row, entry in enumerate(to_write): | |
for col, value in enumerate(entry): | |
worksheet.write(row, col, value) | |
workbook.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment