nzbparser

package module
v0.5.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2025 License: MIT Imports: 10 Imported by: 0

README

NZBParser

A Go module for parsing NZB files.

About

This is a fork of Tensai75's nzbparser, a library for parsing NZB files in Go.

NZB files are XML-based files that contain information necessary for downloading binary files from Usenet servers. This parser allows you to extract and work with that information programmatically.

Installation

go get github.com/javi11/nzbparser

Usage

package main

import (
	"fmt"
	"github.com/javi11/nzbparser"
)

func main() {
	// Parse an NZB file
	nzb, err := nzbparser.ParseNZBFile("path/to/file.nzb")
	if err != nil {
		panic(err)
	}

	// Access NZB metadata
	fmt.Println("NZB File:", nzb.Filename)
	fmt.Println("Total Size:", nzb.Size)
	fmt.Println("Number of files:", len(nzb.Files))

	// Iterate through files in the NZB
	for _, file := range nzb.Files {
		fmt.Println("File:", file.Filename)
		fmt.Println("Size:", file.Size)
	}
}

License

This project is licensed under the MIT License - see the original repository for details.

Acknowledgments

Documentation

Index

Constants

View Source
const (
	// xml header for nzb files
	Header = `<?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">` + "\n"
	// xml namespace for nzb files
	Xmlns = "http://www.newzbin.com/DTD/2003/nzb"
)

Variables

This section is empty.

Functions

func MakeUnique

func MakeUnique(nzb *Nzb)

clean up nzb files by keeping only the first occurrence of duplicate file entries and removing duplicate segments

func ScanNzbFile

func ScanNzbFile(nzb *Nzb)

scan the nzb struct for additional information

func Write

func Write(nzb *Nzb) ([]byte, error)

write nzb struct to nzb xml as byte slice

func WriteString

func WriteString(nzb *Nzb) (string, error)

write nzb struct to nzb xml as string

Types

type Nzb

type Nzb struct {
	Comment       string            // comment tag
	Meta          map[string]string // meta data as map
	Files         NzbFiles          // files structure
	TotalFiles    int               // number of total files
	Segments      int               // number of available segments
	TotalSegments int               // number of total segments
	Bytes         int64             // total size of all files
}

nzb file structure with additional information

func Parse

func Parse(buf io.Reader) (*Nzb, error)

parse nzb file provided as io.Reader buffer

func ParseString

func ParseString(data string) (*Nzb, error)

parse nzb file provided as string

func ParseStringWithOptions added in v0.5.0

func ParseStringWithOptions(data string, opts ParseOptions) (*Nzb, error)

parse nzb file provided as string with custom options

func ParseWithOptions added in v0.5.0

func ParseWithOptions(buf io.Reader, opts ParseOptions) (*Nzb, error)

parse nzb file provided as io.Reader buffer with custom options

type NzbFile

type NzbFile struct {
	Groups        []string    `xml:"groups>group"`
	Segments      NzbSegments `xml:"segments>segment"`
	Poster        string      `xml:"poster,attr"`
	Date          int         `xml:"date,attr"`
	Subject       string      `xml:"subject,attr"`
	Bytes         int64       `xml:"bytes,attr"`    // total size of the file
	FileHash      string      `xml:"filehash,attr"` // hash of the file
	Number        int         `xml:"-"`             // number of the file (if indicated in the subject)
	Filename      string      `xml:"-"`             // filename of the file (if indicated in the subject)
	Basefilename  string      `xml:"-"`             // basefilename of the file (if indicated in the subject)
	TotalSegments int         `xml:"-"`             // number of total segments
}

individual file structure with additional information

type NzbFiles

type NzbFiles []NzbFile

a slice of NzbFiles extended to allow sorting

func (NzbFiles) Len

func (s NzbFiles) Len() int

func (NzbFiles) Less

func (s NzbFiles) Less(i, j int) bool

func (NzbFiles) Swap

func (s NzbFiles) Swap(i, j int)

type NzbSegment

type NzbSegment struct {
	Bytes  int    `xml:"bytes,attr"`
	Number int    `xml:"number,attr"`
	ID     string `xml:",innerxml"`
}

individual segment structure

type NzbSegments

type NzbSegments []NzbSegment

a slice of NzbSegments extended to allow sorting

func (NzbSegments) Len

func (s NzbSegments) Len() int

func (NzbSegments) Less

func (s NzbSegments) Less(i, j int) bool

func (NzbSegments) Swap

func (s NzbSegments) Swap(i, j int)

type ParseOptions added in v0.5.0

type ParseOptions struct {
	RemoveDuplicates bool // whether to remove duplicate files and segments
}

ParseOptions allows configuration of the NZB parsing behavior

type Subject added in v0.4.0

type Subject struct {
	Subject       string // full subject
	Header        string // header (remaining text before the filename)
	Filename      string // filename with extension (in the subject it is usually between quotes)
	Basefilename  string // filename without extension(s)
	File          int    // number of the file in the file set (=X in [X/Y])
	TotalFiles    int    // number of total files in the file set (=Y in [X/Y])
	Segment       int    // number of the segment of this file (=X in (X/Y))
	TotalSegments int    // number of total segments for this file (=Y in (X/Y))
}

func ParseSubject added in v0.4.0

func ParseSubject(s string) (Subject, error)

the Parse method analyses a given string and returns the Subject structure

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL