typora_upload_image For Python

typora_upload_image For Python

author:troy
website : https://www.zygd.site

Because I need to use it for free cdn.jsdelivr.net Acceleration service for

Therefore, I need to limit the warehouse capacity to 50MB

And the GitHub API used by picgo always fails, so I wrote a python script myself

This script will complete the timestamp rename, copy the file to the specified directory, and upload the service

Warehouse address and link need to be configured

The code is as follows

# _*_ coding: utf-8 _*_
import os
import time
import sys
from shutil import copyfile
from sys import exit
import datetime

push_path = "D:\\Github\\api-temp\\"  # Address of GitHub image storage TODO
# TODO
# If you don't need to cdn.jsdelivr.net  Speed up, please change the size_ Text() with copy_ img()


def size_text():  # The default matching file size is 50mb because cdn.jsdelivr.net  No acceleration after 50mb limit
    a = os.popen('dir D:\\Github\\api-temp\\ | find "文件"')  # Here we need to pay attention to Chinese and English TODO
    text = a.read()
    size = (text.split()[2].replace(',', ''))
    size = int(size)
    if size <= 411041792:
        print("Upload Success:")
        copy_img()
    else:
        os.popen('msg %username% /time:10 "File memory is about to exceed budget, please note!"')
        os.popen('msg %username% /time:10 "This time will not upload, please pay attention to whether you need to remind"')
        exit()
        # os.open('echo -e "\a"') # linux Please comment on the line above and uncomment this line
    a.close()


def copy_img():
    for i in range(1, len(sys.argv)):
        new_name = datetime.datetime.now().strftime("%Y%m%d%H%M%S%f")
        oldname = sys.argv[int(i)]
        oldnames = oldname.split(".")[1]  # Get file suffix
        new_names = push_path + new_name + "." + oldnames  # New image address splicing
        copyfile(oldname, new_names)
        file_status = os.path.exists(new_names)
        if file_status == True:
            print("https://cdn.jsdelivr.net/gh/zygds/api-temp@master/" + new_name + "." + oldnames)  # TODO
        else:
            print("Image upload failed")


def upload_img():  # image upload
    print(os.system('D: && cd D:\\Github\\api-temp\\ && git add . && git commit -m "update" && git push'))  # TODO


size_text()
upload_img()

posted @ 2020-11-02 20:25  佛系云  阅读(123)  评论(0编辑  收藏  举报