使用metaweblog API實現通用博客發佈 之 API測試

使用metaweblog API實現通用博客發佈 之 API測試

使用博客比較少,一則是文筆有限,怕寫出的東西狗屁不通,有礙觀瞻, 二則是懶,很討厭要登陸到網站上寫東西,也沒有那麼多時間(藉口)。我的最喜歡用於記錄的工具是Zim https://zim-wiki.org/ ,記錄東西超級方便,惋惜只支持PC版本, 記錄的東西能夠處處爲MarkDown 格式,很是方便(你如今看到的這篇就是用Zim寫的)。html

無心間看到Vs Code上有博客園的插件,做爲程序員,順手google/百度了一下,原來通用博客都支持使用metaweblog API來訪問,還支持直接發佈markdown 格式,簡直不要太好。 找了找2年前註冊的博客源帳號,用來測試一下。python

發揮典型中國程序員的拿來主義精神,通過goolgle/百度一番搜索,參考如下文檔進行API測試,在此表示感謝!!
http://www.javashuo.com/article/p-cwrmfhpa-ch.html
https://github.com/1024th/cnblogs_githookgit

1 在哪裏找API說明

在博客設置最的最末端,有MetaWeblog 的訪問地址連接
點擊進入頁面,有metaweblog API 的詳細說明
具體內容不贅述了。程序員

2 測試API

使用python3 進行API測試,直接上代碼:
```python
#encoding = utf-8
#!/bin/sh python3github

import xmlrpc.client as xmlrpclib
import json

'''
配置字典:
type | description(example)
str  | metaWeblog url, 博客設置中有('https://rpc.cnblogs.com/metaweblog/1024th')
str  | appkey, Blog地址名('1024th')
str  | blogid, 這個無需手動輸入,經過getUsersBlogs獲得
str  | usr, 登陸用戶名
str  | passwd, 登陸密碼
str  | rootpath, 博文存放根路徑(添加git管理)
'''

'''
POST:
dateTime    dateCreated - Required when posting.
string  description - Required when posting.
string  title - Required when posting.
array of string categories (optional)
struct Enclosure    enclosure (optional)
string  link (optional)
string  permalink (optional)
any postid (optional)
struct Source   source (optional)
string  userid (optional)
any mt_allow_comments (optional)
any mt_allow_pings (optional)
any mt_convert_breaks (optional)
string  mt_text_more (optional)
string  mt_excerpt (optional)
string  mt_keywords (optional)
string  wp_slug (optional)
'''

class MetablogClient():
  def __init__(self, configpath):
    '''
    @configpath: 指定配置文件路徑
    '''
    self._configpath = configpath
    self._config = None
    self._server = None
    self._mwb = None
  
  def createConfig(self):
    '''
    建立配置
    '''
    while True:
        cfg = {}
        for item in [("url", "metaWeblog url, 博客設置中有\
            ('https://rpc.cnblogs.com/metaweblog/blogaddress')"),
                     ("appkey", "Blog地址名('blogaddress')"),
                     ("usr", "登陸用戶名"),
                     ("passwd", "登陸密碼"),
                     ("rootpath", "博文本地存儲根路徑")]:
            cfg[item[0]] = input("輸入"+item[1])
        try:
            server = xmlrpclib.ServerProxy(cfg["url"])
            userInfo = server.blogger.getUsersBlogs(
                cfg["appkey"], cfg["usr"], cfg["passwd"])
            print(userInfo[0])
            # {'blogid': 'xxx', 'url': 'xxx', 'blogName': 'xxx'}
            cfg["blogid"] = userInfo[0]["blogid"]
            break
        except:
            print("發生錯誤!")
    with open(self._configpath, "w", encoding="utf-8") as f:
        json.dump(cfg, f, indent=4, ensure_ascii=False)
    
  def existConfig(self):
    '''
    返回配置是否存在
    '''
    try:
        with open(self._configpath, "r", encoding="utf-8") as f:
            try:
                cfg = json.load(f)
                if cfg == {}:
                    return False
                else:
                    return True
            except json.decoder.JSONDecodeError:  # 文件爲空
                return False
    except:
        with open(self._configpath, "w", encoding="utf-8") as f:
            json.dump({}, f)
            return False

  def readConfig(self):
    '''
    讀取配置
    '''
    if not self.existConfig():
      self.createConfig()

    with open(self._configpath, "r", encoding="utf-8") as f:
      self._config = json.load(f)
      self._server = xmlrpclib.ServerProxy(self._config["url"])
      self._mwb = self._server.metaWeblog

  def getUsersBlogs(self):
    '''
    獲取博客信息
    @return: {
      string  blogid
      string    url
      string    blogName
    }
    '''
    userInfo = self._server.blogger.getUsersBlogs(self._config["appkey"], self._config["usr"], self._config["passwd"])
    return userInfo

  def getRecentPosts(self, num):
    '''
    讀取最近的博文信息
    '''
    return self._mwb.getRecentPosts(self._config["blogid"], self._config["usr"], self._config["passwd"], num)

  def newPost(self, post, publish):
    '''
    發佈新博文
    @post: 發佈內容
    @publish: 是否公開
    '''
    while True:
      try:
          postid = self._mwb.newPost(self._config['blogid'], self._config['usr'], self._config['passwd'], post, publish)
          break
      except:
          time.sleep(5)
    return postid

  def editPost(self, postid, post, publish):
    '''
    更新已存在的博文
    @postid: 已存在博文ID
    @post: 發佈內容
    @publish: 是否公開發布
    '''
    self._mwb.editPost(postid, self._config['usr'], self._config['passwd'], post, publish)

  def deletePost(self, postid, publish):
    '''
    刪除博文
    '''
    self._mwb.deletePost(self._config['appkey'], postid, self._config['usr'], self._config['passwd'], post, publish)

  def getCategories(self):
    '''
    獲取博文分類
    '''
    return self._mwb.getCategories(self._config['blogid'], self._config['usr'], self._config['passwd'])

  def getPost(self, postid):
    '''
    讀取博文信息
    @postid: 博文ID
    @return: POST
    '''
    return self._mwb.getPost(postid, self._config['usr'], self._config['passwd'])

  def newMediaObject(self, file):
    '''
    資源文件(圖片,音頻,視頻...)上傳
    @file: {
      base64    bits
      string    name
      string    type
    }
    @return: URL
    '''
    return self._mwb.newMediaObject(self._config['blogid'], self._config['usr'], self._config['passwd'], file)
  
  def newCategory(self, categoray):
    '''
    新建分類
    @categoray: {
      string    name
      string    slug (optional)
      integer   parent_id
      string    description (optional)
    }
    @return : categorayid
    '''
    return self._server.wp.newCategory(self._config['blogid'], self._config['usr'], self._config['passwd'], categoray)
```

以上是對API的簡單封裝,萬事具有,開始測試web

2.1 獲取分類

import core.metablogclient as blogclient
    
    client = blogclient.MetablogClient('blog_config.json')
    client.readConfig()
    catLst = client.getCategories()
    print(catLst)
[{'description': '[發佈至博客園首頁]', 'htmlUrl': '', 'rssUrl': '', 'title': '[發佈至博客園首頁]', 'categoryid': '0'}, 
    {'description': '[Markdown]', 'htmlUrl': '', 'rssUrl': '', 'title': '[Markdown]', 'categoryid': '-5'}...]

獲取了全部的分類信息,其中我在網站上自建了一個隨筆分類,也能夠獲取到json

2.2 新建分類

import core.metablogclient as blogclient
    
    client = blogclient.MetablogClient('blog_config.json')
    client.readConfig()
    catid = client.newCategory({
    "name": "[隨筆分類]測試分類",
    "slug": "",
    "parent_id": 0,
    "description": "測試創建一個隨筆子分類"
    })
    print("新建分類:", catid)
新建分類: 1536823

可是在博客園網站上沒法看到這個分類,使用獲取分類再次測試,也沒法獲取到該分類,使用該分類發佈博客,也是無
效的,因此我想__根據年月自動分類__的想法就泡湯啦api

2.3 拉取現有博文

import core.metablogclient as blogclient
    
    client = blogclient.MetablogClient('blog_config.json')
    client.readConfig()
    posts = client.getRecentPosts(9999)
    print(posts)
[{'dateCreated': <DateTime '20190829T11:21:00' at 0x2a80990>, 'description': '<p>測試</p>', 'title': '測試', 'enclosure': {'length': 0}, 
    'link': 'https://www.cnblogs.com/robert-9/p/11428668.html', 'permalink': 'https://www.cnblogs.com/robert-9/p/11428668.html', 
    'postid': '11428668', 'source': {}, 'userid': '-2'}]

正確拉取現有博文,經過API文檔,發現沒法獲取博文是否處於發佈狀態,這是一個遺憾服務器

2.4 發佈博文

import core.metablogclient as blogclient
    import datetime
    
    client = blogclient.MetablogClient('blog_config.json')
    client.readConfig()
    postid = client.newPost({
      "time": datetime.datetime.now(),
      "title": "metaweblog API隨筆發佈",
      "description": "##metaweblog API隨筆發佈\n測試\n",
      "categories": ["[Markdown]"],
      "mt_keywords": "metaweblog;python"
    }, False)
    print('發佈隨筆:', postid)

測試發佈成功,並能在網站上看到該隨筆, 若是想發佈爲文章,日誌或新聞,加入必要的分類便可。markdown

2.5 上傳圖片

import datetime
    import base64
    import core.metablogclient as blogclient
    
    client = blogclient.MetablogClient('blog_config.json')
    client.readConfig()
    with open('abc.png', 'rb') as f:
      bs64_str = base64.b64encode(f.read())
      url = client.newMediaObject({
          "bits": bs64_str,
          "name": "abc.png",
          "type": "image/png"
      })
      print(url)
{'url': 'https://img2018.cnblogs.com/blog/1211514/201908/1211514-20190829114435333-814710358.png'}

測試成功, 這樣就能夠在上傳Markdown 格式以前,自動將本地的圖片上傳到服務器上了。

相關文章
相關標籤/搜索