优点:
自带 CDN,大厂基本不会被删(但还是建议本地保留一份),加载速度极快
缺点:
网上发布会有跨域问题
下载:https://github.com/xlzy520/typora-plugin-bilibili/releases 中的 main.exe
程序到本地
创建 python 脚本 MarkdownPicTOBilibiliPic.py
import os
import re
import subprocess
def get_md_files(folder_path):
"""获取指定文件夹下所有.md文件的路径"""
md_files = []
for root, _, files in os.walk(folder_path):
for file in files:
if file.endswith(".md"):
md_files.append(os.path.join(root, file))
return md_files
def extract_image_urls(md_file_path):
"""从.md文件中提取所有图片的相对链接"""
pattern = r'!\[.*?\]\((.*?)\)' # 匹配 ![标识](url)
urls = []
with open(md_file_path, 'r', encoding='utf-8') as file:
content = file.read()
urls = re.findall(pattern, content)
return urls, content
def get_absolute_path(md_file_path, relative_url):
"""根据.md文件路径和相对图片链接生成图片的绝对路径"""
md_dir = os.path.dirname(md_file_path)
absolute_path = os.path.normpath(os.path.join(md_dir, relative_url))
return absolute_path
def replace_urls_in_content(content, old_new_url_map):
"""将文件内容中的旧URL替换为新URL"""
for old_url, new_url in old_new_url_map.items():
content = content.replace(old_url, new_url)
return content
def process_md_files(folder_path, main_exe_path, token, csrf):
"""核心功能实现"""
md_files = get_md_files(folder_path)
print(f"找到 {len(md_files)} 个.md文件:")
for md_file in md_files:
print(f"- {md_file}")
for md_file_path in md_files:
print(f"\n处理文件: {md_file_path}")
# 提取图片相对URL和文件内容
urls, content = extract_image_urls(md_file_path)
print(f"找到 {len(urls)} 个图片链接。")
# URL映射:{相对URL: 新URL}
old_new_url_map = {}
for relative_url in urls:
# 获取图片的绝对路径
absolute_path = get_absolute_path(md_file_path, relative_url)
print(f"相对路径: {relative_url} -> 绝对路径: {absolute_path}")
# 调用main.exe,获取新的图片URL
command = f'"{main_exe_path}" token={token} csrf={csrf} "{absolute_path}"'
print(f"执行命令: {command}")
try:
result = subprocess.run(command, capture_output=True, text=True, shell=True)
new_url = result.stdout.strip() # 假设返回的URL在stdout中
if new_url:
old_new_url_map[relative_url] = new_url
print(f"新图片URL: {new_url}")
else:
print(f"未能获取新URL,跳过: {relative_url}")
except Exception as e:
print(f"执行命令时出错: {e}")
# 替换内容中的旧URL为新URL
updated_content = replace_urls_in_content(content, old_new_url_map)
# 将更新后的内容写回.md文件
with open(md_file_path, 'w', encoding='utf-8') as file:
file.write(updated_content)
print(f"文件更新完成:{md_file_path}")
if __name__ == "__main__":
# 文件夹路径(需要遍历.md文件的目录)
folder_path = r"C:/Users/t295/Downloads/ (1)"
# main.exe路径
main_exe_path = r"C:/Users/t295/Downloads/main.exe"
# Token和csrf参数
token = "f45d0aad%2C175房东0f24%2A11CjD57FWOhgdLG43LXCsczMMWPfE14HdoiJ1adu47okRjUqXl-9bDZGX8nvxbml8LIgESVkxMbEJzSzE0eldYakVwaUxKeThnbll5bFBFV1JLcTF4R0RxcnBuSjVRTDNOcDB1NXVhMG0tMFpvRVJkMXdCRncxckFHMnB6ZGJYRGxnMUNBZ0ZuU0p3IIEC"
csrf = "7c703b2f1f27f52反对f5c17409850f"
process_md_files(folder_path, main_exe_path, token, csrf)
修改内容:
# markdown文件夹路径(需要遍历.md文件的目录)
folder_path = r"C:/Users/t295/Downloads/markdown文件夹/"
# main.exe路径
main_exe_path = r"C:/Users/t295/Downloads/main.exe"
# Token和csrf参数
token = "f45d0aad%2C170f24%WOhgdLG43LXCsczMMWPfE14HdoiJ1adu47okRjUqXl-9bDZGX8nvxbml8LIgESVkxMbEJzSzE0eldYakVwaUxKeThnbll5bFBFV1JLcTF4R0RxcnBuSjVRTDNOcDB1NXVhMG0tMFpvRVJkMXdCRncxckFHMnB6ZGJYRGxnMUNBZ0ZuU0p3IIEC"
csrf = "7c703b2f1f27ff59850f"
注意其中的 cmd 命令将路径进行替换
同时将 token 和 csrf 进行替换,具体位置在 b 站中登录后,在应用程序(F12⇒ 应用程序)中查看 cookie 了,两个值分别对应:推荐使用浏览器插件 Cookie-Editor,更加方便:
- SESSDATA⇒token
- bili_jct⇒csrf
最后运行脚本即可,最后随机打开一个查看效果:
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于