-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathWeiBanAPI.py
More file actions
267 lines (237 loc) · 7.89 KB
/
WeiBanAPI.py
File metadata and controls
267 lines (237 loc) · 7.89 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# -*- coding: utf-8 -*-
'''
微伴自动做题模块
'''
import requests
import json
import time
import os
class RE(Exception):
'''
WeiBan 类自定义运行时错误
'''
def __init__(self, reason):
self.reason = reason
class WeiBan:
'''
微伴做题主模块
'''
# API 列表
# 获取验证码以及验证码 ID
__genQRCodeURL = 'https://weiban.mycourse.cn/pharos/login/genBarCodeImageAndCacheUuid.do'
# 用于二维码登录刷新登录状态
__loginStatusURL = 'https://weiban.mycourse.cn/pharos/login/barCodeWebAutoLogin.do'
# 请求姓名
__getNameURL = 'https://weiban.mycourse.cn/pharos/my/getInfo.do'
# 请求当前任务
__getTaskURL = 'https://weiban.mycourse.cn/pharos/index/getStudyTask.do'
# 请求进度
__getProgressURL = 'https://weiban.mycourse.cn/pharos/project/showProgress.do'
# 请求课程种类
__getListCourseURL = 'https://weiban.mycourse.cn/pharos/usercourse/listCategory.do'
# 请求课程列表
__getListURL = 'https://weiban.mycourse.cn/pharos/usercourse/listCourse.do'
# 请求完成课程
__finishCourseURL = 'https://weiban.mycourse.cn/pharos/usercourse/finish.do'
# 学习课程
__doStudyURL = 'https://weiban.mycourse.cn/pharos/usercourse/study.do'
def __init__(self, schoolID):
self.tenantCode = schoolID
return
def qrLogin(self):
'''
TODO
'''
try:
self.qrCodeID = self.__getQRCode()
time.sleep(5)
while True:
responseText = self.__getLoginStatus()
responseJSON = json.loads(responseText)
if responseJSON['code'] == '0':
self.username = responseJSON['data']['userName']
self.userID = responseJSON['data']['userId']
self.__getTask()
# self.projectID = responseJSON['data']['preUserProjectId']
# self.projectID = '95bbd1bc-6361-4352-9db5-a5ddf4d347b7'
break
else:
print('未登录,等待后5s刷新')
time.sleep(5)
except KeyboardInterrupt:
raise RE('用户中止程序运行')
except requests.exceptions.ConnectionError:
raise RE('无法连接至服务器:连接故障')
except requests.exceptions.ConnectTimeout:
raise RE('无法连接至服务器:请求超时')
return
# 获取并返回 QRCode 链接以及 QRCode ID
def __getQRCode(self):
try:
r = requests.post(self.__genQRCodeURL)
responseJSON = json.loads(r.text)
if os.name == 'nt':
# Windows 系统直接用 explorer.exe 打开浏览器
print('请扫描二维码登录')
os.system('explorer.exe {}'.format(responseJSON['data']['imagePath']))
print('如浏览器未自动打开,请打开下面的URL并扫描二维码登录')
else:
# TODO: Print the QR code in the terminal
print('请打开下面的URL并扫描二维码登录')
print(responseJSON['data']['imagePath'])
return responseJSON['data']['barCodeCacheUserId']
except KeyboardInterrupt:
raise RE('用户中止程序运行')
except requests.exceptions.ConnectionError:
raise RE('无法连接至服务器:连接故障')
except requests.exceptions.ConnectTimeout:
raise RE('无法连接至服务器:请求超时')
# 用于二维码登录,刷新是否已经成功登录
def __getLoginStatus(self):
try:
param = {
'barCodeCacheUserId': self.qrCodeID
}
r = requests.post(self.__loginStatusURL, data=param)
return r.text
except KeyboardInterrupt:
raise RE('用户中止程序运行')
except requests.exceptions.ConnectionError:
raise RE('无法连接至服务器:连接故障')
except requests.exceptions.ConnectTimeout:
raise RE('无法连接至服务器:请求超时')
def printUserInfo(self):
'''
打印学生信息
'''
param = {
'userId': self.userID,
'tenantCode': self.tenantCode
}
r = requests.post(self.__getNameURL, data=param)
info = json.loads(r.text)['data']
print('用户信息:')
print(info['realName'], info['orgName'], info['specialtyName'])
return
def printProgress(self):
'''
打印课程进度
'''
try:
param = {
'userProjectId': self.projectID,
'tenantCode': self.tenantCode
}
r = requests.post(self.__getProgressURL, data=param)
progress = json.loads(r.text)['data']
print('课程总数:' + str(progress['requiredNum']))
print('完成课程:' + str(progress['requiredFinishedNum']))
print('结束时间:' + str(progress['endTime']))
print('剩余天数:' + str(progress['lastDays']))
except KeyboardInterrupt:
raise RE('用户中止程序运行')
except requests.exceptions.ConnectionError:
raise RE('无法连接至服务器:连接故障')
except requests.exceptions.ConnectTimeout:
raise RE('无法连接至服务器:请求超时')
return
# 获取课程列表
def __getCourseList(self, chooseType):
param = {
'userProjectId': self.projectID,
'chooseType': chooseType,
'tenantCode': self.tenantCode,
}
r = requests.post(self.__getListCourseURL, data=param)
return json.loads(r.text)['data']
def __getTask(self):
try:
param = {
'userId': self.userID,
'tenantCode': self.tenantCode
}
r = requests.post(self.__getTaskURL, data=param)
response = json.loads(r.text)
if response['code'] == '0':
self.projectID = response['data']['userProjectId']
else:
raise RE('请求失败\n' + r.text)
except KeyboardInterrupt:
raise RE('用户中止程序运行')
except requests.exceptions.ConnectionError:
raise RE('无法连接至服务器:连接故障')
except requests.exceptions.ConnectTimeout:
raise RE('无法连接至服务器:请求超时')
return
def __getList(self, categoryCode, chooseType, name):
try:
param = {
'userProjectId': self.projectID,
'categoryCode': categoryCode,
'chooseType': chooseType,
'tenantCode': self.tenantCode,
'name': name
}
r = requests.post(self.__getListURL, data=param)
return json.loads(r.text)['data']
except KeyboardInterrupt:
raise RE('用户中止程序运行')
except requests.exceptions.ConnectionError:
raise RE('无法连接至服务器:连接故障')
except requests.exceptions.ConnectTimeout:
raise RE('无法连接至服务器:请求超时')
# 完成课程请求
def __finishCourse(self, userCourseID):
try:
r = requests.get('{}?userCourseId={}&tenantCode={}'.format(self.__finishCourseURL, userCourseID, self.tenantCode))
print(r.text)
except KeyboardInterrupt:
raise RE('用户中止程序运行')
except requests.exceptions.ConnectionError:
raise RE('无法连接至服务器:连接故障')
except requests.exceptions.ConnectTimeout:
raise RE('无法连接至服务器:请求超时')
# TODO: Modify so we know if it succeed or not
return
def __doStudy(self, userCourseId):
try:
param = {
'userProjectId': self.projectID,
'courseId': userCourseId,
'tenantCode': self.tenantCode
}
r = requests.post(self.__doStudyURL, data=param)
print(r.text)
except KeyboardInterrupt:
raise RE('用户中止程序运行')
except requests.exceptions.ConnectionError:
raise RE('无法连接至服务器:连接故障')
except requests.exceptions.ConnectTimeout:
raise RE('无法连接至服务器:请求超时')
return
def finishAll(self):
'''
尝试完成所有课程
'''
try:
courses = self.__getCourseList('3')
for course in courses:
print('章节码:' + course['categoryCode'])
print('章节内容:' + course['categoryName'])
classes = self.__getList(course['categoryCode'], '3', '')
for item in classes:
print('课程内容:' + item['resourceName'])
if (item['finished'] == 1):
print('已完成')
else:
print('发送完成请求')
self.__doStudy(item['resourceId'])
self.__finishCourse(item['userCourseId'])
print('')
except KeyboardInterrupt:
raise RE('用户中止程序运行')
except requests.exceptions.ConnectionError:
raise RE('无法连接至服务器:连接故障')
except requests.exceptions.ConnectTimeout:
raise RE('无法连接至服务器:请求超时')
return