From f496801c4dd2d60c00c7e12a867cb1f5fe8b9c04 Mon Sep 17 00:00:00 2001 From: fantasticbin Date: Thu, 4 Apr 2024 09:18:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E5=85=AC=E5=85=B1=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=8F=98=E9=87=8F=EF=BC=8C=E5=B0=81=E8=A3=85=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/lib.go | 30 ++++++++---------------------- src/cmd/root.go | 35 ++++++++++++++++++++++++++++------- src/cmd/start.go | 22 +++++++++++----------- 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/cmd/lib.go b/src/cmd/lib.go index 081c73f..1e6059b 100644 --- a/src/cmd/lib.go +++ b/src/cmd/lib.go @@ -10,21 +10,14 @@ import ( "strings" ) -var ( - url string - folderTitleUrl string - max int - output string - chapter int - host = "https://img4.qy0.ru" -) +func Execute() { + rootCmd := NewRootCmd() + CommandInit(rootCmd) -func init() { - rootCmd.PersistentFlags().StringVarP(&url, "url", "u", "", "除域名外的链接") - rootCmd.PersistentFlags().IntVarP(&max, "max", "m", 1, "图片最大值") - rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "anime", "设置漫画抓取结果的保存位置,默认为当前用户的主目录下的 anime 文件夹") - rootCmd.PersistentFlags().StringVarP(&folderTitleUrl, "folderTitleUrl", "t", "", "文件夹标题抓取链接") - rootCmd.PersistentFlags().IntVarP(&chapter, "chapter", "c", 1, "指定章节,用于文件夹标题抓取") + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } } func IfPathNotExistDoMkdir(path string) error { @@ -39,13 +32,6 @@ func IfPathNotExistDoMkdir(path string) error { return nil } -func Execute() { - if err := rootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) - } -} - // FindChapterTitle 解析文件夹标题 func FindChapterTitle(url string) string { resp, err := http.Get(url) @@ -68,7 +54,7 @@ func FindChapterTitle(url string) string { return "" } - re := regexp.MustCompile(`\\u7b2c` + strconv.Itoa(chapter) + `\\u8bdd (.+?)"`) + re := regexp.MustCompile(`\\u7b2c` + strconv.Itoa(config.chapter) + `\\u8bdd (.+?)"`) matches := re.FindAllStringSubmatch(string(body), -1) if len(matches) == 0 { diff --git a/src/cmd/root.go b/src/cmd/root.go index 8a6ff77..056c1e6 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -5,11 +5,32 @@ import ( "github.com/spf13/cobra" ) -var rootCmd = &cobra.Command{ - Use: "anime", - Short: "Anime crawler written by go", - Run: func(cmd *cobra.Command, args []string) { - NewCrawler().Start() - fmt.Println("抓取完成") - }, +type Config struct { + url string + folderTitleUrl string + max int + output string + chapter int + host string +} + +var config = Config{host: "https://img4.qy0.ru"} + +func NewRootCmd() *cobra.Command { + return &cobra.Command{ + Use: "anime", + Short: "Anime crawler written by go", + Run: func(cmd *cobra.Command, args []string) { + NewCrawler().Start() + fmt.Println("抓取完成") + }, + } +} + +func CommandInit(rootCmd *cobra.Command) { + rootCmd.PersistentFlags().StringVarP(&config.url, "url", "u", "", "除域名外的链接") + rootCmd.PersistentFlags().IntVarP(&config.max, "max", "m", 1, "图片最大值") + rootCmd.PersistentFlags().StringVarP(&config.output, "output", "o", "anime", "设置漫画抓取结果的保存位置,默认为当前用户的主目录下的 anime 文件夹") + rootCmd.PersistentFlags().StringVarP(&config.folderTitleUrl, "folderTitleUrl", "t", "", "文件夹标题抓取链接") + rootCmd.PersistentFlags().IntVarP(&config.chapter, "chapter", "c", 1, "指定章节,用于文件夹标题抓取") } diff --git a/src/cmd/start.go b/src/cmd/start.go index 3c2903d..2549dc0 100644 --- a/src/cmd/start.go +++ b/src/cmd/start.go @@ -22,16 +22,16 @@ func NewCrawler() *Crawler { } func (c *Crawler) Start() { - c.path = output - if folderTitleUrl != "" { - chapterTitle := FindChapterTitle(folderTitleUrl) + c.path = config.output + if config.folderTitleUrl != "" { + chapterTitle := FindChapterTitle(config.folderTitleUrl) title := strings.Join([]string{ "第", - strconv.Itoa(chapter), + strconv.Itoa(config.chapter), "话-", chapterTitle, }, "") - c.path = filepath.Join(output, title) // 组装章节路径 + c.path = filepath.Join(config.output, title) // 组装章节路径 } err := IfPathNotExistDoMkdir(c.path) @@ -40,8 +40,8 @@ func (c *Crawler) Start() { return } - c.wg.Add(max) - for i := 1; i <= max; i++ { + c.wg.Add(config.max) + for i := 1; i <= config.max; i++ { go c.do(i) } @@ -52,12 +52,12 @@ func (c *Crawler) do(num int) { defer c.wg.Done() // 兼容未携带斜杆的地址 - if url[0] != '/' { - url = "/" + url + if config.url[0] != '/' { + config.url = "/" + config.url } numString := strconv.Itoa(num) - urlSlice := strings.Split(url, "_") // 取URL组装 + urlSlice := strings.Split(config.url, "_") // 取URL组装 fileSlice := strings.Split(urlSlice[1], ".") // 取后缀名 fileName := strings.Join([]string{ // 组装文件名 numString, @@ -70,7 +70,7 @@ func (c *Crawler) do(num int) { } imgUrl := strings.Join([]string{ // 组装图片URL - host, + config.host, urlSlice[0], "_", urlNum,