如何使用 xmlrpclib Python 库向 wordpress 帖子添加缩略图? [英] How can I add a thumbnail to a wordpress post using xmlrpclib Python library?

查看:23
本文介绍了如何使用 xmlrpclib Python 库向 wordpress 帖子添加缩略图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个需要将内容发布到 wordpress 博客的 Python 脚本,问题是我需要将图像设置为帖子的缩略图,但我不知道该怎么做

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.

这是将某些内容(没有缩略图)发布到 WP 的代码示例:

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)

在网上搜索我发现另一个库可以将内容发布到 wordpress,我尝试了 这个示例代码,但它不起作用.

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

¿你知道另一个 Python 库可以与接受缩略图的 Wordpress 交互吗?

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

谢谢:)

编辑:

好的,现在代码将图片上传到我的 wordpress 库,但我没有设置为帖子缩略图.

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

这是代码:

#!/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)

问题是,即使 content['post_thumbnail'] 和 upload['id'] 是相同的数字,当我在我的 Wordpress 博客上发布它时,它也不会显示任何缩略图,但这是在图库中上传的.

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.

最终代码:

#!/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)

推荐答案

您可以使用 xmlrpc 接口中的 wp.* 调用来访问 WP 特定功能,这些功能比元网络日志功能更强大.

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

首先,您将调用 server.wp.uploadFileserver.wp.getMediaLibrary 上传缩略图或查找现有缩略图并获取其 ID.然后在 content["post_thumbnail"] 参数中使用这个 id 到 server.wp.newPost.

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.

WP API 具有所有这些的文档.

The WP API has docs for all of this.

这篇关于如何使用 xmlrpclib Python 库向 wordpress 帖子添加缩略图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