from flask import Flask, render_template
import requests
from datetime import datetime, timezone
app = Flask(__name__)
# prepare url and authorization key
base_url = 'https://poway.instructure.com/api/v1/'
headers = {
'Authorization': 'Bearer API_TOKEN'
}
# api endpoint decorator
@app.route('/')
def homework_list():
try:
# send a request to the AP CSP Canvas assignments Canvas API
response = requests.get(
base_url + 'courses/141645/assignments', headers=headers)
if response.status_code == 200:
# if the response is good, convert the response into json
assignments = response.json()
# get the current date and time in UTC timezone
current_date = datetime.now(timezone.utc)
# filter assignments with due dates in the future
future_assignments = [assignment for assignment in assignments if assignment.get(
'due_at') and datetime.fromisoformat(assignment['due_at']) > current_date]
# this actually displays the json data and future assignments in an HTML page
return render_template('homework-list.html', assignments=future_assignments)
else:
return " to retrieve assignments. Status code: " + str(response.status_code)
# runs if response code is not good
except Exception as e:
return "An error occurred: " + str(e)
if __name__ == '__main__':
app.run(debug=True)