将解析文件夹标题的函数移入lib

master
fantasticbin 6 months ago
parent 91bcd18eff
commit 83c5892641

@ -2,7 +2,12 @@ package cmd
import ( import (
"fmt" "fmt"
"io"
"net/http"
"os" "os"
"regexp"
"strconv"
"strings"
) )
var ( var (
@ -40,3 +45,51 @@ func Execute() {
os.Exit(1) 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
}

@ -7,7 +7,6 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -19,7 +18,7 @@ var wg sync.WaitGroup
func start() { func start() {
path := output path := output
if folderTitleUrl != "" { if folderTitleUrl != "" {
chapterTitle := findChapterTitle(folderTitleUrl, chapter) chapterTitle := FindChapterTitle(folderTitleUrl, chapter)
title := strings.Join([]string{ title := strings.Join([]string{
"第", "第",
strconv.Itoa(chapter), strconv.Itoa(chapter),
@ -108,51 +107,3 @@ func get(num int, path string) {
return 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
}

Loading…
Cancel
Save