-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvasData.py
More file actions
92 lines (71 loc) · 3.45 KB
/
CanvasData.py
File metadata and controls
92 lines (71 loc) · 3.45 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
83
84
85
86
87
88
89
90
91
92
"""
---------------------------------------------------------------------------------
This file extracts relevant information 'userid,username,score,total,assignment,
assignmentid,module' of each assignment and store them in the dictionary
'Assignments' and the list 'users'. Run this file to generate a text file of
raw input 'received.txt', and two simplified text files 'modAssignment.txt'
and 'modUsers.txt' to get a better understanding of the data structure.
---------------------------------------------------------------------------------
Contact anyone of us if you need furthur explanation:
Hao Yang hyang46@asu.edu
Chun Yang cyang114@asu.edu
Logan Cousins lcousins@asu.edu
Brandon Grossnickle bgrossni@asu.edu
--------------------------------------------------------------------------------
"""
import json
import requests
import html2text
def grab_canvas_data():
# curl standard format
# curl -H "Authorization: Bearer <ACCESS-TOKEN>" "https://canvas.instructure.com/api/v1/courses"
# course url
url = "https://asu.instructure.com/api/v1/courses/18732/assignments?per_page=200"
headers = {"Authorization": "Bearer " + "7236~vh5XQQveDqwkvzPvhzsK9IivIdSmUDKY3FarvXAiY0xUpeCGhFmXkjKzMu67yYcc"}
response = requests.get(url, headers = headers)
# dictionary
data = json.loads(response.text)
# for grades only
url2 = "https://asu.instructure.com/api/v1/courses/18732/gradebook_history/feed?per_page=200"
response2 = requests.get(url2, headers = headers)
data2 = json.loads(response2.text)
# original raw data stored locally for inspection
receivedText = open("received.txt","w")
receivedText.write(response2.text)
receivedText.close()
assignments = []
users = []
for dictionary in data:
for dictionary2 in data2:
## assignment, assignment id, score, total, module
newEntry = {}
newEntry["user_id"] = None
newEntry["score"] = None
newEntry["total"] = 0
if dictionary["name"]:
newEntry["assignment"] = dictionary["name"]
if dictionary["id"]:
newEntry["assignment_id"] = dictionary["id"]
if dictionary2["assignment_id"] == dictionary["id"]:
newEntry["score"] = dictionary2["current_grade"]
if dictionary2["user_id"]:
if dictionary2["user_id"] not in users:
users.append(dictionary2["user_id"])
newEntry["user_id"] = dictionary2["user_id"]
newEntry["user_name"] = dictionary2["user_name"]
if dictionary["points_possible"]:
newEntry['total'] = dictionary["points_possible"]
if dictionary["position"]:
newEntry["module"] = dictionary["position"]
if newEntry["total"] != 0 and newEntry["score"] != None and newEntry["user_id"] != None:
assignments.append(newEntry)
# stored locally for inspection and improvement
# one dictionary output format {"user_id": 4249, "score": "250", "total": 250.0,
# "assignment": "Systems Analysis Paper", "assignment_id": 372387, "module": 1}
with open('modAssignment.txt','w') as filehandle:
json.dump(assignments,filehandle)
# all students' user id [4249, 266746, 333854]
with open('modUsers.txt','w') as filehandle:
json.dump(users,filehandle)
return (assignments, users)
grab_canvas_data()