-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathreplace_image_link.py
65 lines (51 loc) · 2.26 KB
/
replace_image_link.py
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
import os
import re
import hashlib
import logging
# 配置日志
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
def calculate_md5(file_path):
"""计算文件的MD5哈希值"""
logging.info(f"计算文件 {file_path} 的MD5哈希值")
with open(file_path, "rb") as f:
file_hash = hashlib.md5()
while chunk := f.read(8192):
file_hash.update(chunk)
return file_hash.hexdigest()
def update_markdown_images(md_file_path):
"""更新Markdown文件中的图片链接"""
logging.info(f"开始更新Markdown文件 {md_file_path} 中的图片链接")
# 获取Markdown文件的目录
md_file_dir = os.path.dirname(md_file_path)
logging.info(f"Markdown文件目录: {md_file_dir}")
with open(md_file_path, "r", encoding="utf-8") as file:
content = file.read()
# 匹配Markdown中的图片路径
image_pattern = re.compile(r"!\[.*?\]\((.*?)\)")
matches = image_pattern.findall(content)
for image_path in matches:
# 将图片路径转换为绝对路径
absolute_image_path = os.path.join(md_file_dir, image_path)
if os.path.exists(absolute_image_path):
logging.info(f"找到图片路径: {absolute_image_path}")
# 计算图片的MD5哈希值
md5_hash = calculate_md5(absolute_image_path).lower()
# 构造新的图片链接
new_image_link = f"https://pica.zhimg.com/80/v2-{md5_hash}.png"
logging.info(f"将图片链接 {image_path} 替换为 {new_image_link}")
# 替换Markdown中的图片链接
content = content.replace(image_path, new_image_link)
# 删除图片源文件
os.remove(absolute_image_path)
logging.info(f"已删除图片源文件: {absolute_image_path}")
else:
logging.warning(f"图片路径 {absolute_image_path} 不存在")
# 将更新后的内容写回文件
with open(md_file_path, "w", encoding="utf-8") as file:
file.write(content)
logging.info(f"完成更新Markdown文件 {md_file_path}")
# 使用示例
markdown_file_path = "source/_posts/曲阜师范大学教务系统成绩监控(无需服务器).md"
update_markdown_images(markdown_file_path)