基于博客园搭建Typora免费图床
本地需要有python3环境,复制以下代码到一个.py的文件中,如我本地命名为 upd_pic_to_cnblog.py
import os
import sys
import xmlrpc.client as client
import mimetypes
CONFIGS = {
"url": "",
"username": "",
"password": "",
"blogID": "",
}
def get_image(image_path):
image_mimetype = mimetypes.guess_type(image_path, True)[0]
image_name = os.path.basename(image_path)
image_bits = client.Binary(open(image_path, 'rb').read())
image = {
"name": image_name,
"type": image_mimetype,
"bits": image_bits,
}
return image
def upload_image(image_path):
image = get_image(image_path)
server = client.ServerProxy(CONFIGS.get("url"))
url = server.metaWeblog.newMediaObject(CONFIGS.get("blogID"), CONFIGS.get("username"),
CONFIGS.get("password"), image).get("url")
return url
def main(argv):
args = iter(argv)
next(args)
for image_path in args:
print(upload_image(image_path))
if __name__ == "__main__":
main(sys.argv)
将代码中 CONFIGS 参数修改为自己博客园后台相关参数
CONFIGS = {
"url": "",
"username": "",
"password": "",
"blogID": "",
}
用户id可以通过自己的博客园地址查看
账号信息可以在其他设置中查看,需要允许 MetaWeblog 博客客户端访问
修改后如下所示
打开Typora 文件 =》 偏好设置 =》图像 设置界面,按照下图所示进行配置
注意修改命令中的python文件所在目录位置
python D:\Files\Typora\upd_pic_to_cnblog.py
设置好后点击验证图片上传选项进行验证服务是否正常
相关参考
相关推荐