txp_xmlrpc_importer.py (Source)

from os import mkdir
from datetime import datetime
from xmlrpclib import ServerProxy

BLOG_URL   = 'http://blog.url'    # Base Textpattern URL
BLOG_USER  = 'username'           # User name
BLOG_PASS  = 'password'           # Password
BLOG_POSTS = 1000                 # Number of blog posts

METAS = """.. title: %(title)s
.. slug: %(wp_slug)s
.. date: %(date)s
.. author: %(userid)s
.. tags: %(cats)s
.. category: %(section)s

"""

def write_post(blogid, post):
  post['title'] = post['title'].encode('utf-8')
  post['date'] = post['dateCreated'].strftime('%Y-%m-%d %H:%M:%S')
  post['cats'] = ', '.join([p for p in post['categories'] if p]).encode('utf-8')
  post['section'] = blogid
  postid = int(post['postid'])
  basename = '%s/%04d-%s' % (blogid, postid, post['wp_slug'])
  # Write content
  with open('{0}.textile'.format(basename), 'w') as f:
    f.write(post['description'].encode('utf-8'))
  # Write metadata file
  with open('{0}.meta'.format(basename), 'w') as f:
    f.write(METAS % post)
  # Append redirect tuple in format suitable for pasting in Nikola's conf.py
  with open('url_map.py', 'a') as f:
    f.write("('%s/%d/%s/index.html', '/posts/%s.html'),\n" % (blogid, postid, post['wp_slug'], post['wp_slug']))
  print filename
  
def download_section(blogid):
  print "Processing section:", blogid
  posts = blog.metaWeblog.getRecentPosts(blogid, BLOG_USER, BLOG_PASS, BLOG_POSTS)
  if not posts:
    print "- Section empty"
    return
  mkdir(blogid)
  for post in posts:
    write_post(blogid, post)
  print "+ Done"

if __name__ == '__main__':
  # Create XML-RPC server proxy
  blog = ServerProxy(BLOG_URL + '/rpc/', use_datetime=True)
  # Check that server works
  # user_info = blog.blogger.getUserInfo('', BLOG_USER, BLOG_PASS)
  # print user_info
  sections = blog.blogger.getUsersBlogs('', BLOG_USER, BLOG_PASS)
  for section in sections:
    download_section(section['blogid'])