Typora发布文章到博客园


目录

Typora发布文章到博客园

1.导出脚本

image-20220608104915796

cnblogs.json(配置文件)

url 是MetaWeblog访问地址: https://rpc.cnblogs.com/metaweblog/wh984763176

注意:现在用户用户命名已经不能访问,需要创建个人令牌,令牌作为密码

{
  "url": "https://rpc.cnblogs.com/metaweblog/wh984763176",
  "username": "984763176@qq.com",
  "password": "8FA8E7601A47A1F0273C4DB79B520C08F662A12754C2E376F741B9E"
}

image-20220626022220678

cnblogsImage.py(上传图片)

import xmlrpc.client
import ssl
import os
import json
import sys
ssl._create_default_https_context = ssl._create_unverified_context

rootPath = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(rootPath,"cnblogs.json"),"rb") as f:
    config = json.loads(f.read())


def uploadImage(images):
    for image in images:
        with open(image,"rb") as f:
            imageData = f.read()

        baseName = os.path.basename(image)
        suffix = baseName.split(".")[-1]
        file = dict(
            bits = imageData,
            name = baseName,
            type = f"image/{suffix}"
        )
        proxy = xmlrpc.client.ServerProxy(config["url"])
        s = proxy.metaWeblog.newMediaObject('', config['username'], config['password'],file)
        print(s["url"])

if __name__ == '__main__':
    uploadImage(sys.argv[1:])

cnblogsArticle.py(上传文章)

import xmlrpc.client
import ssl
import os
import json
import sys
ssl._create_default_https_context = ssl._create_unverified_context

rootPath = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(rootPath,"cnblogs.json"),"rb") as f:
    config = json.loads(f.read())

# 一个文件
def uploadArticle(article):
    # for article in articles:
        with open(article,"r",encoding="utf8") as f:
            data = f.read()

        title = os.path.basename(article)[:-3]
        post = dict(
            dateCreated = xmlrpc.client.DateTime(),
            description = data,
            title = title,
            categories = ['[Markdown]'],
        )
        proxy = xmlrpc.client.ServerProxy(config["url"])
        s = proxy.metaWeblog.newPost('', config['username'], config['password'],post,True)
        # 输出文章连接
        userName = config["url"].split("/")[-1]
        print(f"https://www.cnblogs.com/{userName}/p/{s}.html")

# 遍历上传
def uploadArticles(articles):
    for article in articles:
        with open(article,"r",encoding="utf8") as f:
            data = f.read()

        title = os.path.basename(article)[:-3]
        post = dict(
            dateCreated = xmlrpc.client.DateTime(),
            description = data,
            title = title,
            categories = ['[Markdown]'],
        )
        proxy = xmlrpc.client.ServerProxy(config["url"])
        s = proxy.metaWeblog.newPost('', config['username'], config['password'],post,True)
        # 输出文章连接
        userName = config["url"].split("/")[-1]
        print(f"https://www.cnblogs.com/{userName}/p/{s}.html")

# 参考文章:https://www.cnblogs.com/Hellowshuo/p/15646255.html
# /Users/didi/mine/software/idea/code/python_study/learn_py/typora2blog/cnblogsArticle.py ${currentPath}
if __name__ == '__main__':
    # uploadArticlePath = "/Users/didi/mine/工作/个人笔记/02个人笔记/99-学习/spark大佬总结.md"
    uploadArticlePath = sys.argv[1:] #参数是个数组
    print(uploadArticlePath)
    # 遍历上传
    uploadArticles(uploadArticlePath)

2.Topora、博客配置

1.博客配置

打开博客园个人博客->设置->其他设置->☑️允许 MetaWeblog 博客客户端访问

2.发布文章

打开Typora->偏好设置->导出->点击➕号自定义发布接口 ->发布至博客园->配置执行命令

执行命令

/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 /Users/didi/mine/software/idea/code/python_study/learn_py/typora2blog/cnblogsArticle.py ${currentPath}

image-20220608103214855

3.发布图片

打开Typora->偏好设置->图像

配置执行命令 ->验证下

/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 /Users/didi/mine/software/idea/code/python_study/learn_py/typora2blog/cnblogsImage.py

本地图片路径配置

image-20220608104434102

上传图片配置

image-20220608103411275

3.使用

1.上传本地图片

1)原来:本地文章的图片是本地的相对路径

image-20220608104508675

2)点击上传图片

image-20220608104538605

3)图片上传成功后,路径变为图床路径

image-20220608104625230

2.发布文章

1.选择文章->文件->导出->发布至博客园

image-20220608104752993

发布成功

image-20220607205435995

4.FAQ

文件名不规范

本地文件名如Hive学习(12)【参数调优】.md会找不到,改为Hive-参数调优.md就可以了,不知道为啥

参考:typora实现多平台发布文章

https://www.52pojie.cn/thread-1556163-1-1.html

原创文章,作者:wure,如若转载,请注明出处:https://blog.ytso.com/270387.html

(0)
上一篇 2022年6月26日
下一篇 2022年6月26日

相关推荐

发表回复

登录后才能评论