#!/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()