Skip to content

Parser

NZBParser

NZBParser(nzb: str, encoding: str | None = 'utf-8')

Initialize the NZBParser instance.

Parameters:

Name Type Description Default
nzb str

NZB content as a string.

required
encoding str

Encoding of the NZB content, defaults to utf-8.

'utf-8'

Raises:

Type Description
InvalidNZBError

Raised if the input is not valid XML. However, being valid XML doesn't guarantee it's a correctly structured NZB.

Source code in src/nzb/_core.py
@validate_call
def __init__(self, nzb: str, encoding: str | None = "utf-8") -> None:
    """
    Initialize the NZBParser instance.

    Parameters
    ----------
    nzb : str
        NZB content as a string.
    encoding : str, optional
        Encoding of the NZB content, defaults to `utf-8`.

    Raises
    ------
    InvalidNZBError
        Raised if the input is not valid XML.
        However, being valid XML doesn't guarantee it's a correctly structured NZB.
    """
    self.__nzb = nzb
    self.__encoding = encoding
    try:
        self.__nzbdict = xmltodict_parse(self.__nzb, encoding=self.__encoding)
    except ExpatError as error:
        raise InvalidNZBError(error.args[0])

from_file classmethod

from_file(nzb: StrPath, encoding: str | None = 'utf-8') -> Self

Create an NZBParser instance from an NZB file path.

Parameters:

Name Type Description Default
nzb StrPath

File path to the NZB.

required
encoding str

Encoding of the NZB, defaults to utf-8.

'utf-8'

Returns:

Type Description
NZBParser

An NZBParser instance initialized with the content of the specified NZB file.

Source code in src/nzb/_core.py
@classmethod
@validate_call
def from_file(cls, nzb: StrPath, encoding: str | None = "utf-8") -> Self:
    """
    Create an NZBParser instance from an NZB file path.

    Parameters
    ----------
    nzb : StrPath
        File path to the NZB.
    encoding : str, optional
        Encoding of the NZB, defaults to `utf-8`.

    Returns
    -------
    NZBParser
        An NZBParser instance initialized with the content of the specified NZB file.
    """
    nzb = Path(nzb).expanduser().resolve().read_text(encoding=encoding)
    return cls(nzb, encoding)

parse

parse() -> NZB

Parse the NZB.

Returns:

Type Description
NZB

NZB object representing the parsed NZB file.

Raises:

Type Description
InvalidNZBError

Raised if the input is not valid NZB.

Source code in src/nzb/_core.py
def parse(self) -> NZB:
    """
    Parse the NZB.

    Returns
    -------
    NZB
        NZB object representing the parsed NZB file.

    Raises
    ------
    InvalidNZBError
        Raised if the input is not valid NZB.
    """
    metadata = parse_metadata(self.__nzbdict)
    files = parse_files(self.__nzbdict)

    return NZB(metadata=metadata, files=files)