From 5bed29566c2009b6246047bc30fce404f3fb51a8 Mon Sep 17 00:00:00 2001 From: gene-2012 Date: Thu, 1 May 2025 14:33:06 +0800 Subject: [PATCH] first edition --- .env | 4 ++ .gitignore | 4 ++ contests.py | 42 +++++++++++++ luogu.ico | Bin 0 -> 4286 bytes main.py | 114 +++++++++++++++++++++++++++++++++++ main.spec | 39 ++++++++++++ models.py | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 371 insertions(+) create mode 100644 .env create mode 100644 .gitignore create mode 100644 contests.py create mode 100644 luogu.ico create mode 100644 main.py create mode 100644 main.spec create mode 100644 models.py diff --git a/.env b/.env new file mode 100644 index 0000000..d59f8eb --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +CLIENT_ID= +UID= +CF_CLEARANCE= +C3VK= \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81bb05f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__/ +build/ +dist/ +env/ \ No newline at end of file diff --git a/contests.py b/contests.py new file mode 100644 index 0000000..83cf44b --- /dev/null +++ b/contests.py @@ -0,0 +1,42 @@ +class Contest: + def __init__(self, contest_id, unrated=False): + self.contest_id = contest_id + self.unrated = unrated + self.contest_url = f"https://luogu.com.cn/contest/{contest_id}" + self.join_url = f"https://www.luogu.com.cn/fe/api/contest/join/{contest_id}" + + # 动态设置 Referer + self.headers = BASE_HEADERS.copy() + self.headers['Referer'] = self.contest_url + + self.csrf_token = self._get_csrf_token() + self._join_contest() + + def _get_csrf_token(self): + resp = requests.get( + self.contest_url, + headers=self.headers, + cookies=cookies + ) + soup = BeautifulSoup(resp.text, 'html.parser') + meta_tag = soup.find('meta', {'name': 'csrf-token'}) + if meta_tag: + return meta_tag.get('content') + else: + raise ValueError("无法找到 csrf-token") + + def _join_contest(self): + headers = self.headers.copy() + headers['X-CSRF-Token'] = self.csrf_token + + data = {"unrated": str(self.unrated).lower()} + + resp = requests.post( + self.join_url, + headers=headers, + cookies=cookies, + json=data + ) + + print(f"Status Code: {resp.status_code}") + print(f"Response Text: {resp.text}") \ No newline at end of file diff --git a/luogu.ico b/luogu.ico new file mode 100644 index 0000000000000000000000000000000000000000..80c5505a63d4f19d2dd54bdd620926fb086d4e7b GIT binary patch literal 4286 zcmc&&Sx-|@5WYr#fWGJ}kNynbjQFk~Mq|V^`eb5!&`8v{8)M>vC`8tP(EzfDfGklF z&_L9-wzjmTR%lsUXo0TdeCPDuVxXZ5#UVq>x#!F`XJ*bf=b|W^_$Mbv!EdE@tDm=(&39yc_ z0vr2wvu?CZFd*XaCJ}IOev)QQy)a9qfwe1Zj}3Be8j<6 zd6K-={uOJNrX6H?bza0toi{%^v|?VW1L1(5{`3@UF+d)E)tn{7MwQ^)1lgLdNNo7Y z@t^#Lqnt3b>e8e>gT869x88``XCKTjIks*yg_q{Z(Q#jUhI+9zU!(BiTvE1^ofPo8 z$lh{Yivw^*>MzpR$1={hT5ow|sya=+Ke}YirZf-d?|0_|Szm}V>Wr}srH8mqkCV0G z63yAaa@f}=?yET$TJVsw`!VAJU)H3l`V4t2p9r zeixkRvGkGS-QBnkTo0pfZzaW%>BIbyB>1?^QqIa`l;Qm zRIZ8T8-8W8-z?u0c*ypq=%0EtRh{BHmSy?0tbRvtH&&j~I03ak-dB+cW{S-^DtKS3 zN5;3jzCfWxZ@NCDOw_0aSAUJ@ML2)ph&1aEjjAyn$H76kbD;kG By ![{author.get('name', '')}]({author.get('avatar', '')}?" + "x-oss-process=image/resize,m_fixed,h_30,w_30,image/circle,r_100/format,png)" + f"[{author.get('name', '')}](https://luogu.com.cn/user/{author.get('uid', '')})" + "\n\n" + f"{content}" + ) + + filename = f"solutions/{title}_{lid}.md" + with open(filename, "w", encoding="utf8") as f: + f.write(markdown_content) + except IOError as e: + raise RuntimeError(f"写入题解文件失败:{e}") + + def gen_all(self): + try: + os.makedirs(self.problem_id, exist_ok=True) + os.chdir(self.problem_id) + self.gen_problem_md() + self.gen_samples() + self.gen_solutions() + except Exception as e: + raise RuntimeError(f"生成全部内容失败:{e}") + + +if __name__ == "__main__": + try: + p = Problem("P1145") + p.gen_all() + except Exception as e: + print(f"[ERROR] 发生错误:{e}") \ No newline at end of file