This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
page.go
159 lines (131 loc) · 3.16 KB
/
page.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package model
import (
"errors"
"fmt"
"net/http"
"net/url"
"regexp"
"sync"
"github.com/ArtalkJS/ArtalkGo/lib"
"github.com/PuerkitoBio/goquery"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
type Page struct {
gorm.Model
Key string `gorm:"index;size:255"` // 页面 Key(一般为不含 hash/query 的完整 url)
Title string
AdminOnly bool
SiteName string `gorm:"index;size:255"`
_Site Site
_SiteOnce sync.Once
_AccessibleURL string
VoteUp int
VoteDown int
PV int
}
func (p Page) IsEmpty() bool {
return p.ID == 0
}
type CookedPage struct {
ID uint `json:"id"`
AdminOnly bool `json:"admin_only"`
Key string `json:"key"`
URL string `json:"url"`
Title string `json:"title"`
SiteName string `json:"site_name"`
VoteUp int `json:"vote_up"`
VoteDown int `json:"vote_down"`
PV int `json:"pv"`
}
func (p Page) ToCooked() CookedPage {
return CookedPage{
ID: p.ID,
AdminOnly: p.AdminOnly,
Key: p.Key,
URL: p.GetAccessibleURL(),
Title: p.Title,
SiteName: p.SiteName,
VoteUp: p.VoteUp,
VoteDown: p.VoteDown,
PV: p.PV,
}
}
func (p *Page) FetchSite() Site {
if p._Site.IsEmpty() {
p._SiteOnce.Do(func() {
site := FindSite(p.SiteName)
p._Site = site
})
}
return p._Site
}
// 获取可访问链接
func (p *Page) GetAccessibleURL() string {
if p._AccessibleURL == "" {
acURL := p.Key
// 若 pageKey 为相对路径,生成相对于 site.FirstUrl 配置的 URL
if !lib.ValidateURL(p.Key) {
u1, e1 := url.Parse(p.FetchSite().ToCooked().FirstUrl)
u2, e2 := url.Parse(p.Key)
if e1 == nil && e2 == nil {
acURL = u1.ResolveReference(u2).String()
}
}
p._AccessibleURL = acURL
}
return p._AccessibleURL
}
func (p *Page) FetchURL() error {
cookedPage := p.ToCooked()
url := cookedPage.URL
if url == "" {
return errors.New("URL is null")
}
// 获取 URL 页面 title
title, err := GetTitleByURL(url)
if err == nil && title != "" {
p.Title = title
}
if err := UpdatePage(p); err != nil {
logrus.Error("FetchURL 保存失败")
return err
}
return nil
}
func GetTitleByURL(url string) (string, error) {
if !lib.ValidateURL(url) {
logrus.Error("URL " + url + " is invalid")
return "", errors.New("URL is invalid")
}
// Request the HTML page.
res, err := http.Get(url)
if err != nil {
logrus.Error(err)
return "", err
}
defer res.Body.Close()
if res.StatusCode != 200 {
logrus.Error(fmt.Sprintf("status code error: %d '%s' '%s'", res.StatusCode, res.Status, url))
return "", errors.New("status code error")
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
logrus.Error(err)
return "", err
}
// 读取页面 title
title := doc.Find("title").Text()
// 如果页面有跳转
val, exists := doc.Find(`meta[http-equiv="refresh"]`).Attr("content")
if exists {
urlReg := regexp.MustCompile(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)`)
match := urlReg.FindStringSubmatch(val)
if len(match) > 0 {
redirectURL := match[0]
return GetTitleByURL(redirectURL)
}
}
return title, nil
}