Typora + Chevereto图床Markdown图片自动上传

Typora 配置

编写上传脚本

upload.py图床上传API调用程序

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
66
67
68
69
70
71
72
73
# -*- encoding: utf-8 -*-

import requests
import json
import mimetypes
import argparse
import sys

APP_DESC = """
一个上传图片到chevereto图床的命令行工具
"""

print(APP_DESC)
if len(sys.argv) == 1:
sys.argv.append('--help')

parser = argparse.ArgumentParser()
parser.add_argument('-s', '--source', type=str, nargs='+', help="", required=True)
parser.add_argument('-c', '--config', default="./config.json", help="读取配置文件", required=True)
args = parser.parse_args()

# 从参数中获取要上传的文件列表
img_list = args.source

def read_conf(path):
with open(path,"r",encoding="utf-8") as f:
confstr = f.read()
conf = json.loads(confstr)
#print(conf)
return conf

def up_to_chevereto(img_list):
# 获得本地图片路径后,上传至图床并记录返回的json字段
for img in img_list:
# 先判断传过来的是本地路径还是远程图片地址
if "http" == img[:4]:
# 非本地图片的话可以考虑下载到本地再上传,但是没这个必要
print(img)
continue
else:
try:
res_json = upload(formatSource(img))
parse_response_url(res_json,img)
except:
print(img+"\t上传失败")

def upload(files):
# 图床api
APIKEY = "你的key"
conf = read_conf(args.config)
url = conf['url'] + "?key=" + APIKEY
r = requests.post(url, files=files)
return json.loads(r.text)

def formatSource(filename):
imageList = []
mime_type = mimetypes.guess_type(filename)[0]
imageList.append(
('source', (filename, open(filename, 'rb'), mime_type))
)
return imageList

def parse_response_url(json, img_path):
# 从返回的json中解析字段
if json['status_code'] != 200:
print("{}\tweb端返回失败,可能是APIKey不对. status_code {} .".format(
img_path, json['status_code'])
)
else:
img_url = json["image"]["url"]
print(img_url)

up_to_chevereto(img_list)

config.json配置文件

1
2
3
4
{
"APIKEY": "你的key",
"url": "https://你的域名/api/1/upload/"
}

Windows下上传命令

1
2
python -u "脚本路径\upload.py" -c "C:配置文件路径\config.json" -s
# python -u "C:\Program Files\Typora\upload.py" -c "C:\Program Files\Typora\config.json" -s

工作效果

上传测试

image-20210623143140288

实际测试

image-20210623143228624

图片链接已自动更新

image-20210623143315195


Typora + Chevereto图床Markdown图片自动上传
https://jason-xy.github.io/2021/06/typora-chevereto-markdown/
作者
Jason Hsu
发布于
2021年6月23日
许可协议