利用python-wordpress-xmlrpc远程发布WordPress文章缩略图

利用python-wordpress-xmlrpc远程发布WordPress文章缩略图,可以通过采集后的数据批量发布WordPress文章,对于采集站很实用。

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.compat import xmlrpc_client
from wordpress_xmlrpc.methods import media, posts

client = Client(...)

# set to the path to your file
filename = '/path/to/my/picture.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 = client.call(media.UploadFile(data))
# response == {
# 'id': 6,
# 'file': 'picture.jpg'
# 'url': 'http://www.example.com/wp-content/uploads/2012/04/16/picture.jpg',
# 'type': 'image/jpeg',
# }
attachment_id = response['id']

上传图片文件后,可以将此图片设置为文章缩略图:

post = WordPressPost()
post.title = 'Picture of the Day'
post.content = 'What a lovely picture today!'
post.post_status = 'publish'
post.thumbnail = attachment_id
post.id = client.call(posts.NewPost(post))
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容