-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.go
280 lines (250 loc) · 6.39 KB
/
html.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"regexp"
"strconv"
"strings"
"time"
"golang.org/x/net/html"
"golang.org/x/net/html/charset"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/transform"
)
const (
RecursionLimit = 1000
)
var playerResponsePattern = regexp.MustCompile(`var ytInitialPlayerResponse\s=\s(\{.+?\});`)
/*
* Kudos to https://siongui.github.io/
* https://siongui.github.io/2018/10/27/auto-detect-and-convert-html-encoding-to-utf8-in-go/
* https://siongui.github.io/2016/05/10/go-get-html-title-via-net-html/
*/
type filterFunc func(n *html.Node) bool
type extractFunc func(n *html.Node) (string, bool)
func isTitleElement(n *html.Node) bool {
return n.Type == html.ElementNode && n.Data == "title"
}
func defaultExtractor(n *html.Node) (string, bool) {
if n.FirstChild != nil {
return strings.TrimSpace(n.FirstChild.Data), true
}
return "", false
}
func twitchExtractor(n *html.Node) (string, bool) {
if n == nil {
return "", false
}
for _, attr := range n.Attr {
if attr.Key == "content" {
return strings.TrimSpace(attr.Val), true
}
}
return "", false
}
func tgExtractor(n *html.Node) (string, bool) {
if n == nil {
return "", false
}
return n.FirstChild.Data, true
}
func isTGElement(n *html.Node) bool {
if n.Type == html.ElementNode && n.Data == "div" {
for _, attr := range n.Attr {
if attr.Key == "class" && attr.Val == "tgme_widget_message_text js-message_text" {
return true
}
}
}
return false
}
func extractTGLastPost(data io.Reader) (quote string, err error) {
tree, err := html.Parse(data)
if err != nil {
return
}
quote, ok := revTraverse(tree, 0, isTGElement, tgExtractor)
if !ok {
return "", errors.New("failed to extract post")
}
return
}
func isTwitchElement(n *html.Node) bool {
if n.Type == html.ElementNode && n.Data == "meta" {
for _, attr := range n.Attr {
if attr.Key == "property" && attr.Val == "og:title" {
return true
}
}
}
return false
}
func traverse(n *html.Node, depth uint, filter filterFunc, extractor extractFunc) (string, bool) {
depth++
if depth == RecursionLimit {
return "", false
}
if filter(n) {
return extractor(n)
}
for child := n.FirstChild; child != nil; child = child.NextSibling {
result, ok := traverse(child, depth, filter, extractor)
if ok {
return result, ok
}
}
return "", false
}
func revTraverse(n *html.Node, depth uint, filter filterFunc, extractor extractFunc) (string, bool) {
depth++
if depth == RecursionLimit {
return "", false
}
if filter(n) {
return extractor(n)
}
for child := n.LastChild; child != nil; child = child.PrevSibling {
result, ok := revTraverse(child, depth, filter, extractor)
if ok {
return result, ok
}
}
return "", false
}
func extractTitle(data io.Reader, utf8 bool) (title string, err error) {
if !utf8 {
data, err = htmlToUTF8(data)
if err != nil {
return
}
}
tree, err := html.Parse(data)
if err != nil {
return
}
title, ok := traverse(tree, 0, isTitleElement, defaultExtractor)
if !ok {
return "", errors.New("failed to find title")
}
if strings.HasSuffix(title, " YouTube") {
title = extractYoutube(tree, title)
}
switch title {
case "Twitch":
title, ok = traverse(tree, 0, isTwitchElement, twitchExtractor)
if !ok {
title = "Twitch"
}
}
return
}
func isYoutubeElement(n *html.Node) bool {
if n.Type == html.ElementNode && n.Data == "script" && n != nil && n.FirstChild != nil {
return strings.Contains(n.FirstChild.Data, "var ytInitialPlayerResponse =")
}
return false
}
func formatTime(input string) string {
duration, err := time.ParseDuration(fmt.Sprintf("%ss", input))
if err != nil {
return "N/A"
}
return duration.String()
}
func formatDate(input string) string {
splitted := strings.Split(input, "-")
if len(splitted) != 3 {
return input
}
return fmt.Sprintf("%s.%s.%s", splitted[2], splitted[1], splitted[0])
}
func formatViews(input string) string {
if len(input) < 4 {
return input
}
out, err := strconv.ParseUint(input, 10, 64)
if err != nil {
return "N/A"
}
p := message.NewPrinter(language.Ukrainian)
return p.Sprintf("%d", out)
}
func formatYoutubeTitle(data *playerResponseData) string {
author := data.VideoDetails.Author
title := data.VideoDetails.Title
viewCount := formatViews(data.VideoDetails.ViewCount)
length := formatTime(data.Microformat.PlayerMicroformatRenderer.LengthSeconds)
date := formatDate(data.Microformat.PlayerMicroformatRenderer.PublishDate)
return fmt.Sprintf("%s \x0310©\x03 %s \x0310|\x03 %s \x0310|\x03 %s \x0310|\x03 \x0312V:\x03 %s\n", title, author, date, length, viewCount)
}
func youtubeExtractor(n *html.Node) (string, bool) {
if n != nil {
data := playerResponsePattern.FindSubmatch([]byte(n.FirstChild.Data))
if data == nil || len(data) < 2 {
return "", false
}
var prData playerResponseData
err := json.Unmarshal(data[1], &prData)
if err != nil {
return "", false
}
return formatYoutubeTitle(&prData), true
}
return "", false
}
func extractYoutube(n *html.Node, fallback string) string {
title, ok := traverse(n, 0, isYoutubeElement, youtubeExtractor)
if !ok {
return fallback
}
return title
}
func htmlToUTF8(data io.Reader) (result io.Reader, err error) {
b, err := ioutil.ReadAll(data)
if err != nil {
return nil, err
}
encoding, _, _, err := determineEncoding(bytes.NewReader(b))
if err != nil {
return nil, err
}
result = transform.NewReader(bytes.NewReader(b), encoding.NewDecoder())
return
}
func determineEncoding(data io.Reader) (e encoding.Encoding, name string, certain bool, err error) {
b, err := bufio.NewReader(data).Peek(1024)
if err != nil {
return
}
e, name, certain = charset.DetermineEncoding(b, "")
/* hack for websites like interfax.ru which don't conform to
* HTML standard and put their Content-Type tag beyond 1024 bytes
* https://html.spec.whatwg.org/multipage/parsing.html#determining-the-character-encoding
*/
if e == charmap.Windows1252 && !certain {
e = charmap.Windows1251
}
return
}
func checkContentType(header string, contentType string) (ok bool, utf8 bool) {
header = strings.TrimSpace(header)
values := strings.Split(header, ";")
for _, value := range values {
value = strings.TrimSpace(strings.ToLower(value))
switch value {
case contentType:
ok = true
case "charset=utf-8":
utf8 = true
}
}
return
}