1
1
import json
2
2
import os
3
- from urllib .parse import quote
3
+ import re
4
+ from urllib .parse import quote , unquote
4
5
5
6
from spider import Spider
6
7
@@ -130,6 +131,66 @@ def generate_summary(result):
130
131
f .write (summary_en )
131
132
132
133
134
+ def refresh (result ):
135
+ """update problems"""
136
+ pattern = re .compile ("src=\" (.*?)\" " )
137
+
138
+ for question in result :
139
+ front_question_id = question ['frontend_question_id' ]
140
+ print (front_question_id )
141
+
142
+ path_cn = unquote (str (question ['relative_path_cn' ]).replace ("/solution" , "." ))
143
+ path_en = unquote (str (question ['relative_path_en' ]).replace ("/solution" , "." ))
144
+
145
+ with open (path_cn , 'r' , encoding = 'utf-8' ) as f1 :
146
+ cn_content = f1 .read ()
147
+
148
+ with open (path_en , 'r' , encoding = 'utf-8' ) as f2 :
149
+ en_content = f2 .read ()
150
+
151
+ # update question content
152
+ old_content = re .search ("<!-- 这里写题目描述 -->(.*?)## 解法" , cn_content , re .S ).group (1 )
153
+ cn_content = cn_content .replace (
154
+ old_content , "\n \n " + question ['content_cn' ] + "\n \n "
155
+ ).replace ("\n \n <ul>" , "\n <ul>" )
156
+
157
+ # replace image url to cdn link
158
+ for url in pattern .findall (cn_content ) or []:
159
+ image_name = (
160
+ os .path .basename (url ).replace ('.PNG' , '.png' ).replace ('.JPG' , '.jpg' )
161
+ )
162
+ new_url = (
163
+ 'https://cdn.jsdelivr.net/gh/doocs/leetcode@main'
164
+ + str (question ['relative_path_cn' ]).replace ("README.md" , "images/" )
165
+ + image_name
166
+ )
167
+ cn_content = cn_content .replace (url , new_url )
168
+
169
+ with open (path_cn , 'w' , encoding = 'utf-8' ) as f1 :
170
+ f1 .write (cn_content )
171
+
172
+ old_content = re .search (
173
+ "## Description(.*?)## Solutions" , en_content , re .S
174
+ ).group (1 )
175
+ en_content = en_content .replace (
176
+ old_content , "\n \n " + question ['content_en' ] + "\n \n "
177
+ ).replace ("\n \n <ul>" , "\n <ul>" )
178
+
179
+ for url in pattern .findall (en_content ) or []:
180
+ image_name = (
181
+ os .path .basename (url ).replace ('.PNG' , '.png' ).replace ('.JPG' , '.jpg' )
182
+ )
183
+ new_url = (
184
+ 'https://cdn.jsdelivr.net/gh/doocs/leetcode@main'
185
+ + str (question ['relative_path_cn' ]).replace ("README.md" , "images/" )
186
+ + image_name
187
+ )
188
+ en_content = en_content .replace (url , new_url )
189
+
190
+ with open (path_en , 'w' , encoding = 'utf-8' ) as f2 :
191
+ f2 .write (en_content )
192
+
193
+
133
194
def save (result ):
134
195
with open ('./result.json' , 'w' , encoding = 'utf-8' ) as f :
135
196
f .write (json .dumps (result ))
@@ -140,6 +201,7 @@ def save(result):
140
201
cookie_en = ''
141
202
spider = Spider (cookie_cn , cookie_en )
142
203
res = spider .run ()
204
+ save (res )
143
205
144
206
# with open('./result.json', 'r', encoding='utf-8') as f:
145
207
# res = f.read()
@@ -148,4 +210,4 @@ def save(result):
148
210
generate_readme (res )
149
211
generate_question_readme (res )
150
212
generate_summary (res )
151
- save (res )
213
+ # refresh (res)
0 commit comments