|
Description:
Converts Wordpress Export Files (XML) to multiple html files and optionally uses tags and authors to create a directory structure.
Source: Text Source
import string, os, sys, getopt
from xml.dom import minidom
__author__ = 'Luis Rei <luis.rei@gmail.com>'
__homepage__ = 'http://luisrei.com'
__version__ = '1.0'
__date__ = '2008/03/23'
def convert(infile, outdir, authorDirs, categoryDirs):
"""Convert Wordpress Export File to multiple html files.
Keyword arguments:
infile -- the location of the Wordpress Export File
outdir -- the directory where the files will be created
authorDirs -- if true, create different directories for each author
categoryDirs -- if true, create directories for each category
"""
dom = minidom.parse(infile)
blog = []
for node in dom.getElementsByTagName('item'):
post = dict()
post["title"] = node.getElementsByTagName('title')[0].firstChild.data
post["date"] = node.getElementsByTagName('pubDate')[0].firstChild.data
post["author"] = node.getElementsByTagName(
'dc:creator')[0].firstChild.data
post["id"] = node.getElementsByTagName('wp:post_id')[0].firstChild.data
if node.getElementsByTagName('content:encoded')[0].firstChild != None:
post["text"] = node.getElementsByTagName(
'content:encoded')[0].firstChild.data
else:
post["text"] = ""
tempCategories = []
for subnode in node.getElementsByTagName('category'):
tempCategories.append(subnode.getAttribute('nicename'))
categories = [x for x in tempCategories if x != '']
post["categories"] = categories
blog.append(post)
outdir += "/wordpress/"
if os.path.exists(outdir) == False:
os.makedirs(outdir)
os.chdir(outdir)
for post in blog:
path = ""
if authorDirs == True:
path += post["author"].encode('utf-8') + "/"
if categoryDirs == True:
if (post["categories"] != None):
path += string.join(post["categories"],"/")
if os.path.exists(path) == False and path != "":
os.makedirs(path)
path = outdir + path
title = post["title"].encode('utf-8')
filename = path + "/" + post["id"] + ' - ' + title \
+ '.html'
meta = """<META http-equiv="Content-Type" content="text/html; \
charset=UTF-8">"""
f = open(filename, 'w')
f.write(meta+"\n")
start = "<html>\n<head>\n<title>"+ title +"</title>\n</head>\n<body>\n"
f.write(start)
text = post["text"].encode('utf-8')
text = text.replace("\n", "<br/>\n")
f.write(text)
end = "\n</body>\n</html>"
f.write(end)
f.close()
def usage(pname):
"""Displays usage information
keyword arguments:
pname -- program name (e.g. obtained as argv[0])
"""
print """python %s [-hac] [-o outdir] infile
Converts a Wordpress Export File to multiple html files.
Options:
-h,--help\tDisplays this information.
-a,--authors\tCreate different directories for each author.
-c,--categories\tCreate directory structure from post categories.
-o,--outdir\tSpecify a directory for the output.
Example:
python %s -c -o ~/TEMP ~/wordpress.2008-03-20.xml
""" % (pname, pname)
def main(argv):
outdir = ""
authors = False
categories = False
try:
opts, args = getopt.getopt(
argv[1:], "ha:o:c", ["help", "authors", "outdir", "categories"])
except getopt.GetoptError, err:
print str(err)
usage(argv[0])
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage(argv[0])
sys.exit()
elif opt in ("-a", "--authors"):
authors = True
elif opt in ("-c", "--categories"):
categories = True
elif opt in ("-o", "--outdir"):
outdir = arg
infile = "".join(args)
if infile == "":
print "Error: Missing Argument: missing wordpress export file."
usage(argv[0])
sys.exit(3)
if outdir == "":
outdir = os.getcwd()
convert(infile, outdir, authors, categories)
if __name__ == "__main__":
main(sys.argv)
Discussion:
I used this to export my notes out of a wordpress blog (and into evernote.com).
Issues/notes:
Currently does not handle images, attachments or comments.
Was only tested on MacOS X (10.5)
Was not "carefully" developed e.g. poor exception handling, little testing, ...
For getting a Wordpress Export File see http://wordpress.com/blog/2006/06/12/xml-import-export/
|