代码拉取完成,页面将自动刷新
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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。