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
| import os import re import sys
import requests
pngquant_exe = sys.path[0] + './pngquant/pngquant.exe' images_path_out = 'images'
def compression(file_path: str): cmd = f'{pngquant_exe} --force {file_path} --quality 80 -o {file_path}' os.system(cmd)
def get_files_with_extension(folder_path, extension): file_names = [] for root, dirs, files in os.walk(folder_path): for file in files: if file.endswith(extension): file_names.append(os.path.abspath(os.path.join(root, file))) return file_names
def replace_urls_in_file(file_path, replacement_func): with open(file_path, 'r', encoding='utf-8') as file: content = file.read()
match_count = [0]
def replacement(match): match_count[0] += 1 matched_string = match.group() return replacement_func(matched_string, file_path, match_count[0])
str_pattern = re.compile(r'https://files\.mdnice\.com\S*\.png') new_content = str_pattern.sub(replacement, content) with open(file_path, 'w', encoding='utf-8') as file: file.write(new_content)
def download_png(url: str, out_path: str): try: response = requests.get(url, proxies={ "http": None, "https": None, }, timeout=3) response.raise_for_status() with open(out_path, 'wb') as file: file.write(response.content) compression(out_path) except requests.RequestException as e: raise e
def dynamic_replacement(matched_string, file_path, num): dir_name, base_name = os.path.split(file_path) last_component = os.path.basename(dir_name) file_name, ext = os.path.splitext(base_name) out_path = os.path.join(images_path_out, last_component, file_name) if not os.path.exists(out_path): os.makedirs(out_path) out_file_path = os.path.join(out_path, f'{num}.png').replace(os.path.sep, '/') download_png(matched_string, out_file_path) return f'{out_file_path}'
if __name__ == '__main__': file_list = get_files_with_extension('D:/WebstormProjects/hexo-blog/source/_posts', '.md') for item in file_list: replace_urls_in_file(item, dynamic_replacement)
|