-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathcopy_paste_course.py
More file actions
218 lines (184 loc) · 7.84 KB
/
copy_paste_course.py
File metadata and controls
218 lines (184 loc) · 7.84 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Run with Python 3
# Clone course from one Stepik instance (domain) into another
import re
import os
import csv
import ast
import json
import requests
import datetime
# Enter parameters below:
# 1. Get your keys at https://stepik.org/oauth2/applications/
# (client type = confidential, authorization grant type = client credentials)
client_id = '...'
client_secret = '...'
api_host = 'https://stepik.org'
# client_id = '...'
# client_secret = '...'
# api_host = 'http://127.0.0.1' # save to localhost
course_id = 401
mode = 'SAVE' # IMPORTANT: use SAVE first, then use PASTE with uncommented (or changed) lines above (client keys and host)
cross_domain = True # to re-upload videos
# 2. Get a token
auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
response = requests.post('{}/oauth2/token/'.format(api_host),
data={'grant_type': 'client_credentials'},
auth=auth)
token = response.json().get('access_token', None)
if not token:
print('Unable to authorize with provided credentials')
exit(1)
# 3. Call API (https://stepik.org/api/docs/) using this token.
def fetch_object(obj_class, obj_id):
api_url = '{}/api/{}s/{}'.format(api_host, obj_class, obj_id)
response = requests.get(api_url,
headers={'Authorization': 'Bearer ' + token}).json()
return response['{}s'.format(obj_class)][0]
def fetch_objects(obj_class, obj_ids):
objs = []
# Fetch objects by 30 items,
# so we won't bump into HTTP request length limits
step_size = 30
for i in range(0, len(obj_ids), step_size):
obj_ids_slice = obj_ids[i:i + step_size]
api_url = '{}/api/{}s?{}'.format(api_host, obj_class,
'&'.join('ids[]={}'.format(obj_id)
for obj_id in obj_ids_slice))
response = requests.get(api_url,
headers={'Authorization': 'Bearer ' + token}
).json()
objs += response['{}s'.format(obj_class)]
return objs
print('Mode:', mode)
print('Course ID:', course_id)
print()
if mode == 'SAVE': # SAVE TO DATA
course = fetch_object('course', course_id)
sections = fetch_objects('section', course['sections'])
unit_ids = [unit for section in sections for unit in section['units']]
units = fetch_objects('unit', unit_ids)
lessons_ids = [unit['lesson'] for unit in units]
lessons = fetch_objects('lesson', lessons_ids)
step_ids = [step for lesson in lessons for step in lesson['steps']]
steps = fetch_objects('step-source', step_ids)
data = []
idd = course['id']
course = { key: course[key] for key in ['title', 'summary', 'course_format', 'language', 'requirements', 'workload', 'is_public', 'description', 'certificate', 'target_audience'] }
row = ['course', idd, course]
data.append(row)
for section in sections:
idd = section['id']
section = { key: section[key] for key in ['title', 'position', 'course'] }
row = ['section', idd, section]
data.append(row)
for unit in units:
idd = unit['id']
unit = { key: unit[key] for key in ['position', 'section', 'lesson'] }
row = ['unit', idd, unit]
data.append(row)
for lesson in lessons:
idd = lesson['id']
lesson = { key: lesson[key] for key in ['title', 'is_public', 'language'] }
row = ['lesson', idd, lesson]
data.append(row)
for step in steps:
idd = step['id']
step = { key: step[key] for key in ['lesson', 'position', 'block', 'cost'] }
row = ['step-source', idd, step]
data.append(row)
# write data to file
csv_file = open('course-{}-dump.csv'.format(course_id), 'w')
csv_writer = csv.writer(csv_file)
csv_writer.writerows(data)
csv_file.close()
print('Objects:', len(data))
else: #PASTE FROM DATA
# read data to file
csv_file = open('course-{}-dump.csv'.format(course_id), 'r')
csv_reader = csv.reader(csv_file)
data = [row for row in csv_reader]
for row in data:
row[2] = ast.literal_eval(row[2]) # cast str to dict
row[1] = int(row[1])
csv_file.close()
print('Objects:', len(data))
print()
courses_map = {}
sections_map = {}
lessons_map = {}
for row in data:
if row[0] != 'lesson':
continue
api_url = '{}/api/lessons'.format(api_host)
api_data = { 'lesson' : row[2] }
r = requests.post(api_url, headers={'Authorization': 'Bearer '+ token}, json=api_data)
new_id = r.json()['lessons'][0]['id']
old_id = row[1]
lessons_map[old_id] = new_id
print('Lesson ID:', old_id, '-->', new_id)
for row in data:
if row[0] != 'step-source':
continue
# reupload video if needed
if cross_domain and row[2]['block']['name'] == 'video':
# find best video from the old step to upload it by url
video_urls = row[2]['block']['video']['urls']
best_quality = 0
best_url = ''
for url_and_quality in video_urls:
q = int(url_and_quality['quality'])
if q > best_quality:
best_quality = q
best_url = url_and_quality['url']
api_url = '{}/api/videos'.format(api_host)
api_data = {'source_url': best_url, 'lesson': lessons_map[row[2]['lesson']]}
r = requests.post(api_url, headers={'Authorization': 'Bearer '+ token}, data=api_data)
new_id = r.json()['videos'][0]['id']
old_id = row[2]['block']['video']['id']
row[2]['block']['video']['id'] = new_id
print('Video ID:', old_id, '-->', new_id)
api_url = '{}/api/step-sources'.format(api_host)
row[2]['lesson'] = lessons_map[row[2]['lesson']] # fix lesson id to new ones
if cross_domain: # hack for attachements, only works for when source domain is stepik.org
row[2]['block']['text'] = row[2]['block']['text'].replace('href="/media/attachments/', 'href="https://stepik.org/media/attachments/')
api_data = { 'stepSource' : row[2] }
r = requests.post(api_url, headers={'Authorization': 'Bearer '+ token}, json=api_data)
new_id = r.json()['step-sources'][0]['id']
old_id = row[1]
print('Step ID:', old_id, '-->', new_id)
for row in data:
if row[0] != 'course':
continue
api_url = '{}/api/courses'.format(api_host)
api_data = { 'course' : row[2] }
r = requests.post(api_url, headers={'Authorization': 'Bearer '+ token}, json=api_data)
new_id = r.json()['courses'][0]['id']
old_id = row[1]
courses_map[old_id] = new_id
print('Course ID:', old_id, '-->', new_id)
for row in data:
if row[0] != 'section':
continue
api_url = '{}/api/sections'.format(api_host)
row[2]['course'] = courses_map[row[2]['course']] # fix course id to new ones
api_data = { 'section' : row[2] }
r = requests.post(api_url, headers={'Authorization': 'Bearer '+ token}, json=api_data)
new_id = r.json()['sections'][0]['id']
old_id = row[1]
sections_map[old_id] = new_id
print('Section ID:', old_id, '-->', new_id)
for row in data:
if row[0] != 'unit':
continue
api_url = '{}/api/units'.format(api_host)
row[2]['section'] = sections_map[row[2]['section']] # fix section id to new ones
row[2]['lesson'] = lessons_map[row[2]['lesson']] # fix lesson id to new ones
api_data = { 'units' : row[2] }
r = requests.post(api_url, headers={'Authorization': 'Bearer '+ token}, json=api_data)
new_id = r.json()['units'][0]['id']
old_id = row[1]
print('Unit ID:', old_id, '-->', new_id)
print()
print(courses_map)
print()
print('Done!')