Re: importing entries
Bill Mill <[email protected]>
| Newsgroups | gmane.comp.web.pyblosxom.devel |
|---|---|
| Message-ID | <[email protected]> |
On Apr 6, 2005 4:37 PM, will guaraldi <[email protected]> wrote: > > I was skimming http://www.netsplit.com/blog/tech/new_blog (one of many > blogs that use PyBlosxom, but don't have the PyBlosxom image thing, so > we'd probably never figure it out by doing a google search) and the second > comment talks about importing entries by using RSS feeds. > > Does that strike anyone as a good idea for something to implement? For > exmaple, if someone wanted to move from SystemX to PyBlosxom, they'd have > SystemX produce an RSS feed, then we'd run it through some script, and > that'd generate all the categories and entries for a PyBlosxom datadir. > > If someone had some spare time, I think that might be a useful thing to > have. > > Thoughts? A few thoughts at the bottom, but first, a shot at it. Requires cElementTree: ===============start rss2blosxom.py============ #!/usr/bin/env python import cElementTree as ce import re, os infile = file("ted.xml") outdir = "." #\W = ^[a-zA-Z0-9], except unicode-aware mkfilename = re.compile('\W').sub for e, item in ce.iterparse(infile): #print e, item if item.tag == 'item': title = item.findtext('title') dir = item.findtext('category') if not os.path.isdir(item.findtext('category')): os.makedirs(item.findtext('category')) filename = os.path.join(dir, mkfilename('_', title)) + '.txt' filename = filename[:200] if not os.path.isfile(filename): outfile = file(filename, 'w') else: raise "file %s already exists" % filename print >> outfile, title for n in item: if n.tag.find('content') != -1: print >> outfile, n.text elif n.tag == 'description': print >> outfile, n.text ===============end rss2blosxom.py============ I tested this on both Ted's and my sites' main RSS files, and it worked like a charm. It seems, in looking over some RSS from others, that some feeds use the 'content:encoded' tag, while others use 'description'. I test for a content tag (which is a little wonky in cElementTree, so there's room for improvement in that test), and if it's there, use that. If not, I use the description. Also, how do you want to do file naming? I sanitize the title and use the first 200 characters of it, but that's suboptimal. To use it, just change the infile and outdir parameters at the top. If it was made for real, it could obviously accept command line parameters. Peace Bill Mill bill.mill at gmail.com ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click