@@ -14,8 +14,8 @@ class LCSpider:
14
14
def __init__ (self ):
15
15
self .session = requests .session ()
16
16
17
- # mapper
18
- self .number_mapper = [str (i * 100 ).zfill (4 ) + '-' + str (i * 100 + 99 ).zfill (4 ) for i in range (100 )]
17
+ # ['0000-0099', '0100-0199', ..., '9900-9999']
18
+ self .sub_folders = [str (i * 100 ).zfill (4 ) + '-' + str (i * 100 + 99 ).zfill (4 ) for i in range (100 )]
19
19
self .difficulty_mapper = dict (Easy = '简单' , Medium = '中等' , Hard = '困难' )
20
20
21
21
# result
@@ -24,7 +24,7 @@ def __init__(self):
24
24
self .md_table_en = []
25
25
26
26
def get_question_detail (self , question_title_slug ):
27
- """fetch question detail by leetcode 's api"""
27
+ """fetch question detail by lc 's api"""
28
28
form_data = {
29
29
'operationName' : 'globalData' ,
30
30
'query' : 'query globalData {\n feature {\n questionTranslation\n subscription\n signUp\n '
@@ -48,7 +48,10 @@ def get_question_detail(self, question_title_slug):
48
48
'Content-Type' : 'application/json' ,
49
49
'Referer' : 'https://leetcode-cn.com/problems/' + question_title_slug
50
50
}
51
- self .session .post (LCSpider .graph_url , data = json .dumps (form_data ), headers = headers , timeout = 10 )
51
+ self .session .post (url = LCSpider .graph_url ,
52
+ data = json .dumps (form_data ),
53
+ headers = headers ,
54
+ timeout = 10 )
52
55
53
56
form_data = {
54
57
'operationName' : 'questionData' ,
@@ -71,21 +74,26 @@ def get_question_detail(self, question_title_slug):
71
74
}
72
75
73
76
# get question detail
74
- resp = self .session .post (url = LCSpider .graph_url , data = json .dumps (form_data ).encode ('utf-8' ), headers = headers ,
77
+ resp = self .session .post (url = LCSpider .graph_url ,
78
+ data = json .dumps (form_data ).encode ('utf-8' ),
79
+ headers = headers ,
75
80
timeout = 10 )
76
81
res = resp .json ()
77
82
return res ['data' ]['question' ]
78
83
79
84
def get_all_questions (self ):
80
- """fetch all question by leetcode 's api"""
85
+ """fetch all question by lc 's api"""
81
86
headers = {
82
87
'accept' : 'application/json, text/javascript, */*; q=0.01' ,
83
88
'content-type' : 'application/json' ,
84
89
'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
85
90
'Chrome/77.0.3865.120 Safari/537.36' ,
86
91
'x-requested-with' : 'XMLHttpRequest'
87
92
}
88
- resp = self .session .get (url = 'https://leetcode.com/api/problems/all/' , headers = headers , allow_redirects = False )
93
+ resp = self .session .get (url = 'https://leetcode.com/api/problems/all/' ,
94
+ headers = headers ,
95
+ allow_redirects = False ,
96
+ timeout = 10 )
89
97
questions = resp .json ()['stat_status_pairs' ]
90
98
91
99
for question in questions :
@@ -96,16 +104,17 @@ def get_all_questions(self):
96
104
97
105
question_title_en = question ['stat' ]['question__title' ]
98
106
question_title_en = re .sub (r'[\\/:*?"<>|]' , '' , question_title_en ).strip ()
107
+
99
108
question_title_slug = question ['stat' ]['question__title_slug' ]
100
109
101
- url_cn = 'https://leetcode-cn.com/problems/' + question_title_slug
102
- url_en = 'https://leetcode.com/problems/' + question_title_slug
110
+ url_cn = f'https://leetcode-cn.com/problems/{ question_title_slug } '
111
+ url_en = f'https://leetcode.com/problems/{ question_title_slug } '
112
+
113
+ path_cn = f'/solution/{ self .sub_folders [no ]} /{ frontend_question_id } .{ quote (question_title_en )} /README.md'
114
+ path_en = f'/solution/{ self .sub_folders [no ]} /{ frontend_question_id } .{ quote (question_title_en )} /README_EN.md'
103
115
104
- path_cn = '/solution/{}/{}.{}/README.md' .format (self .number_mapper [no ], frontend_question_id ,
105
- quote (question_title_en ))
106
- path_en = '/solution/{}/{}.{}/README_EN.md' .format (self .number_mapper [no ], frontend_question_id ,
107
- quote (question_title_en ))
108
116
print (frontend_question_id )
117
+
109
118
item = {
110
119
'question_id' : question_id ,
111
120
'frontend_question_id' : frontend_question_id ,
@@ -164,7 +173,8 @@ def save_result(self):
164
173
f .write (json .dumps (self .result ))
165
174
166
175
def generate_readme (self ):
167
- """generate README.md and README_EN.md files"""
176
+ """generate readme files"""
177
+ # generate README.md
168
178
items = []
169
179
table_cn = "\n | 题号 | 题解 | 标签 | 难度 | 备注 |\n | --- | --- | --- | --- | --- |"
170
180
for item in sorted (self .md_table_cn , key = lambda x : x [0 ]):
@@ -176,6 +186,7 @@ def generate_readme(self):
176
186
with open ('./README.md' , 'w' , encoding = 'utf-8' ) as f :
177
187
f .write (readme_cn .format (table_cn ))
178
188
189
+ # generate README_EN.md
179
190
items = []
180
191
table_en = "\n | # | Solution | Tags | Difficulty | Remark |\n | --- | --- | --- | --- | --- |"
181
192
for item in sorted (self .md_table_en , key = lambda x : x [0 ]):
@@ -197,22 +208,22 @@ def generate_question_readme(self):
197
208
result = json .loads (result )
198
209
for item in result :
199
210
no = int (item ['frontend_question_id' ]) // 100
200
- path = f'./{ self .number_mapper [no ]} /{ item ["frontend_question_id" ]} .{ item ["title_en" ]} '
211
+ path = f'./{ self .sub_folders [no ]} /{ item ["frontend_question_id" ]} .{ item ["title_en" ]} '
201
212
path = path .replace (":" , " " )
202
213
203
214
if os .path .isdir (path ):
204
215
continue
205
216
os .makedirs (path )
206
217
207
- # generate readme cn
218
+ # generate lc problem readme cn
208
219
with open (f'{ path } /README.md' , 'w' , encoding = 'utf-8' ) as f1 :
209
220
f1 .write (readme_cn .format (int (item ['frontend_question_id' ]),
210
221
item ["title_cn" ],
211
222
item ['url_cn' ],
212
223
item ['relative_path_en' ],
213
224
item ['content_cn' ]))
214
225
215
- # generate readme en
226
+ # generate lc problem readme en
216
227
with open (f'{ path } /README_EN.md' , 'w' , encoding = 'utf-8' ) as f2 :
217
228
f2 .write (readme_en .format (int (item ['frontend_question_id' ]),
218
229
item ["title_en" ],
@@ -222,6 +233,7 @@ def generate_question_readme(self):
222
233
223
234
@staticmethod
224
235
def generate_summary ():
236
+ """generate summary files"""
225
237
summary_cn = ""
226
238
summary_en = ""
227
239
for file in os .listdir ("./" ):
@@ -233,8 +245,11 @@ def generate_summary():
233
245
summary_cn += f'\t - [{ sub } ](/solution/{ file } /{ enc } /README.md)\n '
234
246
summary_en += f'\t - [{ sub } ](/solution/{ file } /{ enc } /README_EN.md)\n '
235
247
248
+ # generate summary.md
236
249
with open ('./summary.md' , 'w' , encoding = 'utf-8' ) as f :
237
250
f .write (summary_cn )
251
+
252
+ # generate summary_en.md
238
253
with open ('./summary_en.md' , 'w' , encoding = 'utf-8' ) as f :
239
254
f .write (summary_en )
240
255
0 commit comments