因为经常在火线撰写文章,本地是利用思源笔记来编写的(思源笔记 yyds),支持导出各种格式。但是每次上传文章后,本地的 markdown 在替换上去时,总要重新将图片上传一次,图片少了还好,图片一多,就感觉特别麻烦,于是想着写个脚本,方便对图片的处理操作。
为了方便好兄弟们直接操作,先贴出源码,使用说明,再对开发脚本的思路进行共享
import os
import requests
import json
import re
import urllib3
import shutil
import glob
# 禁用不安全请求警告
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
headers = {
'X-CSRF-Token': 'WBATX01hDCUjtKvympRiV7QkQkvGI5SewvaIv4ix',
'Cookie': 'Hm_lvt_0293e7bd41a38802c337b3133b5f0eb7=1696924650; flarum_remember=IWkphZ0Wk7kJqxTtmyBMf9d32w9CBxTfxhFNIAwy; flarum_session=XZbU2wuQOpCKtXdo6y2qS2j5h9iuGyZv2YuJRrXi; Hm_lpvt_0293e7bd41a38802c337b3133b5f0eb7=1696989728'
}
# 获取所有图片名
def get_all_files(folder_path):
file_names = []
for root, directories, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
file_names.append(file_path)
return file_names
# 上传图片获取实际上传地址
def post_upload_all_files(folder_path):
img_dict ={}
files = get_all_files(folder_path)
for file in files:
real_file = file
res = requests.post(url="https://zone.huoxian.cn/api/fof/upload", headers=headers, files={"files[]": open(real_file, "rb")}, verify=False)
#print(res.text)
data = json.loads(res.text)
url = data['data'][0]['attributes']['url']
pattern = r'https?://[^\s]+'
img_url = re.findall(pattern, url)[0]
print(img_url)
img_dict[file] = img_url
return img_dict
def replace_all_files(folder_path):
my_dict = post_upload_all_files(folder_path)
# 指定当前目录下的 Markdown 文件的匹配模式
md_files = glob.glob('*.md')
new_md_file = "new_"+md_files[0]
# 使用 shutil 模块的 copy2 函数复制文件
shutil.copy2(md_files[0], new_md_file)
with open(new_md_file, 'r', encoding='utf-8') as file:
file_content = file.read()
new_content = file_content.replace("", '')
#print(new_content)
for key, value in my_dict.items():
key = key.replace('\\', '/')
new_content = new_content.replace("("+key+")", "[upl-image-preview url="+value+"]")
#print(new_content)
with open(new_md_file, 'w', encoding='utf-8') as file:
file.write(new_content)
folder_path = 'assets' # 替换为实际的文件夹路径
replace_all_files(folder_path)
![6]
请求 社区主页 将获取到的 X-CSRF-Token
和 Cookie
替换到脚本中。文件夹包含 *.md(需要转换的原文章) 文件夹(图片文件夹) 执行后会得到 new_.md(转换后的新文章) 直接打开将其复制到火线社区内就可以
思路分享
根据指定的文件夹地址,获取对应的所有图片名
# 获取所有图片名
def get_all_files(folder_path):
file_names = []
for root, directories, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
file_names.append(file_path)
return file_names
将获取的文件按照火线原本发布文章上传的地址进行上传,并匹配出上传后的图片地址,为了方便后面进行替换操作,同时将与原本的文件名作为键值,匹配后的图片地址作为值一一对应
# 上传图片获取实际上传地址
def post_upload_all_files(folder_path):
img_dict ={}
files = get_all_files(folder_path)
for file in files:
real_file = file
res = requests.post(url="https://zone.huoxian.cn/api/fof/upload", headers=headers, files={"files[]": open(real_file, "rb")}, verify=False)
#print(res.text)
data = json.loads(res.text)
url = data['data'][0]['attributes']['url']
pattern = r'https?://[^\s]+'
img_url = re.findall(pattern, url)[0]
print(img_url)
img_dict[file] = img_url
return img_dict
接下来就是先匹配当前文件夹下的 md 文件 同时进行复制 和替换操作,最后保存起来。如果关于图片有不同的保存方式的话 也是可以在这里进行修改。
def replace_all_files(folder_path):
my_dict = post_upload_all_files(folder_path)
# 指定当前目录下的 Markdown 文件的匹配模式
md_files = glob.glob('*.md')
new_md_file = "new_"+md_files[0]
# 使用 shutil 模块的 copy2 函数复制文件
shutil.copy2(md_files[0], new_md_file)
with open(new_md_file, 'r', encoding='utf-8') as file:
file_content = file.read()
new_content = file_content.replace("", '')
#print(new_content)
for key, value in my_dict.items():
key = key.replace('\\', '/')
new_content = new_content.replace("("+key+")", "[upl-image-preview url="+value+"]")
#print(new_content)
with open(new_md_file, 'w', encoding='utf-8') as file:
file.write(new_content)