gokhttp_download

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 26 Imported by: 2

README

gOkHttp-download

Addon for gOkHttp for downloading files and streams

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidDownloadType = errors.New("not a valid DownloadType")
View Source
var ErrInvalidDownloadVersion = errors.New("not a valid DownloadVersion")

Functions

func Censor

func Censor(in string, pad string, chars int, front bool) string

func GetCurrentIPAddress

func GetCurrentIPAddress(hClient *http.Client) (ip string)

func SortHLSStreams added in v0.4.1

func SortHLSStreams(ctx context.Context, hClient *http.Client, playlistUrl string, opts ...gokhttp_requests.Option) ([]HLSVariant, []HLSAudioRendition, error)

SortHLSStreams fetches and parses the master playlist, returning video variants sorted by bandwidth descending and audio renditions for the selected video variant. This allows callers to inspect available qualities before starting a download.

func Truncate

func Truncate(in string, chars, mode int) string

Types

type CustomTime added in v0.3.3

type CustomTime struct {
	*atomic.Time
}

CustomTime wraps a *atomic.Time with a custom JSON marshaler

func NewCustomTime added in v0.3.3

func NewCustomTime(t time.Time) *CustomTime

func (*CustomTime) MarshalJSON added in v0.3.3

func (t *CustomTime) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for CustomMap

func (*CustomTime) UnmarshalJSON added in v0.3.3

func (t *CustomTime) UnmarshalJSON(data []byte) error

type DownloadTask

type DownloadTask interface {
	Download(ctx context.Context) error
	Type() DownloadType
	Progress(sb *strings.Builder) error
	ResetDelta()
}

type DownloadType

type DownloadType int

DownloadType x ENUM(Threaded, LiveHLS)

const (
	// DownloadTypeThreaded is a DownloadType of type Threaded.
	DownloadTypeThreaded DownloadType = iota
	// DownloadTypeLiveHLS is a DownloadType of type LiveHLS.
	DownloadTypeLiveHLS
)

func ParseDownloadType

func ParseDownloadType(name string) (DownloadType, error)

ParseDownloadType attempts to convert a string to a DownloadType.

func (DownloadType) MarshalText

func (x DownloadType) MarshalText() ([]byte, error)

MarshalText implements the text marshaller method.

func (DownloadType) Ptr

func (x DownloadType) Ptr() *DownloadType

func (DownloadType) String

func (x DownloadType) String() string

String implements the Stringer interface.

func (*DownloadType) UnmarshalText

func (x *DownloadType) UnmarshalText(text []byte) error

UnmarshalText implements the text unmarshaller method.

type DownloadVersion

type DownloadVersion int

DownloadVersion x ENUM(v1)

const (
	// DownloadVersionV1 is a DownloadVersion of type V1.
	DownloadVersionV1 DownloadVersion = iota
)

func ParseDownloadVersion

func ParseDownloadVersion(name string) (DownloadVersion, error)

ParseDownloadVersion attempts to convert a string to a DownloadVersion.

func (DownloadVersion) MarshalText

func (x DownloadVersion) MarshalText() ([]byte, error)

MarshalText implements the text marshaller method.

func (DownloadVersion) Ptr

func (DownloadVersion) String

func (x DownloadVersion) String() string

String implements the Stringer interface.

func (*DownloadVersion) UnmarshalText

func (x *DownloadVersion) UnmarshalText(text []byte) error

UnmarshalText implements the text unmarshaller method.

type FMP4Demuxer added in v0.3.12

type FMP4Demuxer struct {
	Muxer            *StreamMuxer
	InitSegment      []byte
	AudioInitSegment []byte
	// contains filtered or unexported fields
}

func NewFMP4Demuxer added in v0.3.12

func NewFMP4Demuxer(muxer *StreamMuxer) *FMP4Demuxer

func (*FMP4Demuxer) FlushAll added in v0.3.13

func (d *FMP4Demuxer) FlushAll() error

func (*FMP4Demuxer) ProcessAudioSegment added in v0.3.12

func (d *FMP4Demuxer) ProcessAudioSegment(segmentData []byte) error

func (*FMP4Demuxer) ProcessSegment added in v0.3.12

func (d *FMP4Demuxer) ProcessSegment(segmentData []byte) error

func (*FMP4Demuxer) SetAudioInitSegment added in v0.3.12

func (d *FMP4Demuxer) SetAudioInitSegment(data []byte)

func (*FMP4Demuxer) SetInitSegment added in v0.3.12

func (d *FMP4Demuxer) SetInitSegment(data []byte)

type GlobalDownloadTracker

