From 83c58926418c62a7b845e2b8697c5fb35147bdd2 Mon Sep 17 00:00:00 2001 From: fantasticbin Date: Wed, 3 Apr 2024 23:02:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E8=A7=A3=E6=9E=90=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9=E6=A0=87=E9=A2=98=E7=9A=84=E5=87=BD=E6=95=B0=E7=A7=BB?= =?UTF-8?q?=E5=85=A5lib?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cmd/lib.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ src/cmd/start.go | 51 +--------------------------------------------- 2 files changed, 54 insertions(+), 50 deletions(-) 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 -}