NZB is an XML-based file format for retrieving posts from NNTP (Usenet) servers. Since NZB is XML-based, it's relatively easy to build one-off parsers to parse NZB files. This project is an attempt to consolidate those many one-off NZB parsers into one simple interface.
This package includes two implementations: one based on ElementTree, and a final implementation based on lxml. The fastest available parser is automatically selected at import time.
Basic installation:
pip install pynzbFor better performance with large NZB files, install with lxml support:
pip install pynzb[lxml]The library will automatically use lxml when available, but falls back to the built-in ElementTree parser if lxml is not installed.
from pynzb import nzb_parser
# Parse an NZB file
with open('my_file.nzb', 'r') as f:
nzb_content = f.read()
files = nzb_parser.parse(nzb_content)
# Access file information
for nzb_file in files:
print(f"Subject: {nzb_file.subject}")
print(f"Size: {sum(segment.bytes for segment in nzb_file.segments)} bytes")For a complete working example that demonstrates parsing an NZB file and displaying file information, see example.py.
Run it with:
python example.py path/to/your/file.nzbSimply import nzb_parser from the pynzb package. It's an instantiated version
of the fastest available parser that your system can support.
ETreeNZBParser: Available in thepynzb.etree_nzbnamespace.LXMLNZBParser: Available in thepynzb.lxml_nzbnamespace (requires lxml).
If you're using a specific parser, like the ETreeNZBParser, you will first
have to instantiate it:
from pynzb.etree_nzb import ETreeNZBParser
nzb_parser = ETreeNZBParser()Otherwise, you can just import the default parser for your system:
from pynzb import nzb_parserThen, simply call the parse method, giving it the NZB content as a string:
files = nzb_parser.parse(nzb_content)This will return a list of NZBFile objects for you to use.
All of the parsers return NZBFile objects, which have the following properties:
poster: The name of the user who posted the file to the newsgroup.date: Adatetime.daterepresentation of when the server first saw the file.subject: The subject used when the user posted the file to the newsgroup.groups: A list of strings representing the newsgroups in which this file may be found.segments: A list ofNZBSegmentobjects containing information about where to get the file contents.
Each NZBFile has a list of NZBSegment objects, which contain information
about how to retrieve a part of a file. Each NZBSegment object has these properties:
number: The number of the segment in the list of files.bytes: The size of the segment, in bytes.message_id: The Message-ID of the segment (useful for retrieving the full contents)