1. 搭建Github 图床
1.1. 创建或者选择一个Public 仓库
1.2. 获取Github Token
2. Typora 结合图床
当Typora需要使用这个图床的时候,Picgo 是很好的解决方案,但是还可以通过其他的方式来进行上传,比如python 脚本。
2.1. upload-img.py
import requests
import base64
import json
import uuid
import datetime
import os
import sys
ext = ""
# 从文件夹下 读取文件
def read_dir(file):
global ext
if not os.path.isdir(file): # 判断是否是文件夹,不是文件夹才打开
ext = os.path.splitext(file)[1] # 文件后缀
with open(file, 'rb') as f: # rb 二进制 读取
fdata_tmp = file_base64(f.read())
f.close()
return fdata_tmp
# 将文件转换为base64编码,github上传文件必须将文件以base64格式上传
def file_base64(data):
data_b64 = base64.b64encode(data).decode()
return data_b64
# 上传文件
def upload_file(file_data):
global ext
file_name = str(uuid.uuid1()) + ext # 文件名 随机生成
# url = "https://api.github.com/repos/[user]/[repo]/contents/[path]/"+file_name # 用户名、库名、路径
# headers = {"Authorization": "Bearer " + token}
# ghp_****
# JaikenWong/Drawing-Bed
# images
# main
token = "ghp_******" ## github -> settings -> Developer Settings -> Personal access tokens -> Tokens(classic) -> Generate new token (classic)
curr_time = datetime.datetime.now()
path = curr_time.strftime("%Y-%m-%d") # 日期文件夹
url = "https://api.github.com/repos/JaikenWong/Drawing-Bed/contents/images/" + path + "/" + file_name # 用户名、库名、路径
headers = {
"Authorization": "Bearer " + token
}
content = file_data
data = {"message": "zj upload pictures", "content": content}
data = json.dumps(data)
req = requests.put(url=url, data=data, headers=headers)
req.encoding = "utf-8"
re_data = json.loads(req.text)
print("https://cdn.jsdelivr.net/gh/JaikenWong/Drawing-Bed@main/images/" + path +
"/" + file_name)
# 这个打印会回显给Typora cdn.jsdelivr.net 是免费的加速cdn
if __name__ == '__main__':
args = sys.argv[1:]
for img_path in args:
# 从文件夹 读取文件
fdata = read_dir(img_path)
upload_file(fdata)
2.2. 效果展示
这里是一些相关打印都 打印出来的情况。上面代码不包含这些print
2.3. 配置在Typora
2.4. 命令配置
/Users/jaiken/miniconda3/bin/python -u /Users/jaiken/workplace/pypro/learning/upload-img.py