Skip to content

Commit

Permalink
feat: 根据课程名抢课
Browse files Browse the repository at this point in the history
  • Loading branch information
msojocs committed May 30, 2022
1 parent 015b3c7 commit ea6bc23
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 136 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
old
pic
config.json
config.json
__pycache__
2 changes: 1 addition & 1 deletion config.example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ocr_server": "http://127.0.0.1:4006/vercode",
"profiled_id": 4130,
"lesson_id": 123,
"course_name": "数字孪生技术",
"cookie": ""
}
154 changes: 154 additions & 0 deletions cuit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import requests
import re
import json
import execjs

class FC(object):
cookie = ""
ocrServer = ""
def __init__(self, cookie, ocrServer):
self.cookie = cookie
self.ocrServer = ocrServer
pass

def fuckCourse(self, profiledId, lessonId):
try:
body = {
"optype": "true",
"operator0": lessonId + ":true:0",
"lesson0": lessonId,
"schLessonGroup_" + lessonId:"undefined"
}
req = requests.post("http://jwgl.cuit.edu.cn/eams/stdElectCourse!batchOperator.action?profileId=" + profiledId,
headers={
"cookie": self.cookie,
"X-Requested-With": "XMLHttpRequest",
"Referer" : "http://jwgl.cuit.edu.cn/eams/stdElectCourse!batchOperator.action?profileId=" + profiledId,
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.0 Safari/537.36 Edg/84.0.521.0"
}, data=body, timeout=5, allow_redirects=False)
req.encoding = 'utf-8'

html = req.text
ret = re.search(r"margin:auto;\">\n\t\t\t\t(.*)<\/br>", html)
if ret == None:
print("cookie过期")
exit(-1)
pass
print(ret.group(1))
req.close()
if '成功' in ret.group(1):
print('get')
return True
except Exception as err:
print("出错")
print(err)
return False
pass
pass

def getPic(self):
url="http://jwgl.cuit.edu.cn/eams/captcha/image.action"
headers = {
'cookie': self.cookie
}
while True:
try:
picReq=requests.get(url=url, headers=headers, timeout=5)
break
except Exception as err:
continue
# print(picReq.content)
return picReq.content
pass

def postOCRPic(self, pic):
url = self.ocrServer
files = {'captcha': pic}
data = {
'enctype':'multipart/form-data',
'name':'captcha'
}
ocrReq=requests.post(url=url, data=data, files=files)
# print(ocrReq.content)
return json.loads(ocrReq.content)
pass

'''
'''
def checkCaptcha(self, captcha, profiledId):
url="http://jwgl.cuit.edu.cn/eams/stdElectCourse!defaultPage.action"
headers = {
'cookie': self.cookie
}
data = {
'captcha_response': captcha,
'electionProfile.id': profiledId
}
while True:
try:
checkReq = requests.post(url=url, headers=headers, data=data, allow_redirects=False, timeout=5)
break
except Exception as err:
continue

# print(checkReq.text)
# 未登录与验证码错误都是302,但Location去向不同
if checkReq.status_code == 200 :
return True
elif 'sso' in checkReq.headers['Location']:
# 转到统一登录中心
print('cookie失效!!!')
exit(1)
return False
pass

def isAvailable(self, captcha, profiledId):
url="http://jwgl.cuit.edu.cn/eams/stdElectCourse!defaultPage.action"
headers = {
'cookie': self.cookie
}
data = {
'captcha_response': captcha,
'electionProfile.id': profiledId
}
while True:
try:
checkReq = requests.post(url=url, headers=headers, data=data, allow_redirects=False, timeout=5)
break
except Exception as err:
continue
# print(checkReq.text)
# 未登录与验证码错误都是302,但Location去向不同
if checkReq.status_code == 200 :
return '不在选课时间内' not in checkReq.text
elif 'sso' in checkReq.headers['Location']:
# 转到统一登录中心
print('cookie失效!!!')
self.cookie = input("请输入新的cookie:")
False
return False
pass

def courseName2Id(self, profileId, courseName):
url="http://jwgl.cuit.edu.cn/eams/stdElectCourse!data.action?profileId=" + str(profileId)
headers = {
'cookie': self.cookie
}
while True:
try:
courseListReq = requests.get(url=url, headers=headers, allow_redirects=False, timeout=5)
break
except Exception as err:
continue
courseList = courseListReq.text
# with open('test/html/courseList.html',encoding='utf-8') as f:
# courseList = f.read()
# print(courseList)
jsData = execjs.compile(courseList)
lessonJSONs = jsData.eval('lessonJSONs')
# print(lessonJSONs)
for lesson in lessonJSONs:
if courseName in lesson['name']:
return lesson['id']
return None
pass
138 changes: 9 additions & 129 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,140 +1,14 @@
import json
import platform
import requests
import re
import time
import os
from cuit import FC


