4 WordPress xml-rpc client library
7 Copyright (C) 2005 Michele Ferretti
9 http://www.blackbirdblog.it
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 XML-RPC supported methods:
42 * http://codex.wordpress.org/XML-RPC_Support
43 * http://www.sixapart.com/movabletype/docs/mtmanual_programmatic.html
44 * http://docs.python.org/lib/module-xmlrpclib.html
47 __author__
= "Michele Ferretti <black.bird@tiscali.it>"
48 __version__
= "$Revision: 1.0 $"
49 __date__
= "$Date: 2005/05/02 $"
50 __copyright__
= "Copyright (c) 2005 Michele Ferretti"
60 class WordPressException(exceptions
.Exception):
61 """Custom exception for WordPress client operations
63 def __init__(self
, obj
):
64 if isinstance(obj
, xmlrpclib
.Fault
):
65 self
.id = obj
.faultCode
66 self
.message
= obj
.faultString
72 return '<%s %d: \'%s\'>' % (self
.__class
__.__name
__, self
.id, self
.message
)
75 """Represents blog item
84 """Represents user item
93 class WordPressCategory
:
94 """Represents category item
99 self
.isPrimary
= False
102 """Represents post item
109 self
.description
= ''
115 self
.allowPings
= False
116 self
.allowComments
= False
119 class WordPressClient
:
120 """Client for connect to WordPress XML-RPC interface
123 def __init__(self
, url
, user
, password
):
126 self
.password
= password
128 self
.categories
= None
129 self
._server
= xmlrpclib
.ServerProxy(self
.url
)
131 def _filterPost(self
, post
):
132 """Transform post struct in WordPressPost instance
134 postObj
= WordPressPost()
135 postObj
.permaLink
= post
['permaLink']
136 postObj
.description
= post
['description']
137 postObj
.title
= post
['title']
138 postObj
.excerpt
= post
['mt_excerpt']
139 postObj
.user
= post
['userid']
140 postObj
.date
= time
.strptime(str(post
['dateCreated']), "%Y%m%dT%H:%M:%S")
141 postObj
.link
= post
['link']
142 postObj
.textMore
= post
['mt_text_more']
143 postObj
.allowComments
= post
['mt_allow_comments'] == 1
144 postObj
.id = int(post
['postid'])
145 postObj
.categories
= post
['categories']
146 postObj
.allowPings
= post
['mt_allow_pings'] == 1
149 def _filterCategory(self
, cat
):
150 """Transform category struct in WordPressCategory instance
152 catObj
= WordPressCategory()
153 catObj
.id = int(cat
['categoryId'])
154 catObj
.name
= cat
['categoryName']
155 if cat
.has_key('isPrimary'):
156 catObj
.isPrimary
= cat
['isPrimary']
159 def selectBlog(self
, blogId
):
162 def supportedMethods(self
):
163 """Get supported methods list
165 return self
._server
.mt
.supportedMethods()
167 def getLastPost(self
):
170 return tuple(self
.getRecentPosts(1))[0]
172 def getRecentPosts(self
, numPosts
=5):
176 posts
= self
._server
.metaWeblog
.getRecentPosts(self
.blogId
, self
.user
,
177 self
.password
, numPosts
)
179 yield self
._filterPost
(post
)
180 except xmlrpclib
.Fault
, fault
:
181 raise WordPressException(fault
)
183 def getPost(self
, postId
):
187 return self
._filterPost
(self
._server
.metaWeblog
.getPost(str(postId
), self
.user
, self
.password
))
188 except xmlrpclib
.Fault
, fault
:
189 raise WordPressException(fault
)
191 def getUserInfo(self
):
195 userinfo
= self
._server
.blogger
.getUserInfo('', self
.user
, self
.password
)
196 userObj
= WordPressUser()
197 userObj
.id = userinfo
['userid']
198 userObj
.firstName
= userinfo
['firstname']
199 userObj
.lastName
= userinfo
['lastname']
200 userObj
.nickname
= userinfo
['nickname']
201 userObj
.email
= userinfo
['email']
203 except xmlrpclib
.Fault
, fault
:
204 raise WordPressException(fault
)
206 def getUsersBlogs(self
):
207 """Get blog's users info
210 blogs
= self
._server
.blogger
.getUsersBlogs('', self
.user
, self
.password
)
212 blogObj
= WordPressBlog()
213 blogObj
.id = blog
['blogid']
214 blogObj
.name
= blog
['blogName']
215 blogObj
.isAdmin
= blog
['isAdmin']
216 blogObj
.url
= blog
['url']
218 except xmlrpclib
.Fault
, fault
:
219 raise WordPressException(fault
)
221 def newPost(self
, post
, publish
):
225 'title' : post
.title
,
226 'description' : post
.description
232 for cat
in post
.categories
:
234 categories
.append({'categoryId' : cat
, 'isPrimary' : 1})
236 categories
.append({'categoryId' : cat
, 'isPrimary' : 0})
240 idNewPost
= int(self
._server
.metaWeblog
.newPost(self
.blogId
, self
.user
, self
.password
, blogContent
, 0))
242 # set categories for new post
243 self
.setPostCategories(idNewPost
, categories
)
245 # publish post if publish set at True
247 self
.publishPost(idNewPost
)
251 def getPostCategories(self
, postId
):
252 """Get post's categories
255 categories
= self
._server
.mt
.getPostCategories(postId
, self
.user
,
257 for cat
in categories
:
258 yield self
._filterCategory
(cat
)
259 except xmlrpclib
.Fault
, fault
:
260 raise WordPressException(fault
)
262 def setPostCategories(self
, postId
, categories
):
263 """Set post's categories
265 self
._server
.mt
.setPostCategories(postId
, self
.user
, self
.password
, categories
)
267 def editPost(self
, postId
, post
, publish
):
271 'title' : post
.title
,
272 'description' : post
.description
,
273 'permaLink' : post
.permaLink
,
274 'mt_allow_pings' : post
.allowPings
,
275 'mt_text_more' : post
.textMore
,
276 'mt_excerpt' : post
.excerpt
280 blogcontent
['dateCreated'] = xmlrpclib
.DateTime(post
.date
)
285 for cat
in post
.categories
:
287 categories
.append({'categoryId' : cat
, 'isPrimary' : 1})
289 categories
.append({'categoryId' : cat
, 'isPrimary' : 0})
292 result
= self
._server
.metaWeblog
.editPost(postId
, self
.user
, self
.password
,
296 raise WordPressException('Post edit failed')
298 # set categories for new post
299 self
.setPostCategories(postId
, categories
)
303 self
.publishPost(postId
)
305 def deletePost(self
, postId
):
309 return self
._server
.blogger
.deletePost('', postId
, self
.user
,
311 except xmlrpclib
.Fault
, fault
:
312 raise WordPressException(fault
)
314 def getCategoryList(self
):
315 """Get blog's categories list
318 if not self
.categories
:
320 categories
= self
._server
.mt
.getCategoryList(self
.blogId
,
321 self
.user
, self
.password
)
322 for cat
in categories
:
323 self
.categories
.append(self
._filterCategory
(cat
))
325 return self
.categories
326 except xmlrpclib
.Fault
, fault
:
327 raise WordPressException(fault
)
329 def getCategoryIdFromName(self
, name
):
330 """Get category id from category name
332 for c
in self
.getCategoryList():
336 def getTrackbackPings(self
, postId
):
337 """Get trackback pings of post
340 return self
._server
.mt
.getTrackbackPings(postId
)
341 except xmlrpclib
.Fault
, fault
:
342 raise WordPressException(fault
)
344 def publishPost(self
, postId
):
348 return (self
._server
.mt
.publishPost(postId
, self
.user
, self
.password
) == 1)
349 except xmlrpclib
.Fault
, fault
:
350 raise WordPressException(fault
)
352 def getPingbacks(self
, postUrl
):
353 """Get pingbacks of post
356 return self
._server
.pingback
.extensions
.getPingbacks(postUrl
)
357 except xmlrpclib
.Fault
, fault
:
358 raise WordPressException(fault
)
360 def newMediaObject(self
, mediaFileName
):
361 """Add new media object (image, movie, etc...)
364 f
= file(mediaFileName
, 'rb')
369 'name' : os
.path
.basename(mediaFileName
),
370 'bits' : xmlrpclib
.Binary(mediaBits
)
373 result
= self
._server
.metaWeblog
.newMediaObject(self
.blogId
,
374 self
.user
, self
.password
, mediaStruct
)
377 except xmlrpclib
.Fault
, fault
:
378 raise WordPressException(fault
)