type GlobalDownloadTracker struct {
	// Synchronization
	sync.WaitGroup
	GraceFulStop *atomic.Bool `json:"-"`
	// Idle stats
	IdleSince   *CustomTime    `json:"idleSince"`
	IdleValue   *atomic.Uint64 `json:"-"`
	IdleTimeout *atomic.Int64  `json:"-"`
	// MetaData
	LastTick     *CustomTime                    `json:"lastTick"`
	CurrentIP    *atomic.String                 `json:"currentIP"`
	TotalThreads *atomic.Uint64                 `json:"totalThreads"`
	Tasks        *TaskMap[string, DownloadTask] `json:"tasks"`
	// Total stats
	TotalFiles *atomic.Uint64 `json:"totalFiles"`
	TotalBytes *atomic.Uint64 `json:"totalBytes"`
	// Download stats
	DownloadedFiles *atomic.Uint64 `json:"downloadedFiles"`
	DownloadedBytes *atomic.Uint64 `json:"downloadedBytes"`
	// Per tick stats
	DeltaBytes *atomic.Uint64 `json:"deltaBytes"`
}

func NewGlobalDownloadTracker

func NewGlobalDownloadTracker(idleTimeout time.Duration) *GlobalDownloadTracker

func (*GlobalDownloadTracker) IdleTimeoutExceeded

func (global *GlobalDownloadTracker) IdleTimeoutExceeded() bool

func (*GlobalDownloadTracker) PollIP

func (global *GlobalDownloadTracker) PollIP(hClient *http.Client)

func (*GlobalDownloadTracker) Stop

func (global *GlobalDownloadTracker) Stop()

func (*GlobalDownloadTracker) Tick

func (global *GlobalDownloadTracker) Tick(humanReadable bool) string

type HLSAudioRendition added in v0.4.1

type HLSAudioRendition struct {
	GroupID   string
	Name      string
	Language  *string
	Channels  *string
	URI       *string
	MediaItem *m3u8.MediaItem
}

HLSAudioRendition represents an audio rendition from the master playlist, associated with a specific video variant's audio group.

type HLSStreamSelector added in v0.4.1

type HLSStreamSelector struct {
	VideoIndex int // index into sorted video variants (default 0 = highest)
	AudioIndex int // index into sorted audio renditions (default 0 = first match)
}

HLSStreamSelector configures which video and audio stream indices to download. Index 0 is the highest quality (sorted by bandwidth descending).

type HLSVariant added in v0.4.1

type HLSVariant struct {
	Bandwidth        int
	AverageBandwidth *int
	Resolution       *m3u8.Resolution
	Codecs           *string
	Name             *string
	FrameRate        *float64
	PlaylistItem     *m3u8.PlaylistItem
}

HLSVariant represents a video variant stream from the master playlist, sorted by bandwidth descending (index 0 = highest quality).

type SequencedBuffer added in v0.3.12

type SequencedBuffer struct {
	MSN  int
	Data []byte
}

SequencedBuffer wraps downloaded segment data with its media sequence number.

type SequencedSegment added in v0.3.12

type SequencedSegment struct {
	MSN     int
	Segment *m3u8.SegmentItem
}

SequencedSegment wraps a segment item with its media sequence number.

type StreamFormat added in v0.3.11

type StreamFormat int
const (
	StreamFormatTS StreamFormat = iota
	StreamFormatFMP4
)

type StreamHLSTask

type StreamHLSTask struct {
	// Tracking ref
	Global  *GlobalDownloadTracker    `json:"-"`
	HClient *http.Client              `json:"-"`
	ReqOpts []gokhttp_requests.Option `json:"-"`

	// Muxers (only one is used, based on Format)
	Muxer       *StreamMuxer `json:"-"`
	FMP4Demuxer *FMP4Demuxer `json:"-"`
	TaskStats   *StreamStats `json:"taskStats"`

	TaskType     DownloadType                    `json:"taskType"`
	TaskVersion  DownloadVersion                 `json:"taskVersion"`
	FileName     *atomic.String                  `json:"fileName"`
	FileLocation *atomic.String                  `json:"fileLocation"`
	PlayListUrl  *atomic.String                  `json:"playListUrl"`
	BaseUrl      *url.URL                        `json:"-"`
	Opts         []gokhttp_requests.Option       `json:"-"`
	SegmentChan  chan SequencedSegment           `json:"-"`
	BufferChan   chan SequencedBuffer            `json:"-"`
	SegmentCache *hashmap.Map[string, time.Time] `json:"-"`
	SaveSegments bool                            `json:"-"`

	// Format detection (set during getSegments init)
	Format StreamFormat `json:"format"`

	// Stream selection
	StreamSelector *HLSStreamSelector `json:"-"`

	// Audio stream support
	HasAudioStream    bool                            `json:"-"`
	AudioPlaylistUrl  *atomic.String                  `json:"-"`
	AudioBaseUrl      *url.URL                        `json:"-"`
	AudioSegmentChan  chan SequencedSegment           `json:"-"`
	AudioBufferChan   chan SequencedBuffer            `json:"-"`
	AudioSegmentCache *hashmap.Map[string, time.Time] `json:"-"`
	// contains filtered or unexported fields
}

