去除公共配置变量,封装执行逻辑
This commit is contained in:
parent
479b1c7cd7
commit
f496801c4d
@ -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 {
|
||||
|
@ -5,7 +5,19 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
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) {
|
||||
@ -13,3 +25,12 @@ var rootCmd = &cobra.Command{
|
||||
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() {
|
||||
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,
|
||||
|
Loading…
Reference in New Issue
Block a user