Add canvas homework retrieval script

This commit is contained in:
Adam Goldsmith 2018-03-12 11:58:02 -04:00
parent bdd76830ed
commit 23e6b4ec88
2 changed files with 49 additions and 0 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/__pycache__/
/.canvas_auth

48
canvas Executable file
View File

@ -0,0 +1,48 @@
#!/usr/bin/env python3
import subprocess
import sys
import os
import requests
s = requests.Session()
# auth should be in a file called '.canvas_auth' in the same dir
# should be of the form 'Bearer <auth code>'
with open(os.path.dirname(os.path.realpath(__file__)) + '/.canvas_auth') as f:
s.headers.update({"Authorization": f.read().strip()})
def do_update():
courses = s.get("https://canvas.wpi.edu/api/v1/courses",
params={'enrollment_state': 'active'}).json()
did_change = False
for course in courses:
assignments = s.get(
f"https://canvas.wpi.edu/api/v1/courses/{course['id']}/assignments",
params={'bucket': 'future'}).json()
for assignment in assignments:
if assignment['due_at'] is not None:
course_code = course['course_code'].split('-')[0]
date_str = assignment['due_at'][:10] # just take %Y-%m-%d
todo_string = f"(B) +hw class:{course_code} {assignment['name']} due:{date_str}"
command = ["todo.sh", "listall", todo_string]
lines = subprocess.check_output(command).decode(encoding='UTF-8').split('\n')[:-5]
if len(lines) < 1: # doesn't exist in todo.txt yet
subprocess.call(["todo.sh", "add", todo_string])
did_change = True
if not did_change:
print("No changes to make")
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == "usage":
print(" canvasUpdate\n"
" Adds all assignments from canvas that do not already exist")
exit(0)
if len(sys.argv) >= 3:
print("Too many arguments!")
exit(-1)
else:
do_update()