@@ -14,8 +14,8 @@ class LCSpider:
1414 def __init__ (self ):
1515 self .session = requests .session ()
1616
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 )]
1919 self .difficulty_mapper = dict (Easy = '简单' , Medium = '中等' , Hard = '困难' )
2020
2121 # result
@@ -24,7 +24,7 @@ def __init__(self):
2424 self .md_table_en = []
2525
2626 def get_question_detail (self , question_title_slug ):
27- """fetch question detail by leetcode 's api"""
27+ """fetch question detail by lc 's api"""
2828 form_data = {
2929 'operationName' : 'globalData' ,
3030 'query' : 'query globalData {\n feature {\n questionTranslation\n subscription\n signUp\n '
@@ -48,7 +48,10 @@ def get_question_detail(self, question_title_slug):
4848 'Content-Type' : 'application/json' ,
4949 'Referer' : 'https://leetcode-cn.com/problems/' + question_title_slug
5050 }
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 )
5255
5356 form_data = {
5457 'operationName' : 'questionData' ,
@@ -71,21 +74,26 @@ def get_question_detail(self, question_title_slug):
7174 }
7275
7376 # 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 ,
7580 timeout = 10 )
7681 res = resp .json ()
7782 return res ['data' ]['question' ]
7883
7984 def get_all_questions (self ):
80- """fetch all question by leetcode 's api"""
85+ """fetch all question by lc 's api"""
8186 headers = {
8287 'accept' : 'application/json, text/javascript, */*; q=0.01' ,
8388 'content-type' : 'application/json' ,
8489 'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
8590 'Chrome/77.0.3865.120 Safari/537.36' ,
8691 'x-requested-with' : 'XMLHttpRequest'
8792 }
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 )
8997 questions = resp .json ()['stat_status_pairs' ]
9098
9199 for question in questions :
@@ -96,16 +104,17 @@ def get_all_questions(self):
96104
97105 question_title_en = question ['stat' ]['question__title' ]
98106 question_title_en = re .sub (r'[\\/:*?"<>|]' , '' , question_title_en ).strip ()
107+
99108 question_title_slug = question ['stat' ]['question__title_slug' ]
100109
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'
103115
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 ))
108116 print (frontend_question_id )
117+
109118 item = {
110119 'question_id' : question_id ,
111120 'frontend_question_id' : frontend_question_id ,
@@ -164,7 +173,8 @@ def save_result(self):
164173 f .write (json .dumps (self .result ))
165174
166175 def generate_readme (self ):
167- """generate README.md and README_EN.md files"""
176+ """generate readme files"""
177+ # generate README.md
168178 items = []
169179 table_cn = "\n | 题号 | 题解 | 标签 | 难度 | 备注 |\n | --- | --- | --- | --- | --- |"
170180 for item in sorted (self .md_table_cn , key = lambda x : x [0 ]):
@@ -176,6 +186,7 @@ def generate_readme(self):
176186 with open ('./README.md' , 'w' , encoding = 'utf-8' ) as f :
177187 f .write (readme_cn .format (table_cn ))
178188
189+ # generate README_EN.md
179190 items = []
180191 table_en = "\n | # | Solution | Tags | Difficulty | Remark |\n | --- | --- | --- | --- | --- |"
181192 for item in sorted (self .md_table_en , key = lambda x : x [0 ]):
@@ -197,22 +208,22 @@ def generate_question_readme(self):
197208 result = json .loads (result )
198209 for item in result :
199210 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" ]} '
201212 path = path .replace (":" , " " )
202213
203214 if os .path .isdir (path ):
204215 continue
205216 os .makedirs (path )
206217
207- # generate readme cn
218+ # generate lc problem readme cn
208219 with open (f'{ path } /README.md' , 'w' , encoding = 'utf-8' ) as f1 :
209220 f1 .write (readme_cn .format (int (item ['frontend_question_id' ]),
210221 item ["title_cn" ],
211222 item ['url_cn' ],
212223 item ['relative_path_en' ],
213224 item ['content_cn' ]))
214225
215- # generate readme en
226+ # generate lc problem readme en
216227 with open (f'{ path } /README_EN.md' , 'w' , encoding = 'utf-8' ) as f2 :
217228 f2 .write (readme_en .format (int (item ['frontend_question_id' ]),
218229 item ["title_en" ],
@@ -222,6 +233,7 @@ def generate_question_readme(self):
222233
223234 @staticmethod
224235 def generate_summary ():
236+ """generate summary files"""
225237 summary_cn = ""
226238 summary_en = ""
227239 for file in os .listdir ("./" ):
@@ -233,8 +245,11 @@ def generate_summary():
233245 summary_cn += f'\t - [{ sub } ](/solution/{ file } /{ enc } /README.md)\n '
234246 summary_en += f'\t - [{ sub } ](/solution/{ file } /{ enc } /README_EN.md)\n '
235247
248+ # generate summary.md
236249 with open ('./summary.md' , 'w' , encoding = 'utf-8' ) as f :
237250 f .write (summary_cn )
251+
252+ # generate summary_en.md
238253 with open ('./summary_en.md' , 'w' , encoding = 'utf-8' ) as f :
239254 f .write (summary_en )
240255
0 commit comments