'''
使用方法:https://www.jysafe.cn/4498.air
author: msojocs
'''
class FC(object):
cookie = ""
ocrServer = ""
def __init__(self, cookie, ocrServer):
self.cookie = cookie
self.ocrServer = ocrServer
pass

def fuckCourse(self, profiledId, lessonId):
try:
body = {
"optype": "true",
"operator0": lessonId + ":true:0",
"lesson0": lessonId,
"schLessonGroup_" + lessonId:"undefined"
}
req = requests.post("http://jwgl.cuit.edu.cn/eams/stdElectCourse!batchOperator.action?profileId=" + profiledId,
headers={
"cookie": self.cookie,
"X-Requested-With": "XMLHttpRequest",
"Referer" : "http://jwgl.cuit.edu.cn/eams/stdElectCourse!batchOperator.action?profileId=" + profiledId,
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.0 Safari/537.36 Edg/84.0.521.0"
}, data=body, timeout=5, allow_redirects=False)
req.encoding = 'utf-8'

html = req.text
ret = re.search(r"margin:auto;\">\n\t\t\t\t(.*)<\/br>", html)
if ret == None:
print("cookie过期")
exit(-1)
pass
print(ret.group(1))
req.close()
if '成功' in ret.group(1):
print('get')
return True
except Exception as err:
print("出错")
print(err)
return False
pass
pass

def getPic(self):
url="http://jwgl.cuit.edu.cn/eams/captcha/image.action"
headers = {
'cookie': self.cookie
}
while True:
try:
picReq=requests.get(url=url, headers=headers, timeout=5)
break
except Exception as err:
continue
# print(picReq.content)
return picReq.content
pass

def postOCRPic(self, pic):
url = self.ocrServer
files = {'captcha': pic}
data = {
'enctype':'multipart/form-data',
'name':'captcha'
}
ocrReq=requests.post(url=url, data=data, files=files)
# print(ocrReq.content)
return json.loads(ocrReq.content)
pass

'''
'''
def checkCaptcha(self, captcha, profiledId):
url="http://jwgl.cuit.edu.cn/eams/stdElectCourse!defaultPage.action"
headers = {
'cookie': self.cookie
}
data = {
'captcha_response': captcha,
'electionProfile.id': profiledId
}
while True:
try:
checkReq = requests.post(url=url, headers=headers, data=data, allow_redirects=False, timeout=5)
break
except Exception as err:
continue

# print(checkReq.text)
# 未登录与验证码错误都是302,但Location去向不同
if checkReq.status_code == 200 :
return True
elif 'sso' in checkReq.headers['Location']:
# 转到统一登录中心
print('cookie失效!!!')
exit(1)
return False
pass

def isAvailable(self, captcha, profiledId):
url="http://jwgl.cuit.edu.cn/eams/stdElectCourse!defaultPage.action"
headers = {
'cookie': self.cookie
}
data = {
'captcha_response': captcha,
'electionProfile.id': profiledId
}
while True:
try:
checkReq = requests.post(url=url, headers=headers, data=data, allow_redirects=False, timeout=5)
break
except Exception as err:
continue
# print(checkReq.text)
# 未登录与验证码错误都是302,但Location去向不同
if checkReq.status_code == 200 :
return '不在选课时间内' not in checkReq.text
elif 'sso' in checkReq.headers['Location']:
# 转到统一登录中心
print('cookie失效!!!')
self.cookie = input("请输入新的cookie:")
False
return False
pass


################### 入口 ###########################
if __name__ == "__main__":
Expand All @@ -143,7 +17,7 @@ def isAvailable(self, captcha, profiledId):
config = json.load(f)
cookie = config['cookie']
profiledId = config['profiled_id']
lessonId = config['lesson_id']
courseName = config['course_name']

print("开始了呀")
cuit = FC(cookie, config['ocr_server'])
Expand Down Expand Up @@ -171,6 +45,12 @@ def isAvailable(self, captcha, profiledId):
print('验证码检测通过,等待1秒')
time.sleep(1)
# 检测开放状态
print('获取lessonId')
lessonId = cuit.courseName2Id(profiledId, courseName)
if lessonId == None:
print('没有找到相关课程:' + courseName)
exit(1)

print('检测选课开放状态')
cnt = 0
while True:
Expand Down
1 change: 1 addition & 0 deletions test/cuit.py
1 change: 1 addition & 0 deletions test/html/courseList.html

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions test/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# import chardet
# s = "实验室安全与防护[OT082A.202001]选课 成功"
# # print("成功".encode('gbk') in s)
# print(chardet.detect(s))
print('wwww' + str(3))
import sys
sys.path.append("..")
from cuit import FC


if __name__ == "__main__":
print("test")
cuit = FC("1234", "2342")
courseList = cuit.courseName2Id("数字孪生技术")
print(courseList)
pass

0 comments on commit ea6bc23

Please sign in to comment.