Parser
Nzb
Bases: Base
Represents a complete NZB file.
Example:
from nzb import Nzb
text = '''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE nzb PUBLIC "-//newzBin//DTD NZB 1.1//EN" "http://www.newzbin.com/DTD/nzb/nzb-1.1.dtd">
<nzb xmlns="http://www.newzbin.com/DTD/2003/nzb">
<head>
<meta type="title">Big Buck Bunny - S01E01.mkv</meta>
<meta type="password">secret</meta>
<meta type="tag">HD</meta>
<meta type="category">TV</meta>
</head>
<file poster="John <nzb@nowhere.example>" date="1706440708" subject="[1/1] - "Big Buck Bunny - S01E01.mkv" yEnc (1/2) 1478616">
<groups>
<group>alt.binaries.boneless</group>
</groups>
<segments>
<segment bytes="739067" number="1">9cacde4c986547369becbf97003fb2c5-9483514693959@example</segment>
<segment bytes="739549" number="2">70a3a038ce324e618e2751e063d6a036-7285710986748@example</segment>
</segments>
</file>
</nzb>
'''
nzb = Nzb.from_str(text)
print(f"{nzb.file.name} ({nzb.meta.category}) was posted by {nzb.file.poster} on {nzb.file.posted_at}.")
print(f"Number of files: {len(nzb.files)}")
print(f"Total size in bytes: {nzb.size}")
file
cached
property
file: File
The main content file (episode, movie, etc) in the NZB. This is determined by finding the largest file in the NZB and may not always be accurate.
filenames
cached
property
Tuple of unique file names across all the files in the NZB. May return an empty tuple if it fails to extract the name for every file.
files
instance-attribute
File objects representing the files included in the NZB.
groups
cached
property
Tuple of unique groups across all the files in the NZB.
meta
class-attribute
instance-attribute
Optional creator-definable metadata for the contents of the NZB.
par2_percentage
cached
property
par2_percentage: float
Percentage of the size of all the .par2
files relative to the total size.
posters
cached
property
Tuple of unique posters across all the files in the NZB.
from_file
classmethod
Parse the given file into an Nzb
.
Handles both regular and gzipped NZB files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nzb
|
str | PathLike[str]
|
Path to the NZB file. |
required |
Returns:
Type | Description |
---|---|
Nzb
|
Object representing the parsed NZB file. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
Raised if the specified file doesn't exist. |
InvalidNzbError
|
Raised if the NZB is invalid. |
Source code in src/nzb/_core.py
from_json
classmethod
Deserialize the given JSON string into an Nzb
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json
|
str
|
JSON string representing the NZB. |
required |
Returns:
Type | Description |
---|---|
Nzb
|
Object representing the parsed NZB file. |
Raises:
Type | Description |
---|---|
InvalidNzbError
|
Raised if the NZB is invalid. |
Source code in src/nzb/_core.py
from_str
classmethod
Parse the given string into an Nzb
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nzb
|
str
|
NZB string. |
required |
Returns:
Type | Description |
---|---|
Nzb
|
Object representing the parsed NZB file. |
Raises:
Type | Description |
---|---|
InvalidNzbError
|
Raised if the NZB is invalid. |
Source code in src/nzb/_core.py
has_extension
Check if any file in the NZB has the specified extension.
This method ensures consistent extension comparison by normalizing the extension (removing any leading dot) and handling case-folding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ext
|
str
|
Extension to check for, with or without a leading dot (e.g., |
required |
Returns:
Type | Description |
---|---|
bool
|
|
```
|
|
Source code in src/nzb/_core.py
has_par2
has_par2() -> bool
has_rar
has_rar() -> bool
is_obfuscated
is_obfuscated() -> bool
is_rar
is_rar() -> bool
to_json
Serialize the Nzb
object into a JSON string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pretty
|
bool
|
Whether to pretty format the JSON string. |
False
|
Returns:
Type | Description |
---|---|
str
|
JSON string representing the NZB. |
Source code in src/nzb/_core.py
Meta
Bases: Base
Optional creator-definable metadata for the contents of the NZB.
File
Bases: Base
Represents a complete file, consisting of segments that make up a file.
extension
cached
property
extension: str | None
Extension of the file without the leading dot extracted from the File.name
.
May return None
if it fails to extract the extension.
name
cached
property
name: str | None
Complete name of the file with it's extension extracted from the subject.
May return None
if it fails to extract the name.
posted_at
instance-attribute
posted_at: datetime
The date and time when the file was posted, in UTC.
stem
cached
property
stem: str | None
Base name of the file without it's extension extracted from the File.name
.
May return None
if it fails to extract the stem.
has_extension
Check if the file has the specified extension.
This method ensures consistent extension comparison by normalizing the extension (removing any leading dot) and handling case-folding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ext
|
str
|
Extension to check for, with or without a leading dot (e.g., |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Examples:
>>> file.has_extension('.TXT') # True for 'file.txt'
True
>>> file.has_extension('txt') # Also True for 'file.txt'
True
Source code in src/nzb/_models.py
is_obfuscated
is_obfuscated() -> bool
is_par2
is_par2() -> bool