去除公共配置变量,封装执行逻辑

master
fantasticbin 6 months ago
parent 479b1c7cd7
commit f496801c4d

@ -10,21 +10,14 @@ import (
"strings" "strings"
) )
var ( func Execute() {
url string rootCmd := NewRootCmd()
folderTitleUrl string CommandInit(rootCmd)
max int
output string
chapter int
host = "https://img4.qy0.ru"
)
func init() { if err := rootCmd.Execute(); err != nil {
rootCmd.PersistentFlags().StringVarP(&url, "url", "u", "", "除域名外的链接") fmt.Println(err)
rootCmd.PersistentFlags().IntVarP(&max, "max", "m", 1, "图片最大值") os.Exit(1)
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "anime", "设置漫画抓取结果的保存位置,默认为当前用户的主目录下的 anime 文件夹") }
rootCmd.PersistentFlags().StringVarP(&folderTitleUrl, "folderTitleUrl", "t", "", "文件夹标题抓取链接")
rootCmd.PersistentFlags().IntVarP(&chapter, "chapter", "c", 1, "指定章节,用于文件夹标题抓取")
} }
func IfPathNotExistDoMkdir(path string) error { func IfPathNotExistDoMkdir(path string) error {
@ -39,13 +32,6 @@ func IfPathNotExistDoMkdir(path string) error {
return nil return nil
} }
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// FindChapterTitle 解析文件夹标题 // FindChapterTitle 解析文件夹标题
func FindChapterTitle(url string) string { func FindChapterTitle(url string) string {
resp, err := http.Get(url) resp, err := http.Get(url)
@ -68,7 +54,7 @@ func FindChapterTitle(url string) string {
return "" return ""
} }
re := regexp.MustCompile(`\\u7b2c` + strconv.Itoa(chapter) + `\\u8bdd (.+?)"`) re := regexp.MustCompile(`\\u7b2c` + strconv.Itoa(config.chapter) + `\\u8bdd (.+?)"`)
matches := re.FindAllStringSubmatch(string(body), -1) matches := re.FindAllStringSubmatch(string(body), -1)
if len(matches) == 0 { if len(matches) == 0 {

@ -5,11 +5,32 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var rootCmd = &cobra.Command{ type Config struct {
Use: "anime", url string
Short: "Anime crawler written by go", folderTitleUrl string
Run: func(cmd *cobra.Command, args []string) { max int
NewCrawler().Start() output string
fmt.Println("抓取完成") 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, "指定章节,用于文件夹标题抓取")
} }

@ -22,16 +22,16 @@ func NewCrawler() *Crawler {
} }
func (c *Crawler) Start() { func (c *Crawler) Start() {
c.path = output c.path = config.output
if folderTitleUrl != "" { if config.folderTitleUrl != "" {
chapterTitle := FindChapterTitle(folderTitleUrl) chapterTitle := FindChapterTitle(config.folderTitleUrl)
title := strings.Join([]string{ title := strings.Join([]string{
"第", "第",
strconv.Itoa(chapter), strconv.Itoa(config.chapter),
"话-", "话-",
chapterTitle, chapterTitle,
}, "") }, "")
c.path = filepath.Join(output, title) // 组装章节路径 c.path = filepath.Join(config.output, title) // 组装章节路径
} }
err := IfPathNotExistDoMkdir(c.path) err := IfPathNotExistDoMkdir(c.path)
@ -40,8 +40,8 @@ func (c *Crawler) Start() {
return return
} }
c.wg.Add(max) c.wg.Add(config.max)
for i := 1; i <= max; i++ { for i := 1; i <= config.max; i++ {
go c.do(i) go c.do(i)
} }
@ -52,12 +52,12 @@ func (c *Crawler) do(num int) {
defer c.wg.Done() defer c.wg.Done()
// 兼容未携带斜杆的地址 // 兼容未携带斜杆的地址
if url[0] != '/' { if config.url[0] != '/' {
url = "/" + url config.url = "/" + config.url
} }
numString := strconv.Itoa(num) numString := strconv.Itoa(num)
urlSlice := strings.Split(url, "_") // 取URL组装 urlSlice := strings.Split(config.url, "_") // 取URL组装
fileSlice := strings.Split(urlSlice[1], ".") // 取后缀名 fileSlice := strings.Split(urlSlice[1], ".") // 取后缀名
fileName := strings.Join([]string{ // 组装文件名 fileName := strings.Join([]string{ // 组装文件名
numString, numString,
@ -70,7 +70,7 @@ func (c *Crawler) do(num int) {
} }
imgUrl := strings.Join([]string{ // 组装图片URL imgUrl := strings.Join([]string{ // 组装图片URL
host, config.host,
urlSlice[0], urlSlice[0],
"_", "_",
urlNum, urlNum,

Loading…
Cancel
Save