From 23e6b4ec88aaa5087f044b94b07c7166d574baea Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 12 Mar 2018 11:58:02 -0400 Subject: [PATCH] Add canvas homework retrieval script --- .gitignore | 1 + canvas | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100755 canvas diff --git a/.gitignore b/.gitignore index a348e50..58c0f03 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /__pycache__/ +/.canvas_auth diff --git a/canvas b/canvas new file mode 100755 index 0000000..e15125c --- /dev/null +++ b/canvas @@ -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 ' +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()