rain

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2022 License: MIT Imports: 13 Imported by: 0

README

简介

rain 是一款方便快捷的网络资源下载器,开发者可以非常方便的使用它来下载网络资源。

我目前没有完善文档,你现在要使用可以参考 test 文件,我会抽时间完善文档。

亮点

  • 零依赖
  • 断点续传
  • 多线程
  • 限速
  • 磁盘缓冲区
  • 下载进度

安装

go get -u gitee.com/rock_rabbit/rain

快速开始

package main

import "gitee.com/rock_rabbit/rain"

func downloadMi(hh *rain.HttpHandle) {
    hh.SetHeader("referer", "https://mi.qq.com")
}

// 带控制台进度条的下载
func main(){
    uri := "https://dldir1.qq.com/qqfile/qq/QQNT/QQ_6.8.6.4481_exp.dmg"
	outdir := "./tmp"
	outname := ""
	httpController, err := rain.Default.RunHttp(uri, rain.WithHttpOutdir(outdir), rain.WithHttpOutname(outname), downloadMi, rain.BarHook)
	if err != nil {
		t.Fatal(err)
	}
	fmt.Println("下载完成, 输出地址:", httpController.Stat().Outpath)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	HttpBarHook     = HttpBar(DefaultBar)
	WithHttpBarHook = WithHttpHook(HttpBarHook)
)
View Source
var Default = New()

Default 默认的下载器

View Source
var DefaultBar = &Bar{
	Template: &BarTemplate{
		Template:       `{{.CompletedLength}} / {{.TotalLength}} {{.Saucer}} {{.Progress}}% {{.DownloadSpeed}}/s {{.EstimatedTime}} CN:{{.Connections}}`,
		NoSizeTemplate: `{{.CompletedLength}} {{.DownloadSpeed}}/s {{.ConsumingTime}}`,
		Saucer:         "=",
		SaucerHead:     ">",
		SaucerPadding:  "-",
		BarStart:       "[",
		BarEnd:         "]",
		BarWidth:       80,
	},
	FriendlyFormat: true,
	Hide:           false,
	Stdout:         os.Stdout,
	FinishHide:     false,
}

Functions

This section is empty.

Types

type Bar

type Bar struct {
	// Template 进度条样式
	Template *BarTemplate

	// FriendlyFormat 使用人类友好的单位
	FriendlyFormat bool

	// FinishHide 完成后隐藏进度条,下载完成后清除掉进度条
	FinishHide bool

	// Hide 是否隐藏进度条
	Hide bool

	// Stdout 进度条输出, 默认为 os.Stdout
	Stdout io.Writer
}

Bar 提供一个简单的进度条

type BarStatString

type BarStatString struct {
	// TotalLength 文件总大小
	TotalLength string

	// CompletedLength 已下载大小
	CompletedLength string

	// DownloadSpeed 文件每秒下载速度
	DownloadSpeed string

	// EstimatedTime 预计下载完成还需要的时间
	EstimatedTime string

	// Progress 下载进度, 长度为 100
	Progress string

	// Saucer 进度条
	Saucer string

	// Connections 与服务器的连接数
	Connections string
}

BarStatString 注入到模版中的字符串结构 {{.CompletedLength}} / {{.TotalLength}} {{.Saucer}} {{.Progress}}% {{.DownloadSpeed}}/s {{.EstimatedTime}}

type BarTemplate

type BarTemplate struct {
	// Template 模版
	Template string

	// NoSizeTemplate 获取不到文件大小时的模板
	NoSizeTemplate string

	// Saucer 进度字符, 默认为 =
	Saucer string

	// SaucerHead 进度条头, 使用场景 =====> , 其中的 > 就是进度条头, 默认为 >
	SaucerHead string

	// SaucerPadding 进度空白字符, 默认为 -
	SaucerPadding string

	// BarStart 进度前缀, 默认为 [
	BarStart string

	// BarEnd 进度后缀, 默认为 ]
	BarEnd string

	// BarWidth 进度条宽度, 默认为 80
	BarWidth int
}

BarTemplate 进度条显示内容的参数

type HttpHandle

type HttpHandle struct {
	// contains filtered or unexported fields
}

HttpHandle HTTP 协议文件下载

func (*HttpHandle) AddHook added in v0.2.0

func (httpHandle *HttpHandle) AddHook(hook ...HttpHook)

AddHook 添加 Hook

func (*HttpHandle) Close

func (httpHandle *HttpHandle) Close() error

Close 关闭下载

func (*HttpHandle) Done

func (httpHandle *HttpHandle) Done() <-chan error

Done 下载完成指针

func (*HttpHandle) ReplaceHeader

func (httpHandle *HttpHandle) ReplaceHeader(d http.Header)

ReplaceHeader 替换掉 Header

func (*HttpHandle) SentHook added in v0.2.0

func (httpHandle *HttpHandle) SentHook()

SentHook 发送 hook 信息

func (*HttpHandle) SetClient

func (httpHandle *HttpHandle) SetClient(d *http.Client)

SetClient 设置 http 客户端

func (*HttpHandle) SetDebug

func (httpHandle *HttpHandle) SetDebug(d bool)

SetDebug 设置 debug

func (*HttpHandle) SetHeader

func (httpHandle *HttpHandle) SetHeader(k, v string)

SetHeader 设置 Header

func (*HttpHandle) SetOutdir

func (httpHandle *HttpHandle) SetOutdir(outdir string)

SetOutdir 设置输出目录

func (*HttpHandle) SetOutname

func (httpHandle *HttpHandle) SetOutname(outname string)

SetOutname 设置输出名称

func (*HttpHandle) SetSendHookIntervalTime added in v0.2.0

func (httpHandle *HttpHandle) SetSendHookIntervalTime(d time.Duration)

SetSendHookIntervalTime 设置发送 hook 间隔时间

func (*HttpHandle) SetURI

func (httpHandle *HttpHandle) SetURI(uri string)

SetURI 设置下载链接

func (*HttpHandle) Start

func (httpHandle *HttpHandle) Start(ctx context.Context) error

Start 启动下载

func (*HttpHandle) Stat

func (httpHandle *HttpHandle) Stat() *protocol.HttpStat

Stat 下载状态

func (*HttpHandle) Wait

func (httpHandle *HttpHandle) Wait() error

Wait 阻塞等待完成

type HttpHandleSetting added in v0.2.0

type HttpHandleSetting func(*HttpHandle)

HttpHandleSetting HTTP 下载的设置

func WithHttpClient added in v0.2.0

func WithHttpClient(d *http.Client) HttpHandleSetting

WithHttpClient 设置输出名称

func WithHttpHook added in v0.2.0

func WithHttpHook(hook ...HttpHook) HttpHandleSetting

WithHttpHook 添加 Hook

func WithHttpOutdir added in v0.2.0

func WithHttpOutdir(d string) HttpHandleSetting

WithHttpOutdir 设置输出目录

func WithHttpOutname added in v0.2.0

func WithHttpOutname(d string) HttpHandleSetting

WithHttpOutname 设置输出名称

func WithHttpReplaceHeader added in v0.2.0

func WithHttpReplaceHeader(d http.Header) HttpHandleSetting

WithHttpReplaceHeader 替换掉 Header

func WithHttpSendHookIntervalTime added in v0.2.0

func WithHttpSendHookIntervalTime(d time.Duration) HttpHandleSetting

WithHttpSendHookIntervalTime 设置发送 hook 间隔时间

type HttpHook added in v0.2.0

type HttpHook func(stat *protocol.HttpStat)

func HttpBar

func HttpBar(bar *Bar) HttpHook

HttpBar 实现 http 进度渲染

type Rain

type Rain struct {
	// contains filtered or unexported fields
}

func New

func New() *Rain

New 创建一个默认的下载器

func (*Rain) NewHttpHandle

func (rain *Rain) NewHttpHandle(uri string) *HttpHandle

NewHttpHandle 创建 http 协议下载器

func (*Rain) RunHttp

func (rain *Rain) RunHttp(uri string, settings ...HttpHandleSetting) (*HttpHandle, error)

func (*Rain) RunHttpContext

func (rain *Rain) RunHttpContext(ctx context.Context, uri string, settings ...HttpHandleSetting) (*HttpHandle, error)

RunHttpContext 阻塞运行一个默认的 http 下载器 ctx 上下文,可以创建上下文来控制运行的关闭 uri 资源地址,要下载的 http 文件地址

func (Rain) SetAllowOverwrite

func (c Rain) SetAllowOverwrite(d bool)

SetAllowOverwrite 设置是否允许覆盖文件

func (Rain) SetBreakpointResume

func (c Rain) SetBreakpointResume(d bool)

SetBreakpointResume 设置是否开启断点续传

func (Rain) SetConnectTimeout

func (c Rain) SetConnectTimeout(d time.Duration)

SetConnectTimeout HTTP 连接请求的超时时间,默认为 5 秒

func (Rain) SetCreateDir

func (c Rain) SetCreateDir(d bool)

SetCreateDir 设置目录不存在时是否可以创建目录

func (Rain) SetDiskCache

func (c Rain) SetDiskCache(d int)

SetDiskCache 设置磁盘缓冲区大小

func (Rain) SetProxy

func (c Rain) SetProxy(d func(*http.Request) (*url.URL, error))

SetProxy 设置网络代理

func (Rain) SetRetryNumber

func (c Rain) SetRetryNumber(d int)

SetRetryNumber 设置请求重试次数

func (Rain) SetRetryTime

func (c Rain) SetRetryTime(d time.Duration)

SetRetryTime 设置请求重试间隔时间

func (Rain) SetSpeedLimit

func (c Rain) SetSpeedLimit(d int)

SetSpeedLimit 设置限速下载

func (Rain) SetThreadCount

func (c Rain) SetThreadCount(d int)

SetThreadCount 设置下载线程数量

func (Rain) SetThreadSize

func (c Rain) SetThreadSize(d int)

SetThreadSize 设置多线程最大下载大小

func (Rain) SetTimeout

func (c Rain) SetTimeout(d time.Duration)

SetTimeout 超时时间

Directories

Path Synopsis
example
rate
Package rate provides a rate limiter.
Package rate provides a rate limiter.

Jump to

Keyboard shortcuts

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