This repository has been archived by the owner on Oct 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
article.go
266 lines (216 loc) · 5.07 KB
/
article.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package swan
import (
"bytes"
"fmt"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/andybalholm/cascadia"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// Article is a fully extracted and cleaned document.
type Article struct {
// Final URL after all redirects
URL string
// Newline-separated and cleaned content
CleanedText string
// Node from which CleanedText was created. Call .Html() on this to get
// printable HTML.
TopNode *goquery.Selection
// A header image to use for the article. Nil if no image could be
// detected.
Img *Image
// All metadata associated with the original document
Meta struct {
Authors []string
Canonical string
Description string
Domain string
Favicon string
Keywords string
Links []string
Lang string
OpenGraph map[string]string
PublishDate string
Tags []string
Title string
}
// Full document backing this article
Doc *goquery.Document
// For use resolving URLs in the document
baseURL *url.URL
// Caches information about nodes so that it doesn't have to be updated
cCache map[*html.Node]*contentCache
// Scores that have been calculated
scores map[*html.Node]int
}
// Image contains information about the header image associated with an
// article
type Image struct {
Src string
Width uint
Height uint
Bytes int64
Confidence uint
Sel *goquery.Selection
}
type contentCache struct {
text string
wordCount uint
stopwords uint
highLinkDensity bool
s *goquery.Selection
}
type runner interface {
run(a *Article) error
}
type processor struct {
probe func(a *Article) uint
runners []runner
}
type useKnownArticles struct{}
const (
imgHeader = `<p class="image-container" style="text-align: center;">` +
`<a href="%s"><img title="%s" src="%s"/></a>` +
`</p>`
)
var (
baseRunners = []runner{
extractMetas{},
extractAuthors{},
extractPublishDate{},
extractTags{},
extractTitle{},
precleanup{},
metaDetectLanguage{},
useKnownArticles{},
cleanup{},
}
defaultProcessor = &processor{
probe: func(a *Article) uint {
return 1
},
runners: []runner{
extractTopNode{},
extractLinks{},
extractImages{},
extractVideos{},
// Does more document mangling and TopNode resetting
extractContent{},
},
}
processors = []*processor{
comicProcessor,
// Always last since it's the default
defaultProcessor,
}
// Don't match all-at-once: there's precedence here
knownArticles = []goquery.Matcher{
cascadia.MustCompile("[itemprop=articleBody]"),
cascadia.MustCompile("[itemprop=blogPost]"),
cascadia.MustCompile(".post-content"),
cascadia.MustCompile("article"),
}
)
func (u useKnownArticles) run(a *Article) error {
for _, m := range knownArticles {
for _, n := range a.Doc.FindMatcher(m).Nodes {
cc := a.getCCache(n)
// Sometimes even "known" articles are wrong
if cc.stopwords > 5 && !cc.highLinkDensity {
// Remove from document so that memory can be freed
if n.Parent != nil {
n.Parent.RemoveChild(n)
}
a.Doc = goquery.NewDocumentFromNode(n)
a.TopNode = a.Doc.Selection
return nil
}
}
}
return nil
}
// Checks to see if TopNode is a known article tag that was picked before
// scoring
func (u useKnownArticles) isKnownArticle(a *Article) bool {
for _, m := range knownArticles {
if a.Doc.IsMatcher(m) {
return true
}
}
return false
}
func (a *Article) extract() error {
var p *processor
a.cCache = make(map[*html.Node]*contentCache)
a.scores = make(map[*html.Node]int)
for _, r := range baseRunners {
err := r.run(a)
if err != nil {
return err
}
}
max := uint(0)
for _, pp := range processors {
score := pp.probe(a)
if score > max {
p = pp
max = score
}
}
for _, r := range p.runners {
err := r.run(a)
if err != nil {
return err
}
}
a.cCache = nil
a.scores = nil
a.baseURL = nil
return nil
}
func (a *Article) getCCache(n *html.Node) *contentCache {
cc, ok := a.cCache[n]
if !ok {
s := goquery.NewDocumentFromNode(n).Selection
cc = &contentCache{
text: strings.TrimSpace(s.Text()),
s: s,
}
ws := splitText(cc.text)
cc.wordCount = uint(len(ws))
cc.stopwords = stopwordCountWs(a.Meta.Lang, ws)
cc.highLinkDensity = highLinkDensity(cc)
a.cCache[n] = cc
}
return cc
}
func highLinkDensity(cc *contentCache) bool {
var b bytes.Buffer
links := cc.s.FindMatcher(linkTags)
if links.Size() == 0 {
return false
}
links.Each(func(i int, l *goquery.Selection) {
b.WriteString(l.Text())
})
linkWords := float32(strings.Count(b.String(), " "))
return ((linkWords / float32(cc.wordCount)) * float32(len(cc.s.Nodes))) >= 1
}
func (a *Article) addInlineArticleImageHTML(title string) {
if a.Img == nil {
return
}
if a.TopNode == nil {
a.TopNode = goquery.NewDocumentFromNode(&html.Node{
Type: html.ElementNode,
DataAtom: atom.Span,
Data: "span",
}).Selection
}
a.TopNode.PrependHtml(fmt.Sprintf(imgHeader,
html.EscapeString(a.URL),
html.EscapeString(title),
html.EscapeString(a.Img.Src)))
}