Skip to content

Instantly share code, notes, and snippets.

@PhotonQuantum
Last active July 15, 2020 17:21
Show Gist options
  • Save PhotonQuantum/1193e39e4d36c05376a94d34f65442fb to your computer and use it in GitHub Desktop.
Save PhotonQuantum/1193e39e4d36c05376a94d34f65442fb to your computer and use it in GitHub Desktop.
from pysjtu import Session
from time import sleep
import re
from loguru import logger
course_map: dict
expect: str = "magicstring"
req_list: dict = {}
interval: float = 0.5
match_url = re.compile("curl '.*?'")
match_data = re.compile("--data-raw '.*?'")
HEADER = {"Host": "i.sjtu.edu.cn", "Origin": "https://i.sjtu.edu.cn", "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"}
def curl_to_req(curl: str):
url = re.findall(match_url, curl)[0][6:-1]
data = re.findall(match_data, curl)[0][12:-1]
return url, data
with open("course") as f:
for line in f:
name, url = line.split("<>")
if name == "expect":
expect = url.strip()
elif name == "interval":
interval = float(url)
else:
req_list[name] = curl_to_req(url)
logger.debug(f"Request List: {req_list}")
logger.debug(f"Expect: {expect}")
logger.debug(f"Interval: {interval}")
sess_file = open("session", mode="r+b")
with Session(session_file=sess_file) as sess:
remove_list: list = []
while True:
remove_list.clear()
for name, req in req_list.items():
try:
rsp = (sess.post(req[0], data=req[1], headers=HEADER)).text
except:
logger.exception(f"Error when electing {name}")
continue
if expect in rsp:
logger.success(f"{name} - elected")
remove_list.append(name)
else:
logger.info(f"{name} - not elected - {rsp[:20]}")
for item in remove_list:
req_list.pop(item)
if not req_list:
logger.success("Finished")
break
sleep(interval)
@PhotonQuantum
Copy link
Author

PhotonQuantum commented Jul 15, 2020

Min Python Version: 3.7
Requirements: pysjtu==0.2.0  loguru

首先需要登录创建 session 文件

>>> from pysjtu import Session
>>> sess = Session(username="blabla", password="blabla")
>>> sess.dump("session")

然后创建 courses 文件配置选课

expect<>ok(请求成功匹配的字符串)
interval<>0.5(每轮循环之间的间隔秒数)
(课程显示名 提示作用 可以乱写)<>(从浏览器F12里找到选课的post请求,然后右键复制>复制为cURL请求,直接贴)
基电<>curl 'https://i.sjtu.edu.cn/......' -H blablabla ...

然后直接跑 elect_course.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment