-
Notifications
You must be signed in to change notification settings - Fork 0
/
novel.go
126 lines (104 loc) · 2.37 KB
/
novel.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
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"log"
"net/url"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
// Page info
type Page struct {
Title string
URL string
}
// Novel info
type Novel struct {
Title string
Content string
}
// NovelURL send php request
type NovelURL struct {
id string
qid string
}
// GetPages 获取分页
func GetPages(url string) []Page {
doc, err := Curl(url)
if err != nil {
log.Fatal(err)
}
return ParsePages(doc)
}
// GetNovel 获取novel
func GetNovel(url string) []Novel {
doc, err := Curl(url)
if err != nil {
log.Fatal(err)
}
return ParseNovel(doc)
}
// Split split sep by / or .
func Split(r rune) bool {
return r == '/' || r == '.'
}
// GetNovelByParam 通过 param 获取novel
func GetNovelByParam(urlString string) (novels []Novel) {
u, err := url.Parse(urlString)
if err != nil {
panic(err)
}
tmpArr := strings.FieldsFunc(u.Path, Split)
BaseFMT := config.URL + config.PhpURL
baseURL := fmt.Sprintf(BaseFMT, tmpArr[2], tmpArr[3])
println(baseURL)
resText, err := CurlText(baseURL)
if err != nil {
log.Fatal(err)
}
//resText = DecodeGBK(resText)
resText = strings.Replace(resText, ` `, ` `, -1)
var re *regexp.Regexp
//去除br
re, _ = regexp.Compile("<br>")
resText = re.ReplaceAllString(resText, "")
re, _ = regexp.Compile("<br />")
resText = re.ReplaceAllString(resText, "\n")
//去除viewall
re, _ = regexp.Compile("<font[\\S\\s]+?</font>")
resText = re.ReplaceAllString(resText, "")
novel := Novel{
Title: "",
Content: resText,
}
novels = append(novels, novel)
return
}
// ParsePages 分析分页
func ParsePages(doc *goquery.Document) (pages []Page) {
doc.Find("#list > dl > dd > a").Each(func(i int, s *goquery.Selection) {
page := DecodeGBK(s.Text())
url, _ := s.Attr("href")
pages = append(pages, Page{
Title: page,
URL: url,
})
})
return pages
}
// ParseNovel 分析novel数据
func ParseNovel(doc *goquery.Document) (novels []Novel) {
doc.Find("#BookCon").Each(func(i int, s *goquery.Selection) {
title := s.Find("h1").Text()
title = DecodeGBK(title)
subtitle := strings.Split(title, " ")
content := DecodeGBK(s.Find("#pagecontent").Text())
content = strings.Replace(content, `聽聽聽聽`, ` `, -1)
novel := Novel{
Title: subtitle[0] + " " + subtitle[1],
Content: content,
}
novels = append(novels, novel)
})
return novels
}