Skip to content

Commit

Permalink
调整代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Sving1024 committed Dec 1, 2024
1 parent c4bfca4 commit 6b688f1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
16 changes: 14 additions & 2 deletions src/lgsv/luogu.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ async def fetch_csrf_token():
"""


class HttpError(Exception):
"""
HTTP 异常。
由于处理各类 HTTP 错误。
"""

def __init__(self, msg):
self.message = msg


class Problem:
"""洛谷题目类"""

Expand Down Expand Up @@ -116,13 +126,13 @@ async def fetch_resources(self):
print("解析题目" + self.problem_id)
# 解析请求到的 json
rescoures = json.loads(raw_resources.text)
if rescoures["code"] != 200:
raise HttpError(f"访问{self.__BASE_URL}{self.problem_id}失败:HTTP ERROR {rescoures["code"]}")
self.data = rescoures["currentData"]["problem"]
return self.data

def get_markdown(self, order=None):
"""以 order 的顺序获取题目的markdown"""
if order is None:
order = ["b", "d", "if", "of", "s", "h", "tr"]
self.markdown = self.part_markdown("title")
for c in order:
self.markdown += self.part_markdown(c)
Expand Down Expand Up @@ -184,6 +194,8 @@ async def fetch_resources(self):
)
print("解析题单" + self.training_id)
rescoures = json.loads(raw_resources.text)
if rescoures["code"] != 200:
raise HttpError(f"访问{self.__BASE_URL}{self.training_id}失败:HTTP ERROR {rescoures["code"]}")
self.data = rescoures["currentData"]["training"]
for p in self.data["problems"]:
self.problemList.append(Problem(problem_id=p["problem"]["pid"]))
Expand Down
41 changes: 15 additions & 26 deletions src/lgsv/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
# modPath = Path(__file__).parent.parent

target = {}
global_config = {"order": None}

cookies = {
"__client_id": "",
"_uid": "",
}

"""
if not Path('config.toml').exists:
Expand All @@ -22,14 +16,19 @@
#配置文件,先咕一会
"""

global_config["pandocArgs"] = [
"-f",
"markdown-blank_before_header+lists_without_preceding_blankline",
"--katex",
"--pdf-engine=xelatex",
"-V mainfont='等线'",
# '--include-in-header=head.tex',
]
global_config = {
"pandocArgs": [
"-f",
"markdown-blank_before_header+lists_without_preceding_blankline",
"--katex",
"--pdf-engine=xelatex",
"-V mainfont='等线'",
# '--include-in-header=head.tex',
],
"cookie": "",
"order": ["b", "d", "if", "of", "s", "h", "tr"],
"output": "out.md",
}

arg_parser = argparse.ArgumentParser(description="爬取洛谷题目并且进行格式转化")
arg_parser.add_argument("-p", "--problem", action="append", help="题目列表")
Expand All @@ -39,7 +38,7 @@
arg_parser.add_argument("--order", type=str, help="指定题目部分的顺序")
# arg_parser.add_argument("-u","--uid",type=int,help="洛谷uid")
arg_parser.add_argument("-c", "--cookie", type=str, help="洛谷cookie")
arg_parser.add_argument("-o","--output",type=str,help="输出 markdown 的位置")
arg_parser.add_argument("-o", "--output", type=str, help="输出 markdown 的位置")


def parse_args():
Expand All @@ -52,17 +51,7 @@ def parse_args():
global_config["pandocArgs"] = args["pandoc_args"]
if "cookie" in args:
global_config["cookie"] = args["cookie"]

# if "uid" in args:
# global_config["uid"] = args["uid"]
# cookies["_uid"]=args["uid"]
if args["order"] is not None:
global_config["order"] = args["order"].split(",")
if args["output"] is not None:
global_config["output"]=args["output"]
else:
global_config["output"]="out.md"

# if "client_id" in args:
# global_config["client_id"]=args["client_id"]
# cookies["__client_id"]=args["client_id"]
global_config["output"] = args["output"]
4 changes: 0 additions & 4 deletions tests/test_luogu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,5 @@

from lgsv import luogu, setting


def test_get_csrf_token():
luogu.get_csrf_token()

def test_luogu():
pass

0 comments on commit 6b688f1

Please sign in to comment.