-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathplot_course_viewed_by.py
More file actions
52 lines (41 loc) · 1.57 KB
/
plot_course_viewed_by.py
File metadata and controls
52 lines (41 loc) · 1.57 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
import json
import requests
import matplotlib.pyplot as plt
client_id = "..."
client_secret = "..."
auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
resp = requests.post('https://stepik.org/oauth2/token/',
data={'grant_type': 'client_credentials'},
auth=auth)
token = json.loads(resp.text)["access_token"]
def make_stepik_api_call_pk(name, pk, key):
api_url = 'https://stepik.org/api/{}/{}'.format(name, pk)
res = json.loads(
requests.get(api_url,
headers={'Authorization': 'Bearer '+ token}).text
)[name][0][key]
return res
def get_all_steps_viewed_by(course):
sections = make_stepik_api_call_pk("courses", course, "sections")
units = [unit
for section in sections
for unit in
make_stepik_api_call_pk("sections", section, "units")]
assignments = [assignment
for unit in units
for assignment in
make_stepik_api_call_pk("units", unit, "assignments")]
steps = [make_stepik_api_call_pk("assignments", assignment, "step")
for assignment in assignments]
viewed_by = [make_stepik_api_call_pk("steps", step, "viewed_by")
for step in steps]
return viewed_by
def main():
# This script gets view statistics for all of the steps in some course
# and then displays it. The trend is usually (naturally) declining
course = 187
viewed_by = get_all_steps_viewed_by(course)
plt.plot(viewed_by)
plt.show()
if __name__ == '__main__':
main()