-
Notifications
You must be signed in to change notification settings - Fork 1
/
fn.go
84 lines (74 loc) · 1.7 KB
/
fn.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
package main
import (
"errors"
"strings"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
// raw url
const rawURL = "http://jmcomic.xyz"
// match key word
const matchKey = "18comic"
// TODO:
// 1. use mem cache
// 2. use http server
// WriteCache WRITE mem cache
func WriteCache() error {
return nil
}
// CleanCache CLEAN mem cache
func CleanCache() error {
return nil
}
// ReadCache READ mem cache
func ReadCache() error {
return nil
}
// MirrorItem single mirror item
type MirrorItem struct {
Title string // comic mirror title
API string // comic mirror raw api
InChina bool // 中国镜像站
}
// GetMirror get 18comic mirror
func GetMirror() ([]MirrorItem, error) {
dom, err := goquery.NewDocument(rawURL)
if err != nil {
return []MirrorItem{}, errors.New("get raw url data is error")
}
var strongs = dom.Find("strong")
var nodes = strongs.Nodes
var matchNodes = []*html.Node{}
for _, item := range nodes {
var ele = goquery.NewDocumentFromNode(item)
var text = ele.Text()
if strings.Contains(text, matchKey) {
matchNodes = append(matchNodes, item)
}
}
var result = []MirrorItem{}
for _, item := range matchNodes {
var temp = goquery.NewDocumentFromNode(item)
var title string
var data = temp.Text()
var checkNext = strings.Contains(data, matchKey)
var eleParent = temp.Parent()
var ele = eleParent.Prev()
title = ele.Text()
if title == "" {
title = ele.Parent().Text()
}
title = strings.ReplaceAll(title, data, "")
var findIndex = strings.Index(title, "18comic")
if findIndex >= 0 {
title = title[0:findIndex]
}
if checkNext {
result = append(result, MirrorItem{
Title: title,
API: data,
})
}
}
return result, nil
}