forked from bsiever/canvas-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetCourseSchedule.py
More file actions
executable file
·82 lines (67 loc) · 2.68 KB
/
Copy pathgetCourseSchedule.py
File metadata and controls
executable file
·82 lines (67 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/local/bin/python3
# TODO: Update above if needed
import sys
from canvasapi import Canvas
import json # For files
import datetime
import sys # sys.stdout and sys.argv
from CanvasSettings import * # Import COURSE_ID, API_URL, API_KEY
# Check for arguments
if len(sys.argv)>2:
print(f"Arguments: file name to write to or none (and .json data written to stdout)")
exit(1)
if len(sys.argv)==2:
# Check if no file and can be opened for writing
f = open(sys.argv[1],"x")
else:
f = sys.stdout
# Initialize a new Canvas object
canvas = Canvas(API_URL, API_KEY)
course = canvas.get_course(COURSE_ID)
data = dict()
# Convert a UTC time to a date time object in the local time zone
def utcTimeStrToLocalDatetime(utcTime):
if utcTime == None:
return None
# https://stackoverflow.com/questions/4563272/how-to-convert-a-utc-datetime-to-a-local-datetime-using-only-standard-library/13287083#13287083
dt = datetime.datetime.strptime(utcTime, '%Y-%m-%dT%H:%M:%SZ')
dateStr = dt.replace(tzinfo=datetime.timezone.utc).astimezone(tz=None)
return str(dateStr)[:-6]
# Get all data about assignments
assignments = [a for a in course.get_assignments()]
assignmentData = dict()
for a in assignments:
name = a.name
assignmentData[name] = dict()
assignmentData[name]["due_at"] = utcTimeStrToLocalDatetime(a.due_at)
assignmentData[name]["lock_at"] = utcTimeStrToLocalDatetime(a.lock_at)
assignmentData[name]["unlock_at"] = utcTimeStrToLocalDatetime(a.unlock_at)
overrides = [o for o in a.get_overrides()]
overidesDict = dict()
for o in overrides:
# Only get the section-level overrides (not individual students/etc.)
if hasattr(o,"course_section_id"):
forTitle = dict()
forTitle["due_at"] = utcTimeStrToLocalDatetime(o.due_at)
forTitle["lock_at"] = utcTimeStrToLocalDatetime(o.lock_at) if hasattr(o,"lock_at") else ""
forTitle["unlock_at"] = utcTimeStrToLocalDatetime(o.unlock_at) if hasattr(o,"unlock_at") else ""
overidesDict[o.title] = forTitle
# Only add overrides if there was at least one
if len(overidesDict)>0:
assignmentData[name]["overrides"] = overidesDict
# Add to the overall data for the JSON
data["assignments"] = assignmentData
pages = [p for p in course.get_pages()]
todoDict = dict()
todoCount = 0
for p in pages:
name = p.title
todoDate = utcTimeStrToLocalDatetime(p.todo_date)
if todoDate!=None:
todoCount = todoCount+1
todoDict[name] = todoDate
# Save the data for the JSON (if there's at least one)
if todoCount>0:
data["page todo dates"] = todoDict
json.dump(data,f, default=str, indent=4, sort_keys=True)
f.close()