代码

# -*- coding: utf-8 -*-
# @Time: 2021/3/31
# @Author: Eritque arcus
# @File: downloadImage.py
import mimetypes
import os
import regex
import requests

if __name__ == '__main__':
p = input("输入hexo的source目录") # image path like D:\hexo\hexo\source
# 去尾
if p.endswith("\\"):
p = p[:len(p) - 1]
image_p = p + "\\images"
post_p = p + "\\_posts"
# 查看博文目录是否存在
if not os.path.exists(post_p):
print("路径下没_posts路径,请重新输入")
exit(1)
else:
os.chmod(p, 777)
# 查看图片目录是否存在
if not os.path.exists(image_p):
os.makedirs(p + "\\images")
else:
os.chmod(p, 777)
# 循环目录下每一个.md文件
for filename in os.listdir(post_p):
if filename.endswith(".md"):
print(filename)
path = post_p + "\\" + filename
# 以utf8编码读取
f = open(path, "r", encoding='utf8', errors='ignore')
i = 0
content = f.read()
f.close()
# 匹配markdown中的图片 即 ![tag](url)
for text in regex.findall(r"\!\[[^\s]*\]\([a-zA-z]+://[^\s]*\)", content):
print(text)
# 提取tag内容
tag = regex.findall(r"\!\[[^\s]*\](?=\([a-zA-z]+://[^\s]*\))", text)[0]
# 提取url内容
urldata = regex.findall(r"(?<=\!\[[^\s]*\])\([a-zA-z]+://[^\s]*\)", text)[0]
# 去掉括号
u = urldata[1:len(urldata) - 1]
# 提取当前文章的名字加上编号即为图片名字
name = filename + "-" + str(i)
# 获取图片
response = requests.get(u)
# 取请求头content-type属性获取扩展名如.png/.jpg
content_type = response.headers['content-type']
extension = mimetypes.guess_extension(content_type)
# 请求返回内容
img = response.content
# 写入图片文件
with open(image_p + "\\" + name + extension, 'wb') as f:
f.write(img)
f.close()
# 新引用地址
new_u = "/images/" + name + extension
# 替换原文
content = content.replace(text, tag + "(" + new_u + ")")
i += 1
# 写入文章文件
f = open(path, "w", encoding="utf8")
f.write(content)
f.close()