-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathrecommendations_top_example.py
More file actions
87 lines (58 loc) · 2.51 KB
/
recommendations_top_example.py
File metadata and controls
87 lines (58 loc) · 2.51 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
import sys
import json
import requests
from collections import Counter
api_url = 'https://stepik.org/api/'
def get_api_requests(topic, pages_max_num=None):
result = []
page_num = 1
while True if pages_max_num is None else page_num <= pages_max_num:
request_url = (api_url + topic + '?page={}').format(page_num)
request_data = json.loads(requests.get(request_url, headers={'Authorization': 'Bearer '+ token}).text)
request_result = request_data[topic]
result.extend(request_result)
if not request_data['meta']['has_next']:
break
else:
page_num += 1
return result
def get_recommendation_reactions_lessons_count(pages_max_num=None):
recommendation_reactions = get_api_requests('recommendation-reactions', pages_max_num)
recommendation_reactions_lessons = [item['lesson'] for item in recommendation_reactions]
return Counter(recommendation_reactions_lessons)
def get_top_lessons(recommendations, n, titles=False):
sorted_keys = [key for (key, value) in sorted(lessons_top.items(), key=lambda x: x[1], reverse=True)]
top_n_keys = sorted_keys[:n]
if titles:
lessons = get_api_requests('lessons')
top_lessons_list = []
for id in top_n_keys:
for lesson in lessons:
if lesson['id'] == id:
top_lessons_list.append((id, lesson['title'], recommendations[id]))
break
else:
top_lessons_list = [(id, recommendations[id]) for id in top_n_keys]
return top_lessons_list
def print_scores(top_lessons, titles=False):
for lesson in top_lessons:
lesson_id = lesson[0]
num_recs = lesson[2] if titles else lesson[1]
if titles:
title = lesson[1]
out_str = 'Course-ID: {0}, title: {1}, #recs: {2}'.format(lesson_id, title, num_recs)
else:
out_str = 'Course-ID: {0}, #recs: {1}'.format(lesson_id, num_recs)
print(out_str)
if __name__ == "__main__":
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']
lessons_top = get_recommendation_reactions_lessons_count(pages_max_num=50)
lessons_top_10 = get_top_lessons(lessons_top, 10)
print_scores(lessons_top_10)