#!/usr/local/bin/python import re, os from elementtree import ElementTree LIBRARY_PATH = '/usr/home/mihai/bin/library/delicious.xml' OUTPUT_PATH = '/usr/home/mihai/public_html/persistent/templates/books-body.html' BOOK_FIELDS = ['author', 'title', 'asin'] DISPLAY_SHELVES = ['Reading', 'To Read', 'Should Read'] UPPER_CASE_RE = re.compile('([A-Z])([A-Z]+)') PARENTHESIS_RE = re.compile('\(.*\)\s*') tree = ElementTree.parse(LIBRARY_PATH) # load all books books = {} for bookNode in tree.findall('//items/book'): book = {} for field in BOOK_FIELDS: value = bookNode.get(field, None) if not value: book[field] = None continue # only first line of multi-line fields (author names can be duplicated) value = value.splitlines()[0] # normalize case value = UPPER_CASE_RE.sub(lambda x: x.group(1) + x.group(2).lower(), value) # strip out values in parentheses value = PARENTHESIS_RE.sub("", value) # and leading/trailing whitespace value = value.strip() book[field] = value books[bookNode.get('uuid')] = book # load shelves shelves = {} for shelfNode in tree.findall('//shelves/shelf'): shelf = [] for shelfItemNode in shelfNode.findall('linkto'): shelf.append(books[shelfItemNode.get('uuid')]) shelves[shelfNode.get('name')] = shelf # display shelves output = file(OUTPUT_PATH, "w") output.write('