Created
February 27, 2020 03:21
-
-
Save uint0/cb27b99ba99ff936346a32f8f05d7245 to your computer and use it in GitHub Desktop.
Creates a html file containing questions from a Udemy Practice Test
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
""" | |
Requirements: requests | |
Usage: python quiz_to_html.py <quiz_id> <access_token> | |
quiz_id can be found in the url of the practice problem typically after /quiz/ | |
it should be a numeric | |
access_token is the value of the access_token cookie | |
Output: A HTML file is outputted to stdout. This can be piped into a file if nessecary. | |
The file is not prettified. The content is in the format | |
Questions | |
Question 1 | |
<Prompt> | |
<Options> | |
--- | |
Question 2... | |
Answers | |
Question 1 | |
<Section> | |
<Correct answers> | |
<Explanation> | |
--- | |
Question 2... | |
""" | |
import requests | |
import string | |
def get_questions(quiz_id, access_token): | |
if any(c not in string.digits for c in quiz_id): | |
raise Exception("Quiz id should be numeric") | |
res = requests.get( | |
f'https://www.udemy.com/api-2.0/quizzes/{quiz_id}/assessments/', | |
params={ | |
'page_size': 250, | |
'fields[assessment]': 'id,assessment_type,prompt,correct_response,section,question_plain,related_lectures' | |
}, | |
headers={ | |
'Authorization': f'Bearer {access_token}' | |
} | |
) | |
if not res.ok: | |
return None | |
return res.json() | |
def generate_html(questions): | |
res = questions['results'] | |
html = "" | |
html += "<h1>Questions</h1>" | |
for i, question in enumerate(res): | |
html += f"<h2>Question {i+1}</h2>" | |
html += f"<div>{question['prompt']['question']}</div>" | |
html += "<ol>" | |
for answer in question['prompt']['answers']: | |
html += f"<li>{answer}</li>" | |
html += "</ol>" | |
html += "<hr>" | |
html += "<h1>Solutions</h1>" | |
for i, question in enumerate(res): | |
html += f"<h2>Question {i+1}</h2>" | |
html += f"<p><b>Section: </b>{question['section']}" | |
html += f"<p><b>Correct Responses: </b>{', '.join(question['correct_response'])}</p>" | |
html += f"<p><b>Explanation</b></p>" | |
html += f"<div>{question['prompt']['explanation']}</div>" | |
html += "<hr>" | |
return f"<html><head><title></title></head><body>{html}</body></html>" | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) < 3: | |
print(f'Usage: {sys.argv[0]} <quiz_id> <access_token>', file=sys.stderr) | |
else: | |
questions = get_questions(sys.argv[1], sys.argv[2]) | |
print(generate_html(questions)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment