@@ -190,13 +190,33 @@ def make_all_pdf(source, output, timeout, compress, power, port):
190190 try :
191191 os .popen ("lsof -i:" + str (port ) + " | grep -v 'PID' | awk '{print $2}' | xargs kill -9" )
192192 parts_dir = os .path .join (dirname , "parts" )
193- part_dir = os .path .join (parts_dir , os .path .basename (dirname ))
194- if not os .path .exists (part_dir ):
195- os .makedirs (part_dir , exist_ok = True )
196193 pattern = r'https?://[^\s]+'
197194 fpath = os .path .join (dirname , "mkdocs.yml" )
198195 data = yaml .safe_load (open (fpath ))
199196 print (f'dirname: { dirname } ' )
197+ output_dir = f'{ output } /{ os .path .basename (os .path .dirname (dirname ))} '
198+ if len (data .get ('nav' )) < 200 :
199+ curr_pdf_name = f"{ os .path .basename (dirname )} .pdf"
200+ curr_output_file = os .path .join (output_dir , curr_pdf_name )
201+ if os .path .exists (curr_output_file ):
202+ if os .path .exists (parts_dir ):
203+ shutil .rmtree (parts_dir )
204+ break
205+ else :
206+ curr_pdf_name = f"{ os .path .basename (dirname )} -上.pdf"
207+ curr_output_file_1 = os .path .join (output_dir , curr_pdf_name )
208+
209+ curr_pdf_name = f"{ os .path .basename (dirname )} -下.pdf"
210+ curr_output_file_2 = os .path .join (output_dir , curr_pdf_name )
211+ if os .path .exists (curr_output_file_1 ) and os .path .exists (curr_output_file_2 ):
212+ if os .path .exists (parts_dir ):
213+ shutil .rmtree (parts_dir )
214+ break
215+
216+ part_dir = os .path .join (parts_dir , os .path .basename (dirname ))
217+ if not os .path .exists (part_dir ):
218+ os .makedirs (part_dir , exist_ok = True )
219+
200220 with tempfile .TemporaryDirectory () as tmpdir :
201221 shutil .copytree (dirname , tmpdir , dirs_exist_ok = True )
202222 for nav in data .get ('nav' ):
@@ -221,7 +241,7 @@ def make_all_pdf(source, output, timeout, compress, power, port):
221241 matches = re .findall (pattern , mk_data )
222242 images = []
223243 for match in matches :
224- if 'static001.geekbang.org' not in match :
244+ if 'static001.geekbang.org/' not in match or 'image/ ' not in match :
225245 continue
226246 match = match if match .count (')' ) <= 0 else match [:match .index (')' )]
227247 if match .count ('(' ):
@@ -294,7 +314,6 @@ def make_all_pdf(source, output, timeout, compress, power, port):
294314 proc .kill ()
295315 os .popen ("lsof -i:" + str (port ) + " | grep -v 'PID' | awk '{print $2}' | xargs kill -9" )
296316
297- output_dir = f'{ output } /{ os .path .basename (os .path .dirname (dirname ))} '
298317 if not os .path .exists (output_dir ):
299318 os .makedirs (output_dir , exist_ok = True )
300319
0 commit comments