| 1 |
""" |
| 2 |
wordpresslib.py |
| 3 |
|
| 4 |
WordPress xml-rpc client library |
| 5 |
use MovableType API |
| 6 |
|
| 7 |
Copyright (C) 2005 Michele Ferretti |
| 8 |
[email protected] |
| 9 |
http://www.blackbirdblog.it |
| 10 |
|
| 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. |
| 15 |
|
| 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. |
| 20 |
|
| 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. |
| 24 |
|
| 25 |
XML-RPC supported methods: |
| 26 |
* getUsersBlogs |
| 27 |
* getUserInfo |
| 28 |
* getPost |
| 29 |
* getRecentPosts |
| 30 |
* newPost |
| 31 |
* editPost |
| 32 |
* deletePost |
| 33 |
* newMediaObject |
| 34 |
* getCategoryList |
| 35 |
* getPostCategories |
| 36 |
* setPostCategories |
| 37 |
* getTrackbackPings |
| 38 |
* publishPost |
| 39 |
* getPingbacks |
| 40 |
|
| 41 |
References: |
| 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 |
| 45 |
""" |
| 46 |
|
| 47 |
__author__ = "Michele Ferretti <[email protected]>" |
| 48 |
__version__ = "$Revision: 1.0 $" |
| 49 |
__date__ = "$Date: 2005/05/02 $" |
| 50 |
__copyright__ = "Copyright © 2005 Michele Ferretti" |
| 51 |
__license__ = "LGPL" |
| 52 |
|
| 53 |
import exceptions |
| 54 |
import re |
| 55 |
import os |
| 56 |
import xmlrpclib |
| 57 |
import datetime |
| 58 |
import time |
| 59 |
import logging |
| 60 |
|
| 61 |
class WordPressException(exceptions.Exception): |
| 62 |
"""Custom exception for WordPress client operations |
| 63 |
""" |
| 64 |
def __init__(self, obj): |
| 65 |
if isinstance(obj, xmlrpclib.Fault): |
| 66 |
self.id = obj.faultCode |
| 67 |
self.message = obj.faultString |
| 68 |
else: |
| 69 |
self.id = 0 |
| 70 |
self.message = obj |
| 71 |
|
| 72 |
def __str__(self): |
| 73 |
return '<%s %d: \'%s\'>' % (self.__class__.__name__, self.id, self.message) |
| 74 |
|
| 75 |
class WordPressBlog: |
| 76 |
"""Represents blog item |
| 77 |
""" |
| 78 |
def __init__(self): |
| 79 |
self.id = '' |
| 80 |
self.name = '' |
| 81 |
self.url = '' |
| 82 |
self.isAdmin = False |
| 83 |
|
| 84 |
class WordPressUser: |
| 85 |
"""Represents user item |
| 86 |
""" |
| 87 |
def __init__(self): |
| 88 |
self.id = '' |
| 89 |
self.firstName = '' |
| 90 |
self.lastName = '' |
| 91 |
self.nickname = '' |
| 92 |
self.email = '' |
| 93 |
|
| 94 |
class WordPressCategory: |
| 95 |
"""Represents category item |
| 96 |
""" |
| 97 |
def __init__(self): |
| 98 |
self.id = 0 |
| 99 |
self.name = '' |
| 100 |
self.isPrimary = False |
| 101 |
|
| 102 |
class WordPressPost: |
| 103 |
"""Represents post item |
| 104 |
""" |
| 105 |
def __init__(self): |
| 106 |
self.id = 0 |
| 107 |
self.title = '' |
| 108 |
self.date = None |
| 109 |
self.permaLink = '' |
| 110 |
self.description = '' |
| 111 |
self.textMore = '' |
| 112 |
self.excerpt = '' |
| 113 |
self.link = '' |
| 114 |
self.categories = [] |
| 115 |
self.user = '' |
| 116 |
self.allowPings = False |
| 117 |
self.allowComments = False |
| 118 |
|
| 119 |
|
| 120 |
class WordPressClient: |
| 121 |
"""Client for connect to WordPress XML-RPC interface |
| 122 |
""" |
| 123 |
|
| 124 |
def __init__(self, url, user, password): |
| 125 |
self.url = url |
| 126 |
self.user = user |
| 127 |
self.password = password |
| 128 |
self.blogId = 0 |
| 129 |
self.categories = None |
| 130 |
self._server = xmlrpclib.ServerProxy(self.url) |
| 131 |
|
| 132 |
def _filterPost(self, post): |
| 133 |
"""Transform post struct in WordPressPost instance |
| 134 |
""" |
| 135 |
postObj = WordPressPost() |
| 136 |
postObj.permaLink = post['permaLink'] |
| 137 |
postObj.description = post['description'] |
| 138 |
postObj.title = post['title'] |
| 139 |
postObj.excerpt = post['mt_excerpt'] |
| 140 |
postObj.user = post['userid'] |
| 141 |
postObj.date = time.strptime(str(post['dateCreated']), "%Y%m%dT%H:%M:%S") |
| 142 |
postObj.link = post['link'] |
| 143 |
postObj.textMore = post['mt_text_more'] |
| 144 |
postObj.allowComments = post['mt_allow_comments'] == 1 |
| 145 |
postObj.id = int(post['postid']) |
| 146 |
postObj.categories = post['categories'] |
| 147 |
postObj.allowPings = post['mt_allow_pings'] == 1 |
| 148 |
return postObj |
| 149 |
|
| 150 |
def _filterCategory(self, cat): |
| 151 |
"""Transform category struct in WordPressCategory instance |
| 152 |
""" |
| 153 |
catObj = WordPressCategory() |
| 154 |
catObj.id = int(cat['categoryId']) |
| 155 |
catObj.name = cat['categoryName'] |
| 156 |
if 'isPrimary' in cat: |
| 157 |
catObj.isPrimary = cat['isPrimary'] |
| 158 |
return catObj |
| 159 |
|
| 160 |
def selectBlog(self, blogId): |
| 161 |
self.blogId = blogId |
| 162 |
|
| 163 |
def supportedMethods(self): |
| 164 |
"""Get supported methods list |
| 165 |
""" |
| 166 |
return self._server.mt.supportedMethods() |
| 167 |
|
| 168 |
def getLastPost(self): |
| 169 |
"""Get last post |
| 170 |
""" |
| 171 |
return tuple(self.getRecentPosts(1))[0] |
| 172 |
|
| 173 |
def getRecentPosts(self, numPosts=5): |
| 174 |
"""Get recent posts |
| 175 |
""" |
| 176 |
try: |
| 177 |
posts = self._server.metaWeblog.getRecentPosts(self.blogId, self.user, |
| 178 |
self.password, numPosts) |
| 179 |
for post in posts: |
| 180 |
yield self._filterPost(post) |
| 181 |
except xmlrpclib.Fault as fault: |
| 182 |
raise WordPressException(fault) |
| 183 |
|
| 184 |
def getPost(self, postId): |
| 185 |
"""Get post item |
| 186 |
""" |
| 187 |
try: |
| 188 |
return self._filterPost(self._server.metaWeblog.getPost(str(postId), self.user, self.password)) |
| 189 |
except xmlrpclib.Fault as fault: |
| 190 |
raise WordPressException(fault) |
| 191 |
|
| 192 |
def getUserInfo(self): |
| 193 |
"""Get user info |
| 194 |
""" |
| 195 |
try: |
| 196 |
userinfo = self._server.blogger.getUserInfo('', self.user, self.password) |
| 197 |
userObj = WordPressUser() |
| 198 |
userObj.id = userinfo['userid'] |
| 199 |
userObj.firstName = userinfo['firstname'] |
| 200 |
userObj.lastName = userinfo['lastname'] |
| 201 |
userObj.nickname = userinfo['nickname'] |
| 202 |
userObj.email = userinfo['email'] |
| 203 |
return userObj |
| 204 |
except xmlrpclib.Fault as fault: |
| 205 |
raise WordPressException(fault) |
| 206 |
|
| 207 |
def getUsersBlogs(self): |
| 208 |
"""Get blog's users info |
| 209 |
""" |
| 210 |
try: |
| 211 |
blogs = self._server.blogger.getUsersBlogs('', self.user, self.password) |
| 212 |
for blog in blogs: |
| 213 |
blogObj = WordPressBlog() |
| 214 |
blogObj.id = blog['blogid'] |
| 215 |
blogObj.name = blog['blogName'] |
| 216 |
blogObj.isAdmin = blog['isAdmin'] |
| 217 |
blogObj.url = blog['url'] |
| 218 |
yield blogObj |
| 219 |
except xmlrpclib.Fault as fault: |
| 220 |
raise WordPressException(fault) |
| 221 |
|
| 222 |
def newPost(self, post, publish): |
| 223 |
"""Insert new post |
| 224 |
""" |
| 225 |
blogContent = { |
| 226 |
'title' : post.title, |
| 227 |
'description' : "<![CDATA["+post.description+"]]>" |
| 228 |
} |
| 229 |
|
| 230 |
# add categories |
| 231 |
i = 0 |
| 232 |
categories = [] |
| 233 |
available_categories = self._server.mt.getCategoryList("238",self.user,self.password) |
| 234 |
for cat in post.categories: |
| 235 |
for i in available_categories: |
| 236 |
if i["categoryName"]==cat: |
| 237 |
categories.append(i) |
| 238 |
|
| 239 |
# insert new post |
| 240 |
idNewPost = int(self._server.metaWeblog.newPost(self.blogId, self.user, self.password, blogContent, 0)) |
| 241 |
logging.debug("idNewPost: %d" % idNewPost) |
| 242 |
logging.debug(blogContent) |
| 243 |
|
| 244 |
# set categories for new post |
| 245 |
self.setPostCategories(idNewPost, categories) |
| 246 |
|
| 247 |
# publish post if publish set at True |
| 248 |
if publish: |
| 249 |
self.publishPost(idNewPost) |
| 250 |
|
| 251 |
return idNewPost |
| 252 |
|
| 253 |
def getPostCategories(self, postId): |
| 254 |
"""Get post's categories |
| 255 |
""" |
| 256 |
try: |
| 257 |
categories = self._server.mt.getPostCategories(postId, self.user, |
| 258 |
self.password) |
| 259 |
for cat in categories: |
| 260 |
yield self._filterCategory(cat) |
| 261 |
except xmlrpclib.Fault as fault: |
| 262 |
raise WordPressException(fault) |
| 263 |
|
| 264 |
def setPostCategories(self, postId, categories): |
| 265 |
"""Set post's categories |
| 266 |
""" |
| 267 |
self._server.mt.setPostCategories(postId, self.user, self.password, categories) |
| 268 |
|
| 269 |
def editPost(self, postId, post, publish): |
| 270 |
"""Edit post |
| 271 |
""" |
| 272 |
blogcontent = { |
| 273 |
'title' : post.title, |
| 274 |
'description' : post.description, |
| 275 |
'permaLink' : post.permaLink, |
| 276 |
'mt_allow_pings' : post.allowPings, |
| 277 |
'mt_text_more' : post.textMore, |
| 278 |
'mt_excerpt' : post.excerpt |
| 279 |
} |
| 280 |
|
| 281 |
if post.date: |
| 282 |
blogcontent['dateCreated'] = xmlrpclib.DateTime(post.date) |
| 283 |
|
| 284 |
# add categories |
| 285 |
i = 0 |
| 286 |
categories = [] |
| 287 |
for cat in post.categories: |
| 288 |
if i == 0: |
| 289 |
categories.append({'categoryId' : cat, 'isPrimary' : 1}) |
| 290 |
else: |
| 291 |
categories.append({'categoryId' : cat, 'isPrimary' : 0}) |
| 292 |
i += 1 |
| 293 |
|
| 294 |
result = self._server.metaWeblog.editPost(postId, self.user, self.password, |
| 295 |
blogcontent, 0) |
| 296 |
|
| 297 |
if result == 0: |
| 298 |
raise WordPressException('Post edit failed') |
| 299 |
|
| 300 |
# set categories for new post |
| 301 |
self.setPostCategories(postId, categories) |
| 302 |
|
| 303 |
# publish new post |
| 304 |
if publish: |
| 305 |
self.publishPost(postId) |
| 306 |
|
| 307 |
def deletePost(self, postId): |
| 308 |
"""Delete post |
| 309 |
""" |
| 310 |
try: |
| 311 |
return self._server.blogger.deletePost('', postId, self.user, |
| 312 |
self.password) |
| 313 |
except xmlrpclib.Fault as fault: |
| 314 |
raise WordPressException(fault) |
| 315 |
|
| 316 |
def getCategoryList(self): |
| 317 |
"""Get blog's categories list |
| 318 |
""" |
| 319 |
try: |
| 320 |
if not self.categories: |
| 321 |
self.categories = [] |
| 322 |
categories = self._server.mt.getCategoryList(self.blogId, |
| 323 |
self.user, self.password) |
| 324 |
for cat in categories: |
| 325 |
self.categories.append(self._filterCategory(cat)) |
| 326 |
|
| 327 |
return self.categories |
| 328 |
except xmlrpclib.Fault as fault: |
| 329 |
raise WordPressException(fault) |
| 330 |
|
| 331 |
def getCategoryIdFromName(self, name): |
| 332 |
"""Get category id from category name |
| 333 |
""" |
| 334 |
for c in self.getCategoryList(): |
| 335 |
if c.name == name: |
| 336 |
return c.id |
| 337 |
|
| 338 |
def getTrackbackPings(self, postId): |
| 339 |
"""Get trackback pings of post |
| 340 |
""" |
| 341 |
try: |
| 342 |
return self._server.mt.getTrackbackPings(postId) |
| 343 |
except xmlrpclib.Fault as fault: |
| 344 |
raise WordPressException(fault) |
| 345 |
|
| 346 |
def publishPost(self, postId): |
| 347 |
"""Publish post |
| 348 |
""" |
| 349 |
try: |
| 350 |
return (self._server.mt.publishPost(postId, self.user, self.password) == 1) |
| 351 |
except xmlrpclib.Fault as fault: |
| 352 |
raise WordPressException(fault) |
| 353 |
|
| 354 |
def getPingbacks(self, postUrl): |
| 355 |
"""Get pingbacks of post |
| 356 |
""" |
| 357 |
try: |
| 358 |
return self._server.pingback.extensions.getPingbacks(postUrl) |
| 359 |
except xmlrpclib.Fault as fault: |
| 360 |
raise WordPressException(fault) |
| 361 |
|
| 362 |
def newMediaObject(self, mediaFileName): |
| 363 |
"""Add new media object (image, movie, etc...) |
| 364 |
""" |
| 365 |
try: |
| 366 |
f = file(mediaFileName, 'rb') |
| 367 |
mediaBits = f.read() |
| 368 |
f.close() |
| 369 |
|
| 370 |
mediaStruct = { |
| 371 |
'name' : os.path.basename(mediaFileName), |
| 372 |
'bits' : xmlrpclib.Binary(mediaBits) |
| 373 |
} |
| 374 |
|
| 375 |
result = self._server.metaWeblog.newMediaObject(self.blogId, |
| 376 |
self.user, self.password, mediaStruct) |
| 377 |
return result['url'] |
| 378 |
|
| 379 |
except xmlrpclib.Fault as fault: |
| 380 |
raise WordPressException(fault) |
| 381 |
|