func NewStreamHLSTask

func NewStreamHLSTask(global *GlobalDownloadTracker, hClient *http.Client, playlistUrl, fileLocation string, saveSegments bool, opts ...gokhttp_requests.Option) (*StreamHLSTask, error)

func (*StreamHLSTask) Download

func (st *StreamHLSTask) Download(ctx context.Context) error

func (*StreamHLSTask) Progress

func (st *StreamHLSTask) Progress(sb *strings.Builder) error

func (*StreamHLSTask) ResetDelta

func (st *StreamHLSTask) ResetDelta()

func (*StreamHLSTask) Type

func (st *StreamHLSTask) Type() DownloadType

type StreamMuxer

type StreamMuxer struct {
	F       *os.File
	Muxer   *mpeg2.TSMuxer
	Demuxer *mpeg2.TSDemuxer
	Streams map[mpeg2.TS_STREAM_TYPE]uint16
	// contains filtered or unexported fields
}

func NewStreamMuxer

func NewStreamMuxer(f *os.File) *StreamMuxer

func (*StreamMuxer) AddStreams

func (m *StreamMuxer) AddStreams(buf *bytes.Buffer) error

func (*StreamMuxer) Demux

func (m *StreamMuxer) Demux(cid mpeg2.TS_STREAM_TYPE, frame []byte, pts uint64, dts uint64)

func (*StreamMuxer) InputSafe added in v0.3.11

func (m *StreamMuxer) InputSafe(buf *bytes.Buffer) error

InputSafe feeds a buffer to the demuxer with mutex protection for concurrent audio+video access.

func (*StreamMuxer) Mux

func (m *StreamMuxer) Mux(pkg []byte)

type StreamStats

type StreamStats struct {
	DownloadedSegments *atomic.Uint64 `json:"downloadedSegments"`
	DownloadedDuration *atomic.Int64  `json:"downloadedDuration"`
	DownloadedBytes    *atomic.Uint64 `json:"downloadedBytes"`
}

type TaskMap added in v0.3.3

type TaskMap[K string, V DownloadTask] struct {
	*hashmap.Map[K, V]
}

TaskMap wraps a hashmap.Map with a custom JSON marshaler

func (*TaskMap[K, V]) MarshalJSON added in v0.3.3

func (m *TaskMap[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for CustomMap

func (*TaskMap[K, V]) UnmarshalJSON added in v0.3.3

func (m *TaskMap[K, V]) UnmarshalJSON(data []byte) error

type ThreadedChunk

type ThreadedChunk struct {
	// Parent
	Parent *ThreadedDownloadTask `json:"-"`
	// Metadata
	ChunkID    *atomic.Uint64 `json:"chunkID"`
	ChunkStart uint64         `json:"-"`
	ChunkStop  uint64         `json:"-"`
	F          *os.File       `json:"-"`
	// Totals
	FileSize *atomic.Uint64 `json:"fileSize"`
	// Download stats
	DownloadedBytes *atomic.Uint64 `json:"downloadedBytes"`
	DeltaBytes      *atomic.Uint64 `json:"deltaBytes"`
}

func (*ThreadedChunk) Write added in v0.3.7

func (tc *ThreadedChunk) Write(p []byte) (int, error)

type ThreadedDownloadTask

type ThreadedDownloadTask struct {
	// Tracking ref
	Global  *GlobalDownloadTracker    `json:"-"`
	HClient *http.Client              `json:"-"`
	ReqOpts []gokhttp_requests.Option `json:"-"`
	// Metadata
	TaskType     DownloadType    `json:"taskType"`
	TaskVersion  DownloadVersion `json:"taskVersion"`
	FileName     *atomic.String  `json:"fileName"`
	FileLocation *atomic.String  `json:"fileLocation"`
	FileURL      *atomic.String  `json:"fileURL"`
	Resumable    *atomic.Bool    `json:"resumable"`
	// Totals
	TaskStats *ThreadedChunk `json:"taskStats"`
	// Chunks
	Chunks     *hashmap.Map[string, *ThreadedChunk] `json:"chunks"`
	ChunkCount *atomic.Uint64                       `json:"chunkCount"`
}

func NewThreadedDownloadTask

func NewThreadedDownloadTask(ctx context.Context, hClient *http.Client, global *GlobalDownloadTracker, fileLocation, fileURL string, threads, expectedSize uint64, opts ...gokhttp_requests.Option) (*ThreadedDownloadTask, error)

func (*ThreadedDownloadTask) Download

func (tt *ThreadedDownloadTask) Download(ctx context.Context) error

func (*ThreadedDownloadTask) Progress

func (tt *ThreadedDownloadTask) Progress(sb *strings.Builder) error

func (*ThreadedDownloadTask) ResetDelta

func (tt *ThreadedDownloadTask) ResetDelta()

func (*ThreadedDownloadTask) Type

Jump to

Keyboard shortcuts

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