美文网首页
利用python脚本实现使用typora时图片自动上传

利用python脚本实现使用typora时图片自动上传

作者: Justin小贾同学 | 来源:发表于2022-03-16 12:07 被阅读0次

    不少人使用PicGo来管理typora中的图片,但是最近我发现一个问题PicGo更新后似乎会丢失图床的配置信息,还得重新配置。
    于是乎我决定动手写一个python脚本来实现图片管理上传功能,思路也比较简单。

    1. python读取用户插入的图片复制到用户配置的目录下面
    2. python调用git命令实现图片上传
    3. 返回图片路径,返回图片路径结果格式参考说明文档
    16_12_f5e19983ca.png
    1. 在typora中配置调用自定义脚本
    16_11_142598cd67.png

    代码如下:

    '''
    Descripttion: 
    version: 
    Author: siebe
    Date: 2021-10-27 14:21:58
    LastEditors: siebe
    LastEditTime: 2021-10-27 14:21:58
    '''
    
    import sys,os,time,platform,hashlib
    from shutil import copyfile
    
    def run(root_path,base_url):
        os.chdir(root_path)
        print(os.getcwd())
        time_str = time.localtime()
        year = time.strftime("%Y", time_str)
        month = time.strftime("%m", time_str)
        
        path = root_path + '/' + year + '/' + month + '/'
        #print(path)
        if os.path.exists(path):
            pass
        else:
            os.makedirs( path, mode=0o777)
        time.sleep(0.1)   
        operating = platform.system()
        img_list = []
        for item in sys.argv[1:]:
            file_path,full_name = os.path.split(item)
            file_name,ext = os.path.splitext(full_name)
            time_str = time.localtime()
            day = time.strftime("%d_%H", time_str)
            file_md5_name = day + "_" + hashlib.md5(file_name.encode(encoding='UTF-8')).hexdigest()[0:10] + ext
            full_path = path + file_md5_name
            img_list.append(file_md5_name)
            #print(full_path)
            copyfile(item, full_path.replace("/", "\\"))
            pass
            if operating == "Linux":
                command = str("cp " + item + " " + full_path).replace("\\\\", "/")
                #print(command)
            elif operating == "Windows":
                command = str("copy " + item + " " + full_path).replace("/", "\\")
                print(command)
            else:
                command = str("copy " + item + " " + full_path).replace("/", "\\")
                #print(command)
            #os.popen(command)
            
        #print(img_list)
        for item in img_list:
            cmd1 = "git add ./" + year + "/" + month + "/" + item
            cmd2 = "git commit -m " + item
            os.system(cmd1)
            os.system(cmd2)
    
        cmd3 = "git push"
        os.system(cmd3)
        print("Upload Success:")
        for item in img_list:
            print(base_url + year + "/" + month + "/" + item)
    
    
    if __name__ == '__main__':
        root_path = "E:/note/album"
        base_url = "https://gitee.com/siebe/album/raw/master/"
        run(root_path,base_url)
    

    相关文章

      网友评论

          本文标题:利用python脚本实现使用typora时图片自动上传

          本文链接:https://www.haomeiwen.com/subject/wskpdrtx.html