Skip to content

Instantly share code, notes, and snippets.

@suncle1993
Created December 13, 2017 07:50
Show Gist options
  • Save suncle1993/740e91fdbdab3c2b59a8c5adeac5f175 to your computer and use it in GitHub Desktop.
Save suncle1993/740e91fdbdab3c2b59a8c5adeac5f175 to your computer and use it in GitHub Desktop.
使用python和wordpress_xmlrpc发布wordpress带特征图片的文章
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import re
import datetime
import os
import time
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc.methods import taxonomies
from wordpress_xmlrpc import WordPressTerm
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts
wp = Client('http://example/xmlrpc.php', 'yourusername', 'yourpassword')
filename = './2017-11-20_104436.jpg' #上传的图片文件路径
# prepare metadata
data = {
'name': 'picture.jpg',
'type': 'image/jpeg', # mimetype
}
# read the binary file and let the XMLRPC library encode it into base64
with open(filename, 'rb') as img:
data['bits'] = xmlrpc_client.Binary(img.read())
response = wp.call(media.UploadFile(data))
attachment_id = response['id']
post = WordPressPost()
post.title = '文章标题'
post.content = '文章正文'
post.post_status = 'publish' #文章状态,不写默认是草稿,private表示私密的,draft表示草稿,publish表示发布
post.terms_names = {
'post_tag': ['test', 'firstpost'], #文章所属标签,没有则自动创建
'category': ['Introductions', 'Tests'] #文章所属分类,没有则自动创建
}
post.thumbnail = attachment_id #缩略图的id
post.id = wp.call(posts.NewPost(post))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment