diff --git a/src/cmd/lib.go b/src/cmd/lib.go index 9af89eb..eaf7b80 100644 --- a/src/cmd/lib.go +++ b/src/cmd/lib.go @@ -2,7 +2,12 @@ package cmd import ( "fmt" + "io" + "net/http" "os" + "regexp" + "strconv" + "strings" ) var ( @@ -40,3 +45,51 @@ func Execute() { os.Exit(1) } } + +// FindChapterTitle 解析文件夹标题 +func FindChapterTitle(url string, num int) string { + resp, err := http.Get(url) + if err != nil { + fmt.Println("请求文件夹标题失败:", err) + return "" + } + + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + fmt.Println("文件夹标题http请求关闭失败:", err) + return + } + }(resp.Body) + + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("文件夹标题读取失败:", err) + return "" + } + + content := string(body) + re := regexp.MustCompile(`\\u7b2c` + strconv.Itoa(num) + `\\u8bdd (.+?)"`) + matches := re.FindAllStringSubmatch(content, -1) + + if len(matches) == 0 { + return "" + } + unquoted, err := strconv.Unquote(`"` + matches[0][1] + `"`) + if err != nil { + fmt.Println("文件夹标题转码失败:", err) + return "" + } + + replacements := map[string]string{ + "?": "?", + ":": ":", + "!": "!", + } + unquoted = strings.TrimSpace(unquoted) + for old, re := range replacements { + unquoted = strings.ReplaceAll(unquoted, old, re) + } + + return unquoted +} diff --git a/src/cmd/start.go b/src/cmd/start.go index 24765d9..028d84d 100644 --- a/src/cmd/start.go +++ b/src/cmd/start.go @@ -7,7 +7,6 @@ import ( "net/http" "os" "path/filepath" - "regexp" "strconv" "strings" "sync" @@ -19,7 +18,7 @@ var wg sync.WaitGroup func start() { path := output if folderTitleUrl != "" { - chapterTitle := findChapterTitle(folderTitleUrl, chapter) + chapterTitle := FindChapterTitle(folderTitleUrl, chapter) title := strings.Join([]string{ "第", strconv.Itoa(chapter), @@ -108,51 +107,3 @@ func get(num int, path string) { return } } - -// findChapterTitle 解析文件夹标题 -func findChapterTitle(url string, num int) string { - resp, err := http.Get(url) - if err != nil { - fmt.Println("请求文件夹标题失败:", err) - return "" - } - - defer func(Body io.ReadCloser) { - err := Body.Close() - if err != nil { - fmt.Println("文件夹标题http请求关闭失败:", err) - return - } - }(resp.Body) - - body, err := io.ReadAll(resp.Body) - if err != nil { - fmt.Println("文件夹标题读取失败:", err) - return "" - } - - content := string(body) - re := regexp.MustCompile(`\\u7b2c` + strconv.Itoa(num) + `\\u8bdd (.+?)"`) - matches := re.FindAllStringSubmatch(content, -1) - - if len(matches) == 0 { - return "" - } - unquoted, err := strconv.Unquote(`"` + matches[0][1] + `"`) - if err != nil { - fmt.Println("文件夹标题转码失败:", err) - return "" - } - - replacements := map[string]string{ - "?": "?", - ":": ":", - "!": "!", - } - unquoted = strings.TrimSpace(unquoted) - for old, re := range replacements { - unquoted = strings.ReplaceAll(unquoted, old, re) - } - - return unquoted -}