Skip to content

Parser

NZBParser

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

Initialize the NZBParser.

Parameters:

Name Type Description Default
nzb str

NZB content as a string.

required
encoding str

Encoding of the NZB content.

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

    Parameters
    ----------
    nzb : str
        NZB content as a string.
    encoding : str, optional
        Encoding of the NZB content.
    """
    self.__nzb = nzb
    self.__encoding = encoding

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
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 = realpath(nzb).read_text(encoding=encoding)
    return cls(nzb, encoding=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.
    """
    try:
        nzbdict = xmltodict_parse(self.__nzb, encoding=self.__encoding)
    except ExpatError as error:
        raise InvalidNZBError(error.args[0])

    meta = parse_metadata(nzbdict)
    files = parse_files(nzbdict)

    return NZB(meta=meta, files=files)