美文网首页
python - 为Typora搭建图床

python - 为Typora搭建图床

作者: 温柔vs先生 | 来源:发表于2020-12-16 10:28 被阅读0次
import getopt
import sys
import os
os.environ["GIT_PYTHON_REFRESH"] = "quiet"
import shutil
import git
import pypinyin
#!/usr/bin/python
# -*- coding: UTF-8 -*-
local_repo="F:/iSource/images/MarkdownPic"

''' 将本地复制的图片添加到本地仓库对应的目录中
'''
def pinyin(word):
    s=''
    word= word.replace(" ","");
    for i in pypinyin.pinyin(word,style=pypinyin.NORMAL):
        s+=''.join(i)
    return s;

def addPic(argv):
    try:
        opts, other_args = getopt.getopt(argv, "", ["filename=", "filepath="])
    except getopt.GetoptError:
        sys.exit(2)
 
    for opt, arg in opts:
        if opt == '--filename':
            filename = pinyin(arg)
        elif opt == '--filepath':
            filepath = pinyin(arg)
 
    try:
        # 列出md文件的每一级路径,去除最后一个,及md文件本身
        dirs = filepath.split('\\')[:-1]
        # 去除盘符后面的冒号
        dirs[0] = dirs[0][:-1]
        # 以md文件名作为最后一级目录名
        dirs.append(filename)
        # 切换工作目录
        os.chdir(local_repo)
 
        path = local_repo
        relative_path = ''
        # 按照md文件路径来创建对应的图片存储目录
        for one_dir in dirs:
            if not os.path.exists(one_dir):
                os.mkdir(one_dir)
            path = os.path.join(path, one_dir)
            os.chdir(path)
            # 图片在本地仓库根路径中的相对路径(目录)
            relative_path = os.path.join(relative_path, one_dir)
 
        # 图片在本地仓库根路径中的相对路径集合(文件)
        pics_relative_path = []
        # 复制图片到对应的仓库目录
        for pic in other_args:
            picName = pinyin(os.path.basename(pic))
            picPath = pinyin(os.path.join(path, picName))
            # 复制图片用绝对路径,捕获异常防止新旧图片是同一个
            try:
                shutil.copyfile(pic, picPath)
            except shutil.SameFileError:
                pass
            # 相对路径集合用于之后的 git add
            pics_relative_path.append(os.path.join(relative_path, picName))
 
    except Exception:
        sys.exit(2)
 
    # 返回
    # filepath —— md 文件路径,用于提交时的描述
    # pics_relative_path —— 图片在本地仓库根路径中的相对路径,用于添加图片
    return filepath, pics_relative_path
        
def upload(argv):
    filepath, pics_relative_path = addPic(argv)
 
    # git 指令
    repo = git.Repo(local_repo)
    repo.index.add(pics_relative_path)
    repo.index.commit("pic from {}".format(filepath))
    repo.remote().push()
    
    # 图片的远程仓库地址前缀
    url_base='http://rnd-isourceb.huawei.com/wWX934853/MarkdownPic/blob/master/'
 
    # 按照 Typora 上传图片的自定义命令输出
    print('Upload Success:')
    # 注意url中一定都是 / ,不能有反斜杠
    for pic_path in pics_relative_path:
        pathList=pic_path.split('\\')
        print(
            url_base + '/'.join(
                pathList
            )
        )
 
if __name__ == '__main__':
    argv = sys.argv[1:]
    upload(argv)


采坑:
原因有两个,脚本里面需要配置os.environ["GIT_PYTHON_REFRESH"] = "quiet",原因在这个里面找到 https://blog.csdn.net/bt517840374/article/details/108528156,第二个是我用的git地址是htts格式的,导致每次都要输入用户密码,而python脚本中没有这样的配置,网上找到这样的解决方法https://blog.csdn.net/yychuyu/article/details/80186783,采用ssh即可。

相关文章

网友评论

      本文标题:python - 为Typora搭建图床

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