-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathget_user_courses.py
More file actions
65 lines (54 loc) · 1.85 KB
/
get_user_courses.py
File metadata and controls
65 lines (54 loc) · 1.85 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
import json
import requests
client_id = "..."
client_secret = "..."
class StepikAPI(object):
def __init__(self, client_id, client_secret):
""" """
self.api_url = 'https://stepik.org/api/'
self.client_id = client_id
self.client_secret = client_secret
try:
auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
resp = requests.post('https://stepik.org/oauth2/token/',
data={'grant_type': 'client_credentials'},
auth=auth
).json()
self.token = resp['access_token']
except:
print("Error while obtaining token")
def get(self, url):
""" """
try:
resp = requests.get(url, headers={'Authorization': 'Bearer ' + self.token}).json()
except:
print("Error while getting data")
resp = None
return resp
def course_subscriptions(self, page = 1):
url = self.api_url + 'course-subscriptions?page={}'.format(page)
return self.get(url)
def course(self, course_id):
url = self.api_url + 'courses/{}'.format(course_id)
return self.get(url)
sapi = StepikAPI(client_id, client_secret)
#Example of getting list of user's courses
has_next = True
page = 0
course_ids = []
while has_next:
page += 1
courses = sapi.course_subscriptions(page)
has_next = courses['meta']['has_next']
for el in courses['course-subscriptions']:
course_ids.append(el['course'])
print("pages: %d"%page)
print("course ids: %s"%str(course_ids))
#For each course get its info
data = []
for course_id in course_ids:
course_data = sapi.course(course_id)
summary = (course_data['courses'][0]['summary'])
title = (course_data['courses'][0]['title'])
data.append((course_id, title, summary))
print(data)