go-crawler-anime/src/cmd/start.go

66 lines
1.3 KiB
Go

package cmd
import (
"bufio"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"sync"
)
var wg sync.WaitGroup
func start() {
err := IfPathNotExistDoMkdir(output)
if err != nil {
fmt.Println("输出目录创建失败")
return
}
for i := 1; i <= max; i++ {
wg.Add(1)
go get(i)
}
wg.Wait()
}
func get(num int) {
defer wg.Done()
// 兼容未携带斜杆的地址
if url[0] != '/' {
url = "/" + url
}
urlSlice := strings.Split(url, "_") // 取URL组装
fileSlice := strings.Split(urlSlice[1], ".") // 取后缀名
fileName := strconv.Itoa(num) + "." + fileSlice[1] // 组装文件名
imgUrl := host + urlSlice[0] + "_" + fmt.Sprintf("%02d", num) + "." + fileSlice[1] // 组装图片URL
fmt.Println("获取图片:" + imgUrl)
resp, err := http.Get(imgUrl)
if err != nil {
fmt.Println(fileName, "图片获取失败:", err)
return
}
defer resp.Body.Close()
reader := bufio.NewReaderSize(resp.Body, 32*1024)
file, err := os.Create(output + "/" + fileName)
if err != nil {
fmt.Println(fileName, "图片创建失败:", err)
return
}
defer file.Close()
_, err = io.Copy(file, reader)
if err != nil {
fmt.Println(fileName, "图片输出失败:", err)
return
}
}