Wiki

Clone wiki

python-wordpress / Home

Image Migration Example

I migrated a site to Wordpress using this library, but had forgotten to migrate the images as well. This is a script that pulls down all images in all site content (up to 999 posts anyway) and stores them as local media objects.

A future improvement could be to only pull offsite images (which wasn't an issue for me since none of the images were on-site).

Note: This makes use of the BeautifulSoup library.

#!/usr/bin/env python
from BeautifulSoup import BeautifulSoup

import os
import re
import urllib2
from urlparse import urlparse
import wordpresslib

###
# * Fetch each post 
# * Download any included images
# * Add image as media object
# * Replace src in body with new src
# * Save post
###

wordpress = 'http://example.com/xmlrpc.php'
user = 'admin'
password = 'admin-password'

wp = wordpresslib.WordPressClient(wordpress, user, password)
wp.selectBlog(0)

for post in wp.getRecentPosts(numPosts=999):
    
    print "==== EDITING %s" % post.title
    soup = BeautifulSoup(post.description)
    
    images = soup.findAll('img')
    for image in images:
        old_src = image['src']
        data = urllib2.urlopen(old_src)
        filename = os.path.basename( urlparse(old_src).path )
        image['src'] = wp.newMediaObject(mediaFileName=filename, fileObject=data)
        print "++ Updated %s to %s" % (old_src, image['src'])
        
    post.description = str(soup)
    wp.editPost(post.id, post, True)

Note: This is an updated fork of the apparently unmaintained wordpress-library.

Updated