Article View: news.software.readers
Article #85240Re: [XNews] Saving articles from particular newsgroups to certain directories
From: Robert Amesz
Date: Mon, 08 Oct 2001 00:22
Date: Mon, 08 Oct 2001 00:22
130 lines
5655 bytes
5655 bytes
Murray Peterson wrote:
> BlueJAMC <BlueJAMC@noneofthatspamhereatnetzero.net> wrote in
> news:Xns9132AC1268BE4BJAMC@207.225.159.8:
>
>> Also,
>> if anyone knows of any MBX "extractors"--something which will take
>> an MBX file, extract it to individual files for each entry, and
>> use a particular field for the filename, that could also work.
>
> OK -- here's a Perl script which extracts articles from .mbx files
> and saves them by the subject name.
That will never do! Exposing innocent people to the vagaries of Perl,
polluting this newsgroup with its foul syntax: it's an affront. So I
took the liberty of translating it into the beatific language that is
Python, its syntax capable of soothing the savage beast.
(Having said that, your programming style is pretty clear and
un-Perl-like. I'd guess you're more accustomed to programming in C.)
Robert Amesz
P.S. Standard caveats apply, only slightly tested. Also, I made the
subject-to-filename code a bit less strict, otherwise the output
of both the Perl and Python scrips should be the same.
--- Cut Here ---
#!/usr/bin/env python
import sys
import os.path
import re
# Program to extract individual files from an Xnews .mbx file
# Usage: "python mbx2files.py <mbx file> [<output dir>]"
# Simple exception class
class SplitMBXException(Exception):
pass
# Write a single article, use <title> as
def write_article(title, article, savedir):
# Make a valid path out of the title
path = title.strip()
path = re.sub("\s+", " ", path)
path = re.sub(":", ";", path)
path = re.sub("[/\\\\:*?\"><|]", "_", path)
path = re.sub("_+", "_", path)
if savedir:
path = os.path.join(savedir, path)
# Check for duplicate filenames
fullpath = "%s.txt" % path
n = 0
while os.path.exists(fullpath):
fullpath = "%s.%d.txt" % (path, n)
n += 1
# Open the file for writing
try:
outfile = open(fullpath, 'w')
except IOError:
raise SplitMBXException("Can't create outputfile \"%s\"" % fullpath)
# Write all the lines of the article
outfile.writelines(article)
outfile.close()
subject_re = re.compile("subject:\s*(.*)$", re.IGNORECASE)
def splitMBX(mbxname, savedir=None):
try:
mbx = open(mbxname)
except IOError:
raise SplitMBXException("Can't open mailboxfile \"%s\"" % mbxname)
article = []
subject = None
# Iterate over all the lines in the file
for line in mbx.xreadlines():
if line.startswith("From ???") :
#Its a new message - close off the old one
if article:
write_article(subject, article, savedir)
article = []
subject = None
else:
# Get subject - if there is one
matchobj = subject_re.match(line)
if matchobj:
subject = matchobj.group(1)
# Add the line to the article
article.append(line)
# Write last article
if article:
write_article(subject, article, savedir)
# Just a bit of Python magic to enable using this script as
# both a module and a standalone program
if __name__ == "__main__":
if len(sys.argv) not in range(2, 4):
print "Usage: python %s <mbx file> [<output dir>]" % sys.argv[0]
sys.exit(1)
else:
if len(sys.argv) == 3:
savedir = sys.argv[2]
else:
savedir = None
try:
splitMBX(sys.argv[1], savedir)
except SplitMBXException, e:
print "Error: %s\n" % str(e)
sys.exit(1)
print "done"
Technical Details
Message-ID:
<Xns9134153161571rcamesz@127.0.0.1>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!csulb.edu!tethys.csu.net!nntp.csufresno.edu!sn-xit-01!sn-post-01!supernews.com!news.demon.nl!amesz.demon.nl!not-for-mailReferences:
<Xns912C780CD877ABlueJAMC@207.225.159.8> <Xns912C89E6EFAE0imamidnighttoken@65.82.44.7> <Xns912D60F251C50BlueJAMC@207.225.159.7> <Xns912F9375A7650damiansmailandnewsco@64.152.100.102> <Xns9130916BC45DDBlueJAMC@207.225.159.7> <Xns9130BBD527020damiansmailandnewsco@64.152.100.102> <Xns9130B47515FC2imamidnighttoken@65.82.44.7> <Xns9130BAB6747D8BJAMC@207.225.159.6> <Xns9130C33E811DDimamidnighttoken@65.82.44.7> <Xns91321212940Crcamesz@127.0.0.1> <Xns913259E086C9BJAMC@207.225.159.7> <Xns913276D26DFCAimamidnighttoken@65.82.44.7> <Xns9132AC1268BE4BJAMC@207.225.159.8> <Xns9132B3DCDDDCBfarkle@24.71.223.45>Show Full Headers
Complete Article Headers
Message-ID: <Xns9134153161571rcamesz@127.0.0.1>
Subject: Re: [XNews] Saving articles from particular newsgroups to certain directories
Date: Mon, 08 Oct 2001 00:22:23 -0000
From: Robert Amesz <reqhye72zux@mailexpire.com>
References: <Xns912C780CD877ABlueJAMC@207.225.159.8> <Xns912C89E6EFAE0imamidnighttoken@65.82.44.7> <Xns912D60F251C50BlueJAMC@207.225.159.7> <Xns912F9375A7650damiansmailandnewsco@64.152.100.102> <Xns9130916BC45DDBlueJAMC@207.225.159.7> <Xns9130BBD527020damiansmailandnewsco@64.152.100.102> <Xns9130B47515FC2imamidnighttoken@65.82.44.7> <Xns9130BAB6747D8BJAMC@207.225.159.6> <Xns9130C33E811DDimamidnighttoken@65.82.44.7> <Xns91321212940Crcamesz@127.0.0.1> <Xns913259E086C9BJAMC@207.225.159.7> <Xns913276D26DFCAimamidnighttoken@65.82.44.7> <Xns9132AC1268BE4BJAMC@207.225.159.8> <Xns9132B3DCDDDCBfarkle@24.71.223.45>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.readers.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!csulb.edu!tethys.csu.net!nntp.csufresno.edu!sn-xit-01!sn-post-01!supernews.com!news.demon.nl!amesz.demon.nl!not-for-mail
Newsgroups: news.software.readers
User-Agent: Xnews/4.06.22 Hamster/1.3.22.104
X-Face: i/!"w>!GV8UL{^20`L2OEvvFz(T"{bFYn2<:u"5<4Blsl-isl(bTDMKVqWL?nk-VY7[[);j =yB|s]|c3nRG4c7^1#+/(D)v4ToNC;PoE=\7baN'i75%+2NhzndU~qJUTI*-v[R"l)Q$GOX3o(-#7C iY6a`%._v`G8cG`!/3,mt?pkyvjv3f}\-mZiNnXJ9EK~D>Qs(ckW8yam<<T!\Kf>`wtj3>eEAAV\
Reply-To: rcamesz@dds.nl
X-Complaints-To: abuse@nl.demon.net
NNTP-Posting-Host: amesz.demon.nl
X-NNTP-Posting-Host: amesz.demon.nl:212.238.72.220
X-Trace: news.demon.nl 1002500542 news:37205 NO-IDENT amesz.demon.nl:212.238.72.220
Lines: 129