1 Star 0 Fork 0

一介农夫/downloader

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
download.go 2.27 KB
一键复制 编辑 原始数据 按行查看 历史
一介农夫 提交于 2021-08-01 19:25 . 1. 逻辑研究
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"sync"
)
//作者 bambuo
//时间 2021/7/19 19:40
//版本 1.0
const domain = ``
const save = ``
type ErrorDownload struct {
Task *Task
Error error
}
type Task struct {
NO int
url string
}
func readFile(domain, m3u8File string) (map[int]string, error) {
in, err := os.OpenFile(m3u8File, os.O_RDONLY, os.ModePerm)
if err != nil {
return nil, err
}
reader := bufio.NewReader(in)
var index = 1
var tss = map[int]string{}
for {
line, _, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
break
} else {
return nil, err
}
}
if len(line) == 0 || strings.HasPrefix(string(line), `#`) {
continue
}
if strings.HasPrefix(string(line), `/`) {
tss[index] = domain + string(line[1:])
} else {
tss[index] = domain + "/" + string(line[1:])
}
index++
}
return tss, nil
}
func download(task *Task, savePath string) *ErrorDownload {
resp, err := http.Get(task.url)
if err != nil {
return &ErrorDownload{
Task: task,
Error: err,
}
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &ErrorDownload{
Task: task,
Error: err,
}
}
ext := path.Ext(task.url)
join := filepath.Join(savePath, fmt.Sprintf("%d%s", task.NO, ext))
out, err := os.Create(join)
if err != nil {
return &ErrorDownload{
Task: task,
Error: err,
}
}
defer out.Close()
size, err := io.Copy(out, bytes.NewReader(data))
if err != nil {
return &ErrorDownload{
Task: task,
Error: err,
}
}
log.Println("URL:[", task.url, "] 下载完成,文件大小[", size, "]字节")
return nil
}
func Download(domain, srcFile string) {
tss, err := readFile(domain, srcFile)
if err != nil {
panic(err)
}
var group sync.WaitGroup
var taskChan = make(chan *Task, 8)
var errorChan = make(chan *ErrorDownload, len(tss))
for i := 0; i < 8; i++ {
group.Add(1)
go func() {
defer group.Done()
for {
url, ok := <-taskChan
if !ok {
return
}
err := download(url, save)
if nil != err {
errorChan <- err
}
}
}()
}
for k, v := range tss {
taskChan <- &Task{
NO: k,
url: v,
}
}
close(taskChan)
group.Wait()
close(errorChan)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/bambuo/downloader.git
git@gitee.com:bambuo/downloader.git
bambuo
downloader
downloader
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385