Last active
July 15, 2020 17:21
-
-
Save PhotonQuantum/1193e39e4d36c05376a94d34f65442fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
首先需要登录创建 session 文件
然后创建 courses 文件配置选课
然后直接跑 elect_course.py