-
Notifications
You must be signed in to change notification settings - Fork 124
/
t2cycomspider.go
117 lines (98 loc) · 3.1 KB
/
t2cycomspider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package localimagespider
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/yqchilde/wxbot/engine/pkg/log"
"github.com/yqchilde/wxbot/plugins/localimage"
)
const (
PageInfoFile = "pageinfo"
T2cyComUrl = "https://t2cy.com"
CosplayUri = "/acg/cos"
CoserUri = "/acg/daily"
PagerTemplate = "/index_%v.html"
)
func crawlCoser(storageFolder string) {
crawl(filepath.Join(storageFolder, "coser"), CoserUri, "ul[class=\"cy_cosList clr\"]")
}
func crawlCosplay(storageFolder string) {
crawl(filepath.Join(storageFolder, "cosplay"), CosplayUri, "ul[class=\"cy2-coslist clr\"]")
}
func crawl(storageFolderPath, uri, containerSelector string) {
if !localimage.Exist(storageFolderPath) && !localimage.MakeDir(storageFolderPath) {
return
}
currentPageNum, err := getCurrentPageNum(storageFolderPath)
if err != nil {
return
}
currentPageNum += 1
url := T2cyComUrl + uri
if currentPageNum > 1 {
url += fmt.Sprintf(PagerTemplate, currentPageNum)
}
linkMap := GetTextLinkInContainer(url, containerSelector)
if len(linkMap) == 0 {
// 为空说明当前分类爬完了,重置页码,这样新增数据之后,可以重新爬到
localimage.WriteFile(filepath.Join(storageFolderPath, PageInfoFile), "0")
return
}
dirEntries, err := localimage.GetSubFolder(storageFolderPath)
if err != nil {
return
}
existTopicMap := convert(dirEntries)
for link, title := range linkMap {
strs := strings.Split(link, "/")
topicId := strings.TrimSuffix(strs[len(strs)-1], ".html")
// TODO 这样不严谨,进一步应该比较一下元素数量
if existTopicMap[topicId] != "" {
continue
}
replaces := []string{"*", "_", ".", "_", "\"", "_", "/", "_", "\"", "_", "[", "_", "]", "_", ":", "_", ";", "_", "|", "_", ",", "_"}
title = strings.NewReplacer(replaces...).Replace(title)
topicFolderPath := filepath.Join(storageFolderPath, topicId+"-"+title)
if !localimage.MakeDir(topicFolderPath) {
return
}
crawlTopic(T2cyComUrl+link, topicFolderPath)
}
localimage.WriteFile(filepath.Join(storageFolderPath, PageInfoFile), strconv.Itoa(currentPageNum))
}
func convert(dirEntries []os.DirEntry) map[string]string {
existTopicMap := make(map[string]string)
for _, entry := range dirEntries {
topicInfo := strings.Split(entry.Name(), "-")
if len(topicInfo) != 2 {
continue
}
existTopicMap[topicInfo[0]] = topicInfo[1]
}
return existTopicMap
}
func crawlTopic(link, topicFolderPath string) {
imageLinks := GetImageLinkInContainer(link, "div[class=\"w maxImg tc\"]")
for _, imageLink := range imageLinks {
DownloadImage(T2cyComUrl+imageLink, link, topicFolderPath)
}
}
func getCurrentPageNum(folderPath string) (int, error) {
pageInfoFilePath := filepath.Join(folderPath, PageInfoFile)
if !localimage.Exist(pageInfoFilePath) {
return 0, nil
}
bytes, err := os.ReadFile(pageInfoFilePath)
if err != nil {
log.Errorf("读取文件 %s 失败, err: %v", pageInfoFilePath, err)
return 0, err
}
pageNum, err := strconv.Atoi(string(bytes))
if err != nil {
log.Errorf("文件 %s 内容非数字, err: %v", pageInfoFilePath, err)
return 0, nil
}
return pageNum, nil
}