-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmute_all_assignments.py
85 lines (68 loc) · 3.2 KB
/
mute_all_assignments.py
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
83
84
85
# -*- coding: utf-8 -*-
"""
Mute all assingments in courses listed in a csv file.
@author: Adam Dixon
"""
try:
import requests
except:
import pip
pip.main(['install', 'requests'])
import requests
try:
import pandas as pd
except:
import pip
pip.main(['install', 'pandas'])
import pandas as pd
try:
import json
except:
import pip
pip.main(['install', 'json'])
import json
url = "https://canvas.ubc.ca/api/v1/"
with open('Canvas API Token.txt') as f:
token = f.read()
# ===============================================================================================================
# @param : csv_file_name is a string that is not empty and has ".csv" at the end of it
# @effects : mutes and prints all assignments in the course
# ===============================================================================================================
def mute_all_csv(csv_file_name):
ss = pd.read_csv(csv_file_name)
for idx, row in ss.iterrows():
a_list = get_assignment_list(row['courses'])
mute_all_assignments(a_list, row['courses'])
print(get_all_assignment_names(a_list))
# ===============================================================================================================
# @param : course_id is a valid Canvas course that you have access to
# @returns : a list of all assignment objects from that course
# ===============================================================================================================
def get_assignment_list(course_id):
r = requests.get(url + "courses/" + str(course_id) + "/assignments?per_page=100",
headers = {'Authorization': 'Bearer ' + str(token)})
assignment_list = json.loads(r.text)
return assignment_list
# ===============================================================================================================
# @param : assignment_list, a list of assignment objects from a course
# @returns : a string with all assignment names separated by new lines
# ===============================================================================================================
def get_all_assignment_names(assignment_list):
s = ""
for idx, val in enumerate(assignment_list):
s = s + str(assignment_list[idx]['name']) + "\n"
return s
# ===============================================================================================================
# @param : a_list is a list of assignments in a course
# @effects: utes all assignments in a_list
# ===============================================================================================================
def mute_all_assignments(a_list, course_id):
for idx, val in enumerate(a_list):
requests.put(url + "courses/" + str(course_id) + "/assignments/" + str(a_list[idx]['id']),
headers = {'Authorization': 'Bearer ' + str(token)},
params = {"assignment[muted]" : True})
return None
print()
print("Make sure you have copy and pasted your token into the file 'Canvas API token.txt'")
mute_all_csv("courses.csv")
print("Complete!")