Re: importing entries

Bill Mill <[email protected]>
Newsgroups gmane.comp.web.pyblosxom.devel
Message-ID <[email protected]>
On Apr 7, 2005 11:41 AM, will guaraldi <[email protected]> wrote:
> 
> I'm thinking we should turn the import-entries-from-rss code into a script
> with some documentation on how to run it from a command line and stick it
> in the contrib area.

Sure. I'll do the optparse code later, once I've got a comment-feed
parsing function. Here's version 2, using feedparser. Entry parsing
and time parsing are way nicer in feedparser, thanks for the
recommendation, wari:

=========start rss2blosxom.py===========
import feedparser, re, os, time

infile = file('index.xml')
outdir = '.'

items = feedparser.parse(infile)['items']
mkfilename = re.compile('\W').sub

class FileExistsError(Exception):
    def __init__(self, filename):
        self.filename = filename
    def __str__(self):
        return "File %s already exists" % filename

for item in items:
    title = item.title
    if 'category' in item:
        dir = item.category
    if not os.path.isdir(dir):
        os.makedirs(dir)
    filename = os.path.join(dir, mkfilename('_', title)) + '.txt'
    filename = filename[:200]
    if not os.path.isfile(filename):
        outfile = file(filename, 'w')
    else:
        raise FileExistsError(filename)
    print >> outfile, title
    print >> outfile, item.content[0].value
    outfile.close()
    t = time.mktime(item.modified_parsed)
    os.utime(filename, (t, t))
=============end rss2blosxom.py=======

Anybody have a better suggestion for file naming? I can't think of one.

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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.