折腾:
【未解决】用Python发布印象笔记帖子内容到WordPress网站
期间,对于要发布新帖子到WordPress,好像都是提到的xmlrpc。
此处希望是更加先进方便的REST API。
如果WordPress能支持rest api,那最好
搜rest api
Search Results for “rest api” | WordPress.org
WP REST API Controller – WordPress plugin | WordPress.org
好像只是custom post type?
不是我要的:通过api新增标准的post?
WordPress REST API Authentication – WordPress plugin | WordPress.org
可以用来验证登录
说是rest支持get之外,也支持POST去新建帖子
-》
Developer Resources | Create cool applications that integrate with WordPress.com
解释貌似很详细,不过好像也很复杂。
- POST /sites/$site/posts/new: Create a new post (requires authentication).
 
REST API支持WordPress提供的众多的api中的一种
其他的所有的API详见:
- Dashboard Widgets API
 - Database API
 - HTTP API
 - REST API
 - File Header API
 - Filesystem API
 - Metadata API
 - Options API
 - Plugin API
 - Quicktags API
 - Rewrite API
 - Settings API
 - Shortcode API
 - Theme Modification API
 - Theme Customization API
 - Transients API
 - Widgets API
 - XML-RPC WordPress API (supersedes the legacy Blogger, MovableType, and metaWeblog APIs)
 
关于认证,看来有很多种方式:
JWT Authentication for WP REST API – WordPress plugin | WordPress.org
然后去:
【已解决】尝试WordPress中的REST的api接口返回数据看看效果
核心内容:
- WP_REST_Request
 - WP_REST_Response
 
- Resource Base Route
 - Posts: wp/v2/posts
 - Post Revisions: wp/v2/posts/<id>/revisions
 - Categories: wp/v2/categories
 - Tags: wp/v2/tags
 - Pages: wp/v2/pages
 - Page Revisions: wp/v2/pages/<id>/revisions
 - Comments: wp/v2/comments
 - Taxonomies: wp/v2/taxonomies
 - Media: wp/v2/media
 - Users: wp/v2/users
 - Post Types: wp/v2/types
 - Post Statuses: wp/v2/statuses
 - Settings: wp/v2/settings
 - Themes: wp/v2/themes
 - Search: wp/v2/search
 - Blocks: wp/v2/blocks
 - Block Revisions: wp/v2/blocks/<id>/autosaves/
 - Block Renderer: wp/v2/block-renderer
 
后面主要用:
/wp/v2/posts
去创建帖子
以及处理相关的
标签:/wp/v2/tags
分类:/wp/v2/categories
状态:/wp/v2/statuses
以及上传图片? /wp/v2/media
- GET /wp-json/wp/v2/posts to get a collection of Posts. This is roughly equivalent to using WP_Query.
 - GET /wp-json/wp/v2/posts/123 to get a single Post with ID 123. This is roughly equivalent to using get_post().
 - POST /wp-json/wp/v2/posts to create a new Post. This is roughly equivalent to using wp_insert_post().
 - DELETE /wp-json/wp/v2/posts/123 to delete Post with ID 123. This is roughly equivalent to wp_delete_post().
 
对于单个post:
- The “route” is wp/v2/posts/123 – The route doesn’t include wp-json because wp-json is the base path for the API itself.
 - This route has 3 endpoints:
 - GET triggers a get_item method, returning the post data to the client.
 - PUT triggers an update_item method, taking the data to update, and returning the updated post data.
 - DELETE triggers a delete_item method, returning the now-deleted post data to the client.
 
【已解决】给crifan.org的WordPress网站REST的API添加JWT的token认证
去试试rest的api
才注意到,之前posts的get已经试了是可以的:

那可以直接去尝试:
posts的post去创建帖子了不过先要:
【已解决】研究WordPress的REST的api中创建POST时所需参数和格式
期间需要去:
【已解决】用Python通过WordPress的REST的API上传图片
考虑到现在访问crifan.org的确慢,上传图片的速度太慢,所以还是尽量去加上代理吧
【已解决】给Python的requests加上代理访问WordPress的REST的api
然后再去想办法发布post,不过很明显需要:
【已解决】用Python把印象笔记中的文章内容部分转换成html用于后续上传到WordPress
然后再去从标题转slug:
【已解决】用Python从印象笔记帖子标题生成WordPress中的slug固定链接地址
然后:
【已解决】Python更新印象笔记报错:EDAMUserException errorCode 11 The content of element type en-media must match EMPTY
不过,此处对于slug,还是要去更新到Note中的:
此处slug,属于attributes:
    # 1. Process Note
    oldSourceUrl = noteDetail.attributes.sourceURL
    noteDetail = processNoteSlug(noteDetail)
    newSourceUrl = noteDetail.attributes.sourceURL
    needUpdateAttributes = False
    if newSourceUrl:
        if newSourceUrl != oldSourceUrl:
            needUpdateAttributes = True
    noteContent = noteDetail.content
    logging.debug("noteContent=%s", noteContent)
    soup = BeautifulSoup(noteContent, 'html.parser')
    soup = mergePostTitleAndUrl(soup)
    updatedContent = soup.prettify()
    # updatedContent = str(soup)
    logging.info("updatedContent=%s", updatedContent)
    # 2. Update to Evernote
    updateParamDict = {
        "noteGuid": noteDetail.guid,
        "noteTitle": noteDetail.title,
        "newContent": updatedContent,
    }
    if needUpdateAttributes:
        updateParamDict["newAttributes"] = noteDetail.attributes
    updatedNote = updateNote(**updateParamDict)
    logging.debug("updatedNote=%s", updatedNote)即可更新成功。
再继续看看后续的
postContentToHtml
再去实现,post发布到WordPress的crifan.org中:
【已解决】Python把印象笔记帖子内容通过WordPress的REST的post接口去上传到crifan.org