-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.py
146 lines (127 loc) · 4.81 KB
/
main.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
#coding=UTF-8
import json
import re
import base64
import time
from cookie_getter import CookieGetter
from captcha import read_captcha
from email_sender import sendEmail
from utils import *
_, URL_DICT = ReadNetWorkJson()
class CourseSearcher(CookieGetter):
def __init__(self, profileId=2224, CAPTCHA_TYPE='slide'): # This param will change with the semester
super(CourseSearcher, self).__init__()
self.profileId = profileId
self.CAPTCHA_TYPE = CAPTCHA_TYPE
self.mainUrl = URL_DICT['MAIN_PAGE']
self.xkPageUrl = URL_DICT["XK_PAGE"]
self.baseUrl = URL_DICT['BASE_URL']
self.selCourseUrl = URL_DICT['SELECT_COURSE']
self.captchaUrl = URL_DICT["CAPTCHA"]
self.secondMajorUrl = URL_DICT["SECONDMAJOR_PAGE"]
self.baseUrl = self.addProfParam(self.baseUrl)
self.selCourseUrl = self.addProfParam(self.selCourseUrl)
self.form_data = PayloadGetter('formData')
self.main_page_data = PayloadGetter('mainPageData')
self.captcha_ret_data = PayloadGetter('captcha_ret')
self.main_page_data['electionProfile.id'] = profileId
self.courseIdList = ReadLessonJson()
self.cookies = self.getCookies()
def RunScript(self):
for lessonNo in self.courseIdList:
self.addCourse(lessonNo)
def addProfParam(self, url):
prof_param = {'profileId':self.profileId}
url = AddParam(url, prof_param)
return url
def addCourse(self, lessonNo):
res = self.searchCourse(lessonNo)
course_no, course_id, course_name = findClassList(res.text, lessonNo)
if isCourseAvailable(res.text, course_no):
info = "课程 [" + course_name + " " + course_id +"], 可选, 正在选课中"
print(info)
#sendEmail(info)
result = self.selCourse(course_no)
return result
else:
info = "课程 [" + course_name + " " + course_id +"], 目前不可选"
print(info)
return False
def searchCourse(self, lessonNo):
form = self.form_data
form['lessonNo'] = lessonNo
self.direct_to_selCoursePage() # 验证步骤,必须执行,不然会被服务器反制
res = self.Post(
url=self.baseUrl,
cookies=self.cookies,
data = form,
ErrMsg="Serch Course Error (getCourseNoAndId)"
)
return res
def direct_to_selCoursePage(self):
param = {'_':int(time.time())}
xkPageUrl_t = AddParam(self.xkPageUrl, param)
self.Get(
url=xkPageUrl_t,
cookies=self.cookies,
ErrMsg="Get Main Page Error (getCourseNoAndId) Get"
)
if self.secondMajor == 1:
self.Get(
url=self.secondMajorUrl,
cookies=self.cookies,
ErrMsg="Get Second Major Page Error (getCourseNoAndId) Get"
)
self.Post(
url=self.mainUrl,
cookies=self.cookies,
data = self.main_page_data,
ErrMsg="Into Xk Page Error (getCourseNoAndId) Post"
)
def handleCAPTCHA(self):
if self.CAPTCHA_TYPE == 'slide':
moveEnd_X, wbili = self.getCaptcha()
self.captcha_ret_data['moveEnd_X'] = moveEnd_X
self.captcha_ret_data['wbili'] = wbili
response = self.Post(
url=self.captchaUrl,
cookies=self.cookies,
data=self.captcha_ret_data,
ErrMsg="CAPTCHA Error (captchaUrl)"
)
captcha_response = 'captcha_response'
elif self.CAPTCHA_TYPE == 'img':
result = self.getCaptcha()
captcha_response = result
else:
raise NotImplementedError
return captcha_response
# 抢课(捡漏)
def selCourse(self, course_no):
captcha_response = self.handleCAPTCHA()
form_data = {
'optype': 'true',
'operator0': course_no + ':true:0',
'captcha_response': captcha_response
}
response = self.Post(
url=self.selCourseUrl,
cookies=self.cookies,
data=form_data,
ErrMsg="selCourse Error (selCourse)"
)
#print(response.content.decode(encoding='utf-8'))
print("选课成功")
def getCaptcha(self):
param = {'_':int(time.time())}
captchaUrl = AddParam(self.captchaUrl, param)
response = self.Get(
url=captchaUrl,
cookies=self.cookies,
ErrMsg="Get Captcha Error (getCaptcha)"
)
captcha = read_captcha(response.content)
return captcha
if __name__ == "__main__":
launcher = CourseSearcher()
launcher.RunScript()