-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathevaluate.py
342 lines (308 loc) · 12.3 KB
/
evaluate.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# @file evaluate.py
# @brief enroll & evaluate
# @author [email protected], [email protected]
# @version 0.2.01
# @date 2017-1-3
import requests
from configparser import RawConfigParser
from bs4 import BeautifulSoup
from CollegeCode import CollegeCode
import time
debug = False
class UCASEvaluate:
def __init__(self):
self.__readCoursesId()
cf = RawConfigParser()
cf.read('config')
self.username = cf.get('info', 'username')
self.password = cf.get('info', 'password')
self.enroll = cf.getboolean('action', 'enroll')
self.evaluate = cf.getboolean('action', 'evaluate')
self.idle = cf.get('idle', 'time')
print('Hi, username: ' + self.username)
print('Password: ' + '*' * len(self.password))
self.loginPage = 'http://sep.ucas.ac.cn'
self.loginUrl = self.loginPage + '/slogin'
self.courseSystem = self.loginPage + '/portal/site/226/821'
self.courseBase = 'http://jwxk.ucas.ac.cn'
self.courseIdentify = self.courseBase + '/login?Identity='
self.courseSelected = self.courseBase + '/courseManage/selectedCourse'
self.courseSelectionBase = self.courseBase + '/courseManage/main'
self.courseCategory = self.courseBase + '/courseManage/selectCourse?s='
self.courseSave = self.courseBase + '/courseManage/saveCourse?s='
self.studentCourseEvaluateUrl = 'http://jwjz.ucas.ac.cn/Student/DeskTopModules/'
self.selectCourseUrl = 'http://jwjz.ucas.ac.cn/Student/DesktopModules/Course/SelectCourse.aspx'
self.evaluateIndex = 'http://jwxk.ucas.ac.cn/evaluate/52574'
self.evaluateBase = 'http://jwxk.ucas.ac.cn/evaluate/evaluate/'
self.evaluateSave = 'http://jwxk.ucas.ac.cn/evaluate/save/{}?s={}'
self.merit = cf.get('comment', 'merit')
self.flaw = cf.get('comment', 'flaw')
self.suggest = cf.get('comment', 'suggest')
self.enrollCount = {}
self.evaluateCount = {}
self.headers = {
'Host': 'sep.ucas.ac.cn',
'Connection': 'keep-alive',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
}
self.s = requests.Session()
loginPage = self.s.get(self.loginPage, headers=self.headers)
self.cookies = loginPage.cookies
def networkUnstable(self, response):
if response.status_code != requests.codes.ok:
if response.status_code == requests.codes.moved_permanently:
self.login()
print('Relogin')
return True
return False
def login(self):
while True:
postdata = {
'userName': self.username,
'pwd': self.password,
'sb': 'sb'
}
status = self.s.post(self.loginUrl, data=postdata, headers=self.headers)
if status.status_code != requests.codes.ok:
continue
if 'sepuser' in self.s.cookies.get_dict():
return True
return False
def __readCoursesId(self):
coursesFile = open('./courseid', 'r')
self.coursesId = {}
for line in coursesFile.readlines():
line = line.strip().replace(' ', '').split(':')
courseId = line[0]
isDegree = False
if len(line) == 2 and line[1] == 'on':
isDegree = True
self.coursesId[courseId] = isDegree
def enrollCourses(self, idle=3):
try:
idle = abs(int(self.idle))
except:
print('Idle Error and use the default idle time %d secs.' % idle)
while True:
response = self.s.get(self.courseSelectionBase)
if self.networkUnstable(response):
continue
break
while True:
response = self.s.get(self.courseSystem, headers=self.headers)
if self.networkUnstable(response):
continue
print('Course System')
break
soup = BeautifulSoup(response.text, 'html.parser')
if debug:
print(response.text)
exit()
try:
identity = str(soup).split('Identity=')[1].split('"'[0])[0]
coursePage = self.courseIdentify + identity
while True:
response = self.s.get(coursePage)
if self.networkUnstable(response):
continue
response = self.s.get(self.courseSelected)
if self.networkUnstable(response):
continue
print('Course Selected')
break
if debug:
print(response.text)
exit()
while True:
for eachCourse in self.coursesId:
if eachCourse in response.text:
print("Course " + eachCourse + " has been selected.")
self.enrollCount[eachCourse] = 0
continue
if (eachCourse in self.enrollCount and
self.enrollCount[eachCourse] == 0):
continue
self.enrollCount[eachCourse] = 1
result = self.__enrollCourse(eachCourse,
self.coursesId[eachCourse])
if result:
self.enrollCount[eachCourse] = 0
time.sleep(idle)
for enroll in self.enrollCount:
if self.enrollCount[enroll] == 0:
self.coursesId.pop(enroll)
self.enrollCount.clear()
if not self.coursesId:
return
except KeyboardInterrupt:
print("Bye")
except Exception as exception:
print("System error")
print(exception)
exit()
def __enrollCourse(self, courseId, isDegree):
print('Enroll ' + courseId + ' Start')
while True:
response = self.s.get(self.courseSelectionBase)
if self.networkUnstable(response):
continue
break
if debug:
print(response.text)
exit()
soup = BeautifulSoup(response.text, 'html.parser')
labels = [(label.contents[0][:2], label['for'][3:])
for label in soup.find_all('label')[2:]]
categories = dict()
for label in labels:
if label[0] in categories:
categories[label[0]] += ',' + label[1]
else:
categories[label[0]] = label[1]
# categoryId = categories[courseId[:2]]
categoryId = categories[CollegeCode[courseId[:2]]]
identity = soup.form['action'].split('=')[1]
postdata = {
'deptIds': categoryId,
'sb': 0
}
categoryUrl = self.courseCategory + identity
while True:
response = self.s.post(categoryUrl, data=postdata)
if self.networkUnstable(response):
continue
break
if debug:
print(response.text)
exit()
soup = BeautifulSoup(response.text, 'html.parser')
courseTable = soup.body.form.table.find_all('tr')[1:]
courseDict = dict([(c.span.contents[0], c.span['id'].split('_')[1])
for c in courseTable])
if courseId in courseDict:
postdata = {
'deptIds': categoryId,
'sids': courseDict[courseId]
}
if isDegree:
postdata['did_' + courseDict[courseId]] = courseDict[courseId]
courseSaveUrl = self.courseSave + identity
while True:
response = self.s.post(courseSaveUrl, data=postdata)
if self.networkUnstable(response):
continue
break
if debug:
print(response.text)
exit()
tmp = BeautifulSoup(response.text, 'html.parser')
flag = tmp.find('div', attrs={'id': 'messageBoxError'})
if 'hide' in flag['class']:
print('[Success] ' + courseId)
return True
else:
print('[Fail] ' + courseId)
return False
else:
print("No such course")
return True
def evaluateCourses(self):
try:
idle = abs(int(self.idle))
except:
print('Idle Error and use the default idle time %d secs.' % idle)
while True:
response = self.s.get(self.courseSelectionBase)
if self.networkUnstable(response):
continue
break
soup = BeautifulSoup(response.text, 'html.parser')
links = soup.find_all('a')
for link in links:
if 'href' in link.attrs.keys() and 'evaluate' in link['href']:
self.evaluateIndex = link['href']
break
while True:
response = self.s.get(self.courseBase + self.evaluateIndex)
if self.networkUnstable(response):
continue
break
try:
soup = BeautifulSoup(response.text, 'html.parser')
links = soup.find_all('a', attrs={'class': 'btn'})
self.evaluateCourseIds = []
for link in links:
self.evaluateCourseIds.append(link['href'].split('/')[-1])
while True:
for courseId in self.evaluateCourseIds:
if (courseId in self.evaluateCount and
self.evaluateCount[courseId] == 0):
continue
self.evaluateCount[courseId] = 1
result = self.__evaluateCourse(courseId)
if result:
self.evaluateCount[courseId] = 0
time.sleep(idle)
for enroll in self.evaluateCount:
if self.evaluateCount[enroll] == 0:
self.evaluateCourseIds.remove(enroll)
self.evaluateCount.clear()
if not self.evaluateCourseIds:
return
except KeyboardInterrupt:
print("Bye")
except Exception as exception:
print("System error")
print(exception)
exit()
def __evaluateCourse(self, courseId):
while True:
response = self.s.get(self.evaluateBase + courseId)
if self.networkUnstable(response):
continue
break
s = response.text.split('?s=')[-1].split('"')[0]
soup = BeautifulSoup(response.text, 'html.parser')
radios = soup.find_all('input', attrs={'type': 'radio'})
value = radios[0]['value']
data = {}
for radio in radios:
data[radio['name']] = value
data['starFlag'] = 5
data['merit'] = self.merit
data['flaw'] = self.flaw
data['suggest'] = self.suggest
while True:
response = self.s.post(self.evaluateSave.format(courseId, s), data=data)
if self.networkUnstable(response):
continue
break
tmp = BeautifulSoup(response.text, 'html.parser')
flag = tmp.find('div', attrs={'id': 'messageBoxError'})
if 'hide' in flag['class']:
print('[Success] ' + courseId)
return True
else:
print('[Fail] ' + courseId)
return False
return True
if __name__ == "__main__":
ucasEvaluate = UCASEvaluate()
if not ucasEvaluate.login():
print('Login error. Please check your username and password.')
exit()
print('Login success')
if ucasEvaluate.enroll:
print('Enrolling start')
ucasEvaluate.enrollCourses()
print('Enrolling finish')
if ucasEvaluate.evaluate:
print('Evaluating start')
ucasEvaluate.evaluateCourses()
print('Evaluating finish')