From 4c8838846af51b75a3e2bd92427b4172b1756910 Mon Sep 17 00:00:00 2001 From: fantasticbin Date: Thu, 4 Apr 2024 08:35:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=88=AC=E8=99=AB=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cmd/root.go | 2 +- src/cmd/start.go | 31 ++++++++++++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/cmd/root.go b/src/cmd/root.go index 1392064..8a6ff77 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -9,7 +9,7 @@ var rootCmd = &cobra.Command{ Use: "anime", Short: "Anime crawler written by go", Run: func(cmd *cobra.Command, args []string) { - start() + NewCrawler().Start() fmt.Println("抓取完成") }, } diff --git a/src/cmd/start.go b/src/cmd/start.go index 028d84d..25c7dea 100644 --- a/src/cmd/start.go +++ b/src/cmd/start.go @@ -12,11 +12,17 @@ import ( "sync" ) -var wg sync.WaitGroup +type Crawler struct { + wg sync.WaitGroup + path string +} + +func NewCrawler() *Crawler { + return &Crawler{} +} -// start 开始执行 -func start() { - path := output +func (c *Crawler) Start() { + c.path = output if folderTitleUrl != "" { chapterTitle := FindChapterTitle(folderTitleUrl, chapter) title := strings.Join([]string{ @@ -25,26 +31,25 @@ func start() { "话-", chapterTitle, }, "") - path = filepath.Join(output, title) // 组装章节路径 + c.path = filepath.Join(output, title) // 组装章节路径 } - err := IfPathNotExistDoMkdir(path) + err := IfPathNotExistDoMkdir(c.path) if err != nil { fmt.Println("输出目录创建失败:", err) return } + c.wg.Add(max) for i := 1; i <= max; i++ { - wg.Add(1) - go get(i, path) + go c.do(i) } - wg.Wait() + c.wg.Wait() } -// get 获取漫画图片 -func get(num int, path string) { - defer wg.Done() +func (c *Crawler) do(num int) { + defer c.wg.Done() // 兼容未携带斜杆的地址 if url[0] != '/' { @@ -88,7 +93,7 @@ func get(num int, path string) { }(resp.Body) reader := bufio.NewReaderSize(resp.Body, 32*1024) - file, err := os.Create(path + "/" + fileName) + file, err := os.Create(c.path + "/" + fileName) if err != nil { fmt.Println(fileName, "图片创建失败:", err) return