-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch_problem.py
208 lines (168 loc) · 5.97 KB
/
fetch_problem.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
USAGE:
길게
python3 fetch_problem.py --name "양영광" --week "week3" --problems 11725 1967 3584 ...
단축
python3 fetch_problem.py -n "양영광" -w "week3" -p 11725 1967 3584 ...
단축2
python3 fetch_problem.py -n 양영광 -w week3 -p 문제번호1 문제번호2
"""
from collections import defaultdict
import json
import urllib.request
import argparse
import datetime
import html
import os
__template = """
\"\"\"
문제 이름: <problem_name>
문제 번호: <problem_id>
문제 링크: <url>
난이도: <difficulty>
태그: <tags>
\"\"\"
import sys
def input(): return sys.stdin.readline().rstrip()
"""
__tier_text = {
0: "Unknown",
1: "Bronze V",
2: "Bronze IV",
3: "Bronze III",
4: "Bronze II",
5: "Bronze I",
6: "Silver V",
7: "Silver IV",
8: "Silver III",
9: "Silver II",
10: "Silver I",
11: "Gold V",
12: "Gold IV",
13: "Gold III",
14: "Gold II",
15: "Gold I",
16: "Platinum V",
17: "Platinum IV",
18: "Platinum III",
19: "Platinum II",
20: "Platinum I",
21: "Diamond V",
22: "Diamond IV",
23: "Diamond III",
24: "Diamond II",
25: "Diamond I",
26: "Ruby V",
27: "Ruby IV",
28: "Ruby III",
29: "Ruby II",
30: "Ruby I",
31: "Master",
}
def make_problem_content(template, problem: dict) -> str:
return template\
.replace('<problem_name>', problem['name']) \
.replace('<problem_id>', str(problem['id'])) \
.replace('<url>', problem['url']) \
.replace('<difficulty>', problem['difficulty']) \
.replace('<tags>', ', '.join(problem['tags'])) \
def write_all_text(path: str, text: str) -> None:
f = open(path, 'w', encoding='utf-8')
f.write(text)
f.close()
def get_problems(problems: list) -> list:
"""문제 정보를 담고 있는 딕셔너리를 반환합니다.
"""
if len(problems) < 1:
raise ValueError("Invalid number of problems")
url = f"https://solved.ac/api/v3/problem/lookup?problemIds={','.join(problems)}"
response = urllib.request.urlopen(url)
source = response.read()
# if response.status_code != 200:
# raise Exception("Unexpected response status")
json_data = json.loads(source)
problems = []
for problem in json_data:
problem_info = defaultdict(str)
problem_info["id"] = problem["problemId"]
problem_info["name"] = html.unescape(problem["titleKo"])
problem_info["level"] = problem["level"]
problem_info["difficulty"] = __tier_text[problem["level"]]
problem_info["url"] = f"https://www.acmicpc.net/problem/{problem['problemId']}"
# language ko만 추출
if problem["tags"] is not None:
tags = [x["displayNames"] for x in problem["tags"]]
problem_info["tags"] = [x["name"]
for tags in tags for x in tags if x["language"] == 'ko']
problems.append(problem_info)
return problems
# response = requests.get("https://solved.ac/api/v3/problem/show?problemId=1052")
# json_data = response.json()
class CommandLineParser:
def __init__(self):
parser = argparse.ArgumentParser(description="백준 문제 가져오기 사용법")
parser.add_argument("-p", "--problems", nargs='+',
help="사용법 -p 1052 3023", required=True, default="")
parser.add_argument(
"-w", "--week", help="사용법: -w (week폴더이름) ex: week1", required=True, default="")
parser.add_argument(
"-n", "--name", help="사용법: -n 자신의 이름", required=True, default="")
try:
self.__problems = None
self.__week = None
self.__name = None
argument = parser.parse_args()
if argument.problems:
try:
self.__problems = argument.problems
except:
print("문제번호는 숫자로 기입해주세요.")
if argument.week:
self.__week = argument.week
if argument.name:
self.__name = argument.name
except:
parser.print_help()
def validation(self):
return self.__problems is not None and self.__week is not None and self.__name is not None
def problems(self):
return self.__problems
def save_dir(self):
return self.__name
def week_dir(self):
return self.__week
def execute(app: CommandLineParser) -> None:
if app.validation():
problems = app.problems()
# 이름 폴더
save_dir = app.save_dir()
# 주 단위 폴더
week_dir = app.week_dir()
# 입력받은 이름 폴더 ( abs path )
target_dir = os.path.join(os.getcwd(), save_dir)
abs_week_dir = os.path.join(target_dir, week_dir)
if not os.path.isdir(target_dir):
print(f"{save_dir} 디렉터리가 존재하지 않습니다.")
return
# if os.path.isdir(os.path.join(target_dir, week_dir)):
# print(f"입력한 디렉터리 -{week_dir}이 이미 존재합니다.")
# return
if not os.path.isdir(abs_week_dir):
os.mkdir(abs_week_dir)
print("문제 정보를 생성합니다.")
problems_info = get_problems(problems)
for problem in problems_info:
content = make_problem_content(__template, problem)
dir_name = ''.join(x for x in problem['name'] if x.isalnum())
problem_dir = os.path.join(
abs_week_dir, f"[{problem['id']}]{dir_name}")
if not os.path.isdir(problem_dir):
os.mkdir(problem_dir)
write_all_text(os.path.join(
problem_dir, f"{problem['id']}.py"), content)
write_all_text(os.path.join(
problem_dir, f"README.md"), f"# {dir_name}")
print(problems_info)
if __name__ == '__main__':
app = CommandLineParser()
execute(app)