forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
222 lines (183 loc) · 7.69 KB
/
main.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import json
import os
import re
from urllib.parse import quote, unquote
from spider import Spider
# load templates
with open('./readme_template.md', 'r', encoding='utf-8') as f:
readme_cn = f.read()
with open('./readme_template_en.md', 'r', encoding='utf-8') as f:
readme_en = f.read()
with open('./problem_readme_template.md', 'r', encoding='utf-8') as f:
problem_readme_cn = f.read()
with open('./problem_readme_template_en.md', 'r', encoding='utf-8') as f:
problem_readme_en = f.read()
with open('./sql_problem_readme_template.md', 'r', encoding='utf-8') as f:
sql_readme_cn = f.read()
with open('./sql_problem_readme_template_en.md', 'r', encoding='utf-8') as f:
sql_readme_en = f.read()
with open('./bash_problem_readme_template.md', 'r', encoding='utf-8') as f:
bash_readme_cn = f.read()
with open('./bash_problem_readme_template_en.md', 'r', encoding='utf-8') as f:
bash_readme_en = f.read()
def select_templates(category):
if category == 'Shell':
return [bash_readme_cn, bash_readme_en]
if category == 'Database':
return [sql_readme_cn, sql_readme_en]
return [problem_readme_cn, problem_readme_en]
def generate_readme(result):
md_table_cn = [item['md_table_row_cn'] for item in result]
md_table_en = [item['md_table_row_en'] for item in result]
# generate README.md
items = []
table_cn = '\n| 题号 | 题解 | 标签 | 难度 | 备注 |\n| --- | --- | --- | --- | --- |'
for item in sorted(md_table_cn, key=lambda x: x[0]):
items.append(
f'\n| {item[0]} | {item[1]} | {item[2]} | {item[3]} | {item[4]} |'
)
table_cn += ''.join(items)
# generate README_EN.md
items = []
table_en = '\n| # | Solution | Tags | Difficulty | Remark |\n| --- | --- | --- | --- | --- |'
for item in sorted(md_table_en, key=lambda x: x[0]):
items.append(
f'\n| {item[0]} | {item[1]} | {item[2]} | {item[3]} | {item[4]} |'
)
table_en += ''.join(items)
with open('./README.md', 'w', encoding='utf-8') as f:
f.write(readme_cn.format(table_cn))
with open('./README_EN.md', 'w', encoding='utf-8') as f:
f.write(readme_en.format(table_en))
def generate_question_readme(result):
for item in result:
if not item['content_cn'] and not item['content_en']:
continue
path = (
f'./{item["sub_folder"]}/{item["frontend_question_id"]}.{item["title_en"]}'
)
path = path.replace(":", " ")
if os.path.isdir(path):
continue
os.makedirs(path)
# choose the readme template
category = item['category']
readme_template_cn, readme_template_en = select_templates(category)
# generate lc-cn problem readme
with open(f'{path}/README.md', 'w', encoding='utf-8') as f1:
f1.write(
readme_template_cn.format(
int(item['frontend_question_id']),
item["title_cn"],
item['url_cn'],
item['relative_path_en'],
item['content_cn'],
)
)
# generate lc-en problem readme
with open(f'{path}/README_EN.md', 'w', encoding='utf-8') as f2:
f2.write(
readme_template_en.format(
int(item['frontend_question_id']),
item["title_en"],
item['url_en'],
item['relative_path_cn'],
item['content_en'],
)
)
def generate_summary(result):
"""generate summary files"""
summary_cn = summary_en = ''
m = {int(item['frontend_question_id']): item for item in result}
for file in sorted(os.listdir("./"), key=lambda x: x.lower()):
if os.path.isdir("./" + file) and file != '__pycache__':
summary_cn += f'\n- {file}\n'
summary_en += f'\n- {file}\n'
for sub in sorted(os.listdir('./' + file), key=lambda x: x.lower()):
sub = sub.replace('`', ' ')
enc = quote(sub)
data = m.get(int(sub[:4]))
sub_cn = sub
if data:
sub_cn = sub[:5] + data['title_cn']
summary_cn += f' - [{sub_cn}](/solution/{file}/{enc}/README.md)\n'
summary_en += f' - [{sub}](/solution/{file}/{enc}/README_EN.md)\n'
# generate summary.md
with open('./summary.md', 'w', encoding='utf-8') as f:
f.write(summary_cn)
# generate summary_en.md
with open('./summary_en.md', 'w', encoding='utf-8') as f:
f.write(summary_en)
def refresh(result):
"""update problems"""
pattern = re.compile("src=\"(.*?)\"")
for question in result:
front_question_id = question['frontend_question_id']
print(front_question_id)
title = question['title_cn']
title_en = question['title_en']
path_cn = unquote(str(question['relative_path_cn']).replace("/solution", "."))
path_en = unquote(str(question['relative_path_en']).replace("/solution", "."))
with open(path_cn, 'r', encoding='utf-8') as f1:
cn_content = f1.read()
# update title
with open(path_en, 'r', encoding='utf-8') as f2:
en_content = f2.read()
i = cn_content.index('. ')
j = cn_content.index(']')
cn_content = cn_content.replace(cn_content[i + 2 : j], title)
i = en_content.index('. ')
j = en_content.index(']')
en_content = en_content.replace(en_content[i + 2 : j], title_en)
# update question content
old_content = re.search("<!-- 这里写题目描述 -->(.*?)## 解法", cn_content, re.S).group(1)
cn_content = cn_content.replace(
old_content, "\n\n" + question['content_cn'] + "\n\n"
).replace("\n\n <ul>", "\n <ul>")
# replace image url to cdn link
for url in pattern.findall(cn_content) or []:
image_name = (
os.path.basename(url).replace('.PNG', '.png').replace('.JPG', '.jpg')
)
new_url = (
'https://fastly.jsdelivr.net/gh/doocs/leetcode@main'
+ str(question['relative_path_cn']).replace("README.md", "images/")
+ image_name
)
cn_content = cn_content.replace(url, new_url)
with open(path_cn, 'w', encoding='utf-8') as f1:
f1.write(cn_content)
old_content = re.search(
"## Description(.*?)## Solutions", en_content, re.S
).group(1)
en_content = en_content.replace(
old_content, "\n\n" + question['content_en'] + "\n\n"
).replace("\n\n <ul>", "\n <ul>")
for url in pattern.findall(en_content) or []:
image_name = (
os.path.basename(url).replace('.PNG', '.png').replace('.JPG', '.jpg')
)
new_url = (
'https://fastly.jsdelivr.net/gh/doocs/leetcode@main'
+ str(question['relative_path_cn']).replace("README.md", "images/")
+ image_name
)
en_content = en_content.replace(url, new_url)
with open(path_en, 'w', encoding='utf-8') as f2:
f2.write(en_content)
def save(result):
with open('./result.json', 'w', encoding='utf-8') as f:
f.write(json.dumps(result))
if __name__ == '__main__':
cookie_cn = ''
cookie_en = ''
spider = Spider(cookie_cn, cookie_en)
res = spider.run()
save(res)
with open('./result.json', 'r', encoding='utf-8') as f:
res = f.read()
res = json.loads(res)
generate_readme(res)
generate_question_readme(res)
generate_summary(res)
refresh(res)