This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
sync.py
executable file
·179 lines (144 loc) · 5.64 KB
/
sync.py
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
#!/usr/bin/env python
import argparse
import json
import logging
import yaml
try:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
import markdown
import requests
def parse_args():
parser = argparse.ArgumentParser(description='Update Udemy Course Data')
parser.add_argument('section', help='Which Udemy section to update')
parser.add_argument('--verbose', dest='verbose', action='store_true')
return parser.parse_args()
def setup_verbose_logging():
http_client.HTTPConnection.debuglevel = 1
# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def load_secrets():
secrets = load_yaml('secrets.yml')
return secrets
def load_yaml(filename):
with open(filename) as stream:
return yaml.load(stream)
def setup_basics(secrets):
"""
"""
basics = load_yaml('basics.yml')
basics_endpoint = "https://www.udemy.com/course-manage/edit-basics/?courseId=%d" % secrets['courseid']
headers = {'referer': basics_endpoint}
basics['csrfmiddlewaretoken'] = secrets['cookies']['csrfmiddlewaretoken']
print "Posting basics:"
print basics
r = requests.post(basics_endpoint,
data=basics, headers=headers, cookies=secrets['cookies'])
print r
r.raise_for_status()
def setup_details(secrets):
"""
"""
details = load_yaml('details.yml')
details_endpoint = "https://www.udemy.com/course-manage/edit-details/?courseId=%d" % secrets['courseid']
headers = {'referer': details_endpoint}
description_html = markdown.markdown(details['description'])
print "Posting Details"
description_dict = {
'description': str(description_html),
'submit': 'Save',
'csrfmiddlewaretoken': secrets['cookies']['csrfmiddlewaretoken'],
}
print description_dict
r = requests.post(details_endpoint, data=description_dict, headers=headers, cookies=secrets['cookies'])
print r
r.raise_for_status()
def setup_goals(secrets):
"""
"""
input_data = load_yaml('goals.yml')
endpoint = "https://www.udemy.com/course-manage/edit-goals-and-audience/?courseId=%d" % secrets['courseid']
headers = {'referer': endpoint}
print "Posting Data:"
output_data = {
'csrfmiddlewaretoken': secrets['cookies']['csrfmiddlewaretoken'],
'what_you_will_learn': json.dumps({"items": input_data['what_you_will_learn']}),
'who_should_attend': json.dumps({"items": input_data['who_should_attend']}),
'requirements': json.dumps({"items": input_data['requirements']}),
'instructional_level': input_data['instructional_level'],
'submit': 'Save',
}
print output_data
r = requests.post(endpoint, data=output_data, headers=headers, cookies=secrets['cookies'])
print r
r.raise_for_status()
def setup_chapter(title, description, secrets):
api_11_request(path="chapters",
data={'title': title, 'description': description},
secrets=secrets)
def setup_lecture(title, description, secrets):
api_11_request(path="lectures",
data={'title': title},
secrets=secrets)
# setup_lecture_description()
def setup_lecture_description(lecture_id, description, secrets):
api_11_request(path="lectures/%d" % lecture_id,
data={'description': description},
secrets=secrets)
def api_11_request(path, data, secrets, method='post'):
endpoint = "https://www.udemy.com/api-1.1/%s" % path
referer = "https://www.udemy.com/course-manage/edit-curriculum/?courseId=%d" % secrets['courseid']
headers = {
'referer': referer,
'X-Requested-With': 'XMLHttpRequest',
'X-Udemy-Bearer-Token': secrets['cookies']['access_token'],
'X-Udemy-Client-Id': secrets['cookies']['client_id'],
'X-Udemy-Snail-Case': 'true',
}
if method == 'post':
output_data = {
'csrfmiddlewaretoken': secrets['cookies']['csrfmiddlewaretoken'],
'disableMemcacheGets': 1,
'courseId': secrets['courseid'],
}
output_data.update(data)
r = requests.post(endpoint, data=output_data, headers=headers, cookies=secrets['cookies'])
else:
r = requests.get(endpoint, headers=headers, cookies=secrets['cookies'])
print r
r.raise_for_status()
return r.json()
def get_course_data(secrets):
return api_11_request(path="courses/%d?disableMemcacheGets=1" % secrets['courseid'],
data={}, method='get',
secrets=secrets)
def get_curriculum(secrets):
return api_11_request(path="courses/%d/curriculum/extended?disableMemcacheGets=1" % secrets['courseid'],
data={}, method='get',
secrets=secrets)
def setup_curriculum(secrets):
"""
"""
# input_data = load_yaml('curriculum.yml')
print get_course_data(secrets)
print get_curriculum(secrets)
# setup_chapter(title="Test chapter title", description="test chapter description", secrets=secrets)
if __name__ == '__main__':
args = parse_args()
if args.verbose:
setup_verbose_logging()
secrets = load_secrets()
if args.section == 'basics':
setup_basics(secrets)
if args.section == 'details':
setup_details(secrets)
if args.section == 'goals':
setup_goals(secrets)
if args.section == 'curriculum':
setup_curriculum(secrets)