-
Notifications
You must be signed in to change notification settings - Fork 4
/
baseline.go
152 lines (132 loc) · 3.65 KB
/
baseline.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
package trafilatura
import (
"encoding/json"
"strings"
"unicode/utf8"
"github.com/go-shiori/dom"
"github.com/markusmobius/go-trafilatura/internal/etree"
"golang.org/x/net/html"
)
var basicCleaningSelector = strings.Join([]string{
`aside`,
`footer`,
`div[id*="footer"]`,
`div[class*="footer"]`,
`script`,
`style`,
}, ", ")
func basicCleaning(doc *html.Node) *html.Node {
discardedElements := dom.QuerySelectorAll(doc, basicCleaningSelector)
for i := len(discardedElements) - 1; i >= 0; i-- {
discardedElements[i].Parent.RemoveChild(discardedElements[i])
}
return doc
}
// baseline uses baseline extraction function targeting text paragraphs and/or JSON metadata.
func baseline(doc *html.Node) (*html.Node, string) {
var tmpText string
postBody := etree.Element("body")
if doc == nil {
return postBody, ""
}
// Scrape JSON+LD for article body
for _, script := range dom.QuerySelectorAll(doc, `script[type="application/ld+json"]`) {
// Get the json text inside the script
jsonLdText := dom.TextContent(script)
jsonLdText = strings.TrimSpace(jsonLdText)
jsonLdText = html.UnescapeString(jsonLdText)
if jsonLdText == "" {
continue
}
// Decode JSON text, assuming it is an object
data := map[string]any{}
err := json.Unmarshal([]byte(jsonLdText), &data)
if err != nil {
continue
}
// Find article body recursively
var articleBody string
var findArticleBody func(obj map[string]any)
findArticleBody = func(obj map[string]any) {
for key, value := range obj {
switch v := value.(type) {
case string:
v = trim(v)
if strings.ToLower(key) == "articlebody" && v != "" {
if strings.Contains(v, "<p>") {
tmp := dom.CreateElement("div")
dom.SetInnerHTML(tmp, v)
articleBody = trim(dom.TextContent(tmp))
} else {
articleBody = v
}
return
}
case map[string]any:
findArticleBody(v)
case []any:
for _, item := range v {
if obj, isObject := item.(map[string]any); isObject {
findArticleBody(obj)
}
}
}
}
}
findArticleBody(data)
if articleBody != "" {
p := etree.SubElement(postBody, "p")
etree.SetText(p, articleBody)
tmpText += " " + articleBody
}
}
tmpText = trim(tmpText)
if utf8.RuneCountInString(tmpText) > 100 {
return postBody, tmpText
}
// Basic tree cleaning
doc = basicCleaning(doc)
// Scrape from article tag
articleElement := dom.QuerySelector(doc, "article")
if articleElement != nil {
articleText := trim(dom.TextContent(articleElement))
if utf8.RuneCountInString(articleText) > 100 {
p := etree.SubElement(postBody, "p")
etree.SetText(p, articleText)
tmpText += " " + articleText
}
}
if len(dom.Children(postBody)) > 0 {
tmpText = trim(tmpText)
return postBody, tmpText
}
// Scrape from text paragraphs
results := make(map[string]struct{})
for _, element := range etree.Iter(doc, "blockquote", "pre", "q", "code", "p") {
entry := trim(dom.TextContent(element))
if _, exist := results[entry]; !exist {
p := etree.SubElement(postBody, "p")
etree.SetText(p, entry)
tmpText += " " + entry
results[entry] = struct{}{}
}
}
tmpText = trim(tmpText)
if utf8.RuneCountInString(tmpText) > 100 {
return postBody, tmpText
}
// Default strategy: clean the tree and take everything
if body := dom.QuerySelector(doc, "body"); body != nil {
text := trim(etree.IterText(body, "\n"))
if utf8.RuneCountInString(text) > 100 {
elem := etree.SubElement(postBody, "p")
etree.SetText(elem, text)
return postBody, text
}
}
// New fallback
text := trim(dom.TextContent(doc))
elem := etree.SubElement(postBody, "p")
etree.SetText(elem, text)
return postBody, text
}