-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherya.py
187 lines (165 loc) · 6.29 KB
/
erya.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
180
181
182
183
184
185
186
187
import requests
import time
from utils import get_log_enc, HEADERS
from utils.parser import *
ERYA_V = '20160407-1'
class NotLoggedIn(Exception):
pass
class EryaSession:
def __init__(self, cookies: dict):
self.session = requests.session()
self.session.cookies.update(cookies)
self.session.headers.update(HEADERS)
@property
def cookies(self):
return self.session.cookies.get_dict()
def _request(self, method, url, **kwargs):
"""
used for normal requests after logging in
:param method:
:param url:
:param params:
:param data:
:param kwargs:
:return:
"""
response = self.session.request(method, url, **kwargs)
if 'passport2.chaoxing.com/login' in response.url:
raise NotLoggedIn
return response
def get_course_chapter_list(self, course_id, class_id, enc):
"""
You need a valid combination of course_id, class_id, enc to get chapter list
:param course_id:
:param class_id:
:param enc:
:return:list[course_id, class_id, chapter_id, chapter_name]
"""
data = {
'courseId': course_id,
'clazzid': class_id,
'enc': enc
}
response = self._request('GET', 'https://mooc1-1.chaoxing.com/mycourse/studentcourse', params=data)
chapter_list = parse_chapter_list(response.text)
return chapter_list
def get_chapter_tabs(self, course_id, class_id, chapter_id):
data = {
'courseId': course_id,
'clazzid': class_id,
'chapterId': chapter_id
}
response = self._request('POST', 'https://mooc1-1.chaoxing.com/mycourse/studentstudyAjax', data=data)
return parse_chapter_tabs(response.text)
def get_card_detail(self, class_id, course_id, chapter_id, num=0, v=ERYA_V):
url = 'https://mooc1-1.chaoxing.com/knowledge/cards'
params = {
'clazzid': class_id,
'courseid': course_id,
'knowledgeid': chapter_id,
'num': num,
'v': v # currently is '20160407-1'
}
response = self._request('GET', url, params=params)
return parse_chapter_detail(response.text)
def get_ananas_data(self, object_id, school_id):
url = 'https://mooc1-1.chaoxing.com/ananas/status/{}'.format(object_id)
params = {
'k': school_id,
'_dc': int(time.time() * 1000)
}
return self._request('GET', url, params=params, headers=HEADERS).json()
def get_checkpoint_data(self, mid):
url = 'https://mooc1-1.chaoxing.com/richvideo/initdatawithviewer?&start=undefined&mid={mid}'.format(
mid=mid)
response = self._request('GET', url, headers=HEADERS)
return parse_checkpoint_data(response.json())
def get_utenc(self, chapter_id, course_id, class_id, enc):
params = {
'chapterId': chapter_id,
'courseId': course_id,
'clazzid': class_id,
'enc': enc
}
url = 'https://mooc1-1.chaoxing.com/mycourse/studentstudy'
response = self._request('GET', url, params=params)
return parse_utenc(response.text)
def get_quiz_data(self, work_id, job_id, chapter_id, class_id, enc, utenc, course_id):
params = {
'api': 1,
'workId': work_id,
'jobid': job_id,
'needRedirect': True,
'knowledgeid': chapter_id,
'ut': 's',
'clazzId': class_id,
'type': '',
'enc': enc,
'utenc': utenc,
'courseid': course_id,
}
url = 'https://mooc1-1.chaoxing.com/api/work'
response = self._request('GET', url, params=params)
if 'doHomeWorkNew' in response.url:
hw_passed = False
elif 'selectWorkQuestionYiPiYue' in response.url:
hw_passed = True
else:
raise ValueError('Unrecognized URL {}'.format(response.url.split('?')))
return parse_quiz_data(response.text, hw_passed)
def request_log(self, dtoken, duration, user_id, job_id, object_id, class_id, playing_time, chapter_id):
duration, user_id, job_id, class_id, playing_time, chapter_id = \
list(map(int, [duration, user_id, job_id, class_id, playing_time, chapter_id]))
params = {
'dtype': 'Video',
'duration': duration,
'userid': user_id,
'rt': '0.9',
'jobid': job_id,
'objectId': object_id,
'clipTime': '0_%d' % duration,
'otherInfo': 'nodeId_{}'.format(chapter_id),
'clazzId': class_id,
'view': 'pc',
'playingTime': playing_time,
'isdrag': '3',
'enc': get_log_enc(class_id, user_id, job_id, object_id, playing_time, duration)
}
url = 'https://mooc1-1.chaoxing.com/multimedia/log/{}'.format(dtoken)
response = self._request('GET', url, params=params)
return response.json()
def answer_checkpoint(self, resource_id, answers: list):
resource_id = int(resource_id)
url = 'https://mooc1-1.chaoxing.com/richvideo/qv'
params = {
'resourceid': resource_id,
'answer': "'" + ''.join(answers) + "'"
}
return self._request('GET', url, params=params).json()
def request_monitor(self, version: str, jsoncallback: str, referer='https://i.mooc.chaoxing.com',
t: int = int(time.time())):
"""
the periodic request to detect multi client login
:param version:
:param jsoncallback:
:param referer:
:param t:
:return:
"""
data = {
'version': version,
'jsoncallback': jsoncallback,
'referer': referer,
't': t
}
self._request('GET', 'https://passport2.chaoxing.com/api/monitor', data=data)
def request_reading_log(self, _from, course_id, chapter_id, height, h):
url = 'https://mooc1-1.chaoxing.com/multimedia/readlog'
params = {
'_from_': _from,
'courseid': course_id,
'chapterid': chapter_id,
'height': height,
'h': h
}
self._request('GET', url, params=params)