How can I add a thumbnail to a wordpress post using xmlrpclib Python library?

断了今生、忘了曾经 提交于 2019-12-21 23:45:40

问题


I'm trying to develope a Python's script which needs to post content to a wordpress blog, the problem is that I need to set an image as the thumbnail of the post and I don't have any idea of how to do it.

This is an example of a code to post something (without thumbnail) to WP:

import xmlrpclib

user = 'username'
passwd = 'password'
server = xmlrpclib.ServerProxy('http://vizible.wordpress.com/xmlrpc.php')

blog_id = 0
title = 'test title' 
content = 'test content, from python'

blog_content = { 'title' : title, 'description' : content }
categories = [{'categoryId' : 'programming', 'isPrimary' : 1}] 

post_id = int(server.metaWeblog.newPost(blog_id, user, passwd, blog_content,0))
server.mt.setPostCategories(post_id, user, passwd, categories) # not work
server.mt.publishPost(post_id, user, passwd)

Searching on the web I found another library to publish content to wordpress, and I tried this example code, but it didn't work.

¿Do you know another Python's library to interact with Wordpress which accepts thumbnails?

Thank you :)

EDIT:

Ok, now the code uploads an image to my wordpress library, but I doesn't set is as the post thumbnail.

This is the code:

#!/usr/bin/env python

import xmlrpclib
import urllib2 as urllib

user = 'username'
passwd = 'pass'
server = xmlrpclib.ServerProxy('http://miweb.com/xmlrpc.php')
blog_id = 0

fileimg = urllib.urlopen('image_url')
fileimg = xmlrpclib.Binary(fileimg.read())

data = {'name':'mqdefault.jpg', 'type':'image/jpeg', 'bits':fileimg}

upload = server.wp.uploadFile(blog_id, user, passwd, data)

content = {'post_title':'title', 'post_content':'content', 'post_thumbnail':upload['id']}

post_id = server.wp.newPost(blog_id, user, passwd, content)

server.mt.publishPost(post_id, user, passwd)

The problem is that even when content['post_thumbnail'] and upload['id'] are the same number, when I post it on my Wordpress Blog it doesn't show any thumbnail, but this is uploaded in the gallery.

FINAL EDIT: My theme showed thumbnails as meta, so I had to add them to the post as meta info. It's better for me because I don't need to host the thumbnail in my server.

FINAL CODE:

#!/usr/bin/env python

import xmlrpclib

user = 'username'
passwd = 'pass'
server = xmlrpclib.ServerProxy('http://miweb.com/xmlrpc.php')
blog_id = 0

content = {'post_title':'prova1', 'post_content':'prova text', 'post_status':'published', 'custom_fields': [{'value': 'thumbnail url', 'key': 'thumb'}]}

post_id = server.wp.newPost(blog_id, user, passwd, content)

server.mt.publishPost(post_id, user, passwd)

回答1:


You can use the wp.* calls in the xmlrpc interface to access WP specific functionality that does more than the metaweblog ones will.

First you would call server.wp.uploadFile or server.wp.getMediaLibrary to upload the thumbnail or find an existing one and fetch it's id. Then use this id in the content["post_thumbnail"] parameter to server.wp.newPost.

The WP API has docs for all of this.



来源:https://stackoverflow.com/questions/12840797/how-can-i-add-a-thumbnail-to-a-wordpress-post-using-xmlrpclib-python-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!