-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.py
149 lines (122 loc) · 4.3 KB
/
update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import datetime as dt
import argparse
import requests
import os
import re
LEVEL_NAME = [
"UNRATED",
"Bronze V",
"Bronze IV",
"Bronze III",
"Bronze II",
"Bronze I",
"Silver V",
"Silver IV",
"Silver III",
"Silver II",
"Silver I",
"Gold V",
"Gold IV",
"Gold III",
"Gold II",
"Gold I",
"Platinum V",
"Platinum IV",
"Platinum III",
"Platinum II",
"Platinum I",
"Diamond V",
"Diamond IV",
"Diamond III",
"Diamond II",
"Diamond I",
"Ruby V",
"Ruby IV",
"Ruby III",
"Ruby II",
"Ruby I",
]
CODE_DIR_PYTHON = "src/python"
def get_problem(p_id: int) -> dict | None:
resp = requests.get(f"https://solved.ac/api/v3/search/suggestion?query={p_id}")
resp.raise_for_status()
data = resp.json()
if not data.get("problemCount", 0):
print(f"Problem {p_id} not found!!")
return None
p = data.get("problems", [])
return p[0]
def append_readme(*args):
with open("README.md", "a", encoding="utf-8") as f:
for code in args:
p = get_problem(code)
if not p:
continue
title: str | None = p.get("caption")
level: str | None = p.get("level")
if title is None or level is None:
print(f"Problem {code} data error!!")
continue
t_date = dt.date.today()
# t_date_str = str(t_date) if t_date.weekday() < 5 else f"**{t_date}**"
t_date_str = str(t_date) # 주말 하이라이팅 제거(?)
t_tier_img = f"<img src=\"icon/{level}.svg\" height=\"18px\" alt=\"{LEVEL_NAME[level]}\" title=\"{LEVEL_NAME[level]}\"/>"
t_problem = f"[{code}. {title}](https://www.acmicpc.net/problem/{code})"
t_code = f"[python]({CODE_DIR_PYTHON}/P{code}.py)"
t_note = ""
f.write(f"|{t_date_str}|{t_tier_img}|{t_problem}|⬛|{t_code}|{t_note}|\n")
if os.path.exists(f"P{code}.py"):
print(f"{LEVEL_NAME[level]:12s} - [{code}. {title}] / code file already exists!!")
continue
else:
code_file_path = os.path.join(CODE_DIR_PYTHON, f"P{code}.py")
with open(code_file_path, "w", encoding="utf-8") as f2:
f2.write(f"# https://www.acmicpc.net/problem/{code}\n# {dt.date.today()} / {code}. {title} / {LEVEL_NAME[level]}\n")
print(f"{LEVEL_NAME[level]:12s} - [{code}. {title}] added!!")
def edit_readme(*args):
with open("README.md", "r", encoding="utf-8") as f:
lines = f.readlines()
idx = lines.index("<!-- TABLE START -->\n")
for line in lines[idx + 3:]:
data = line[1:-1].split("|")
print(data[2])
def generate_markdown(*args):
# args: code(문제 번호) 있는 list
for code in args:
p = get_problem(code)
if not p:
continue
title: str | None = p.get("title")
level: str | None = p.get("level")
if title is None or level is None:
print(f"Problem {code} data error!!")
continue
with open(f"docs/P{code}.md", "w", encoding="utf-8") as f:
f.write(f"# {code}. {title} ({LEVEL_NAME[level]})\n[소스코드(Python)]({CODE_DIR_PYTHON}/P{code}.py)")
def edit_readme_markdown():
pass
def download_icons():
for i, name in enumerate(LEVEL_NAME):
with open(os.path.join("icon", f"{i}.svg"), "wb") as f:
resp = requests.get(f"https://static.solved.ac/tier_small/{i}.svg")
if resp.status_code != 200:
print(f"ERROR: {resp.status_code}")
continue
data = resp.content
if not isinstance(data, bytes):
print("ERROR: 알 수 없는 오류")
continue
f.write(data)
print(f"다운로드 완료: {resp.url}")
def main():
pass
if __name__ == "__main__":
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("-m", "--markdown", help="Markdown file generate mode", action="store_true")
arg_parser.add_argument("number", help="BOJ problem code numbers", type=int, nargs="+")
args = arg_parser.parse_args()
if args.markdown:
edit_readme_markdown()
else:
append_readme(*args.number)
# edit_readme()