1 Star 0 Fork 1

ttpc2008/BaiduPCS-Go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
example.go 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
package downloader
import (
"fmt"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"io"
"os"
)
// DoDownload 执行下载
func DoDownload(durl string, savePath string, cfg *Config) {
var (
file io.WriterAt
err error
)
if savePath != "" {
file, err = os.Create(savePath)
if err != nil {
fmt.Println(err)
return
}
} else {
file = nil
}
download := NewDownloader(durl, file, cfg)
exitDownloadFunc := make(chan struct{})
download.OnExecute(func() {
dc := download.GetDownloadStatusChan()
var ts string
for {
select {
case v, ok := <-dc:
if !ok { // channel 已经关闭
return
}
if v.TotalSize() <= 0 {
ts = converter.ConvertFileSize(v.Downloaded(), 2)
} else {
ts = converter.ConvertFileSize(v.TotalSize(), 2)
}
fmt.Printf("\r ↓ %s/%s %s/s in %s ............",
converter.ConvertFileSize(v.Downloaded(), 2),
ts,
converter.ConvertFileSize(v.SpeedsPerSecond(), 2),
v.TimeElapsed(),
)
}
}
})
download.Execute()
close(exitDownloadFunc)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ttpc2008/BaiduPCS-Go.git
git@gitee.com:ttpc2008/BaiduPCS-Go.git
ttpc2008
BaiduPCS-Go
BaiduPCS-Go
v3.5.2

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385