最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】用Python通过WordPress的REST的API发布文章post

Python crifan 3644浏览 0评论
折腾:
【未解决】用Python发布印象笔记帖子内容到WordPress网站
期间,对于要发布新帖子到WordPress,好像都是提到的xmlrpc。
此处希望是更加先进方便的REST API。
如果WordPress能支持rest api,那最好
搜rest api
Search Results for “rest api” | WordPress.org
https://wordpress.org/plugins/search/rest+api/
WP REST API Controller – WordPress plugin | WordPress.org
https://wordpress.org/plugins/wp-rest-api-controller/
好像只是custom post type?
不是我要的:通过api新增标准的post?
WordPress REST API Authentication – WordPress plugin | WordPress.org
https://wordpress.org/plugins/wp-rest-api-authentication/
可以用来验证登录
WordPress REST API的使用 – 标点符
REST API Handbook | WordPress Developer Resources
说是rest支持get之外,也支持POST去新建帖子
GitHub – WP-API/Basic-Auth: Basic Authentication handler for the JSON API, used for development and debugging purposes
REST API Resources | Developer Resources
-》
Developer Resources | Create cool applications that integrate with WordPress.com
https://developer.wordpress.com/docs/api/1.2/post/sites/%24site/posts/new/
解释貌似很详细,不过好像也很复杂。
Getting Started with the API | Developer Resources
  • POST /sites/$site/posts/new: Create a new post (requires authentication).
WordPress REST API Team · GitHub
GitHub – WP-API/WP-API.github.io
REST API支持WordPress提供的众多的api中的一种
其他的所有的API详见:
WordPress APIs « WordPress Codex
  • 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)
关于认证,看来有很多种方式:
OAuth2 Authentication | Developer Resources
Authentication | REST API Handbook | WordPress Developer Resources
GitHub – WP-API/Basic-Auth: Basic Authentication handler for the JSON API, used for development and debugging purposes
JWT Authentication for WP REST API – WordPress plugin | WordPress.org
https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/
然后去:
【已解决】尝试WordPress中的REST的api接口返回数据看看效果
核心内容:
  • WP_REST_Request
  • WP_REST_Response
Schema | REST API Handbook | WordPress Developer Resources
Using the REST API | REST API Handbook | WordPress Developer Resources
Extending the REST API | REST API Handbook | WordPress Developer Resources
Reference | REST API Handbook | WordPress Developer Resources
  • 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
Glossary | REST API Handbook | WordPress Developer Resources
  • 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.
Global Parameters | REST API Handbook | WordPress Developer Resources
Authentication | REST API Handbook | WordPress Developer Resources
【已解决】给crifan.com的WordPress网站REST的API添加JWT的token认证
去试试rest的api
才注意到,之前posts的get已经试了是可以的:
那可以直接去尝试:
posts的post去创建帖子了不过先要:
【已解决】研究WordPress的REST的api中创建POST时所需参数和格式
期间需要去:
【已解决】用Python通过WordPress的REST的API上传图片
考虑到现在访问crifan.com的确慢,上传图片的速度太慢,所以还是尽量去加上代理吧
【已解决】给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.com中:
【已解决】Python把印象笔记帖子内容通过WordPress的REST的post接口去上传到crifan.com

转载请注明:在路上 » 【已解决】用Python通过WordPress的REST的API发布文章post

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (2)

  1. 请问如何利用REST API上传并设置文章的缩略图呢?
    C4年前 (2021-01-23)回复
    • 加 QQ技术群 群号:104028266,细聊
      crifan4年前 (2021-01-27)回复
87 queries in 0.194 seconds, using 22.20MB memory