forked from barsuk/turboamper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turbo.go
386 lines (338 loc) · 8.95 KB
/
turbo.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package turboamper
import (
"bytes"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// Turbo gives you YandexTurbo-representation of html and its type
// If it cannot recognize your html, it returns simple error.
func Turbo(htmlText []byte) ([]byte, string, error) {
got, err := VkToTurbo(htmlText)
if err == nil {
return got, `vkontakte`, nil
}
got, err = FbToTurbo(htmlText)
if err == nil {
return got, `facebook`, nil
}
got, err = InstaToTurbo(htmlText)
if err == nil {
return got, `instagram`, nil
}
got, err = TwitToTurbo(htmlText)
if err == nil {
return got, `twitter`, nil
}
got, err = YoutubeToTurbo(htmlText)
if err == nil {
return got, `youtube`, nil
}
got, err = IframeToTurbo(htmlText)
if err == nil {
return got, `iframe`, nil
}
return nil, ``, fmt.Errorf("unknown embed")
}
// printTurbo returns ready to handle Turbo with given parameters
func (ifrPost *iframePost) printTurbo() []byte {
var attributes string
if ifrPost.Width > 0 {
attributes += fmt.Sprintf(` width="%d"`, ifrPost.Width)
}
if ifrPost.Height > 0 {
attributes += fmt.Sprintf(` height="%d"`, ifrPost.Height)
}
if ifrPost.AllowFS {
attributes += ` allowfullscreen="true"`
}
template := `<iframe%s frameborder="%d" src="%s"></iframe>`
turbo := fmt.Sprintf(template, attributes, ifrPost.Frameborder, ifrPost.Src)
return []byte(turbo)
}
// printTurbo returns ready to handle Turbo with given parameters
func (ypost *youtubePost) printTurbo() []byte {
var attributes string
if ypost.Width > 0 {
attributes += fmt.Sprintf(` width="%d"`, ypost.Width)
}
if ypost.Height > 0 {
attributes += fmt.Sprintf(` height="%d"`, ypost.Height)
}
if ypost.AllowFS {
attributes += ` allowfullscreen="true"`
}
template := `<iframe%s frameborder="%d" src="https://www.youtube.com/embed/%s"></iframe>`
amp := fmt.Sprintf(template, attributes, ypost.Frameborder, ypost.VideoID)
return []byte(amp)
}
// VkToTurbo validates given vkontakte widget post for Yandex Turbo
// What is that? Look https://vk.com/dev/widget_post
func VkToTurbo(htmlText []byte) ([]byte, error) {
if !bytes.Contains(htmlText, []byte(`VK.Widgets.Post`)) {
return nil, fmt.Errorf("given string is not a VK widget post")
}
re := regexp.MustCompile(`VK.Widgets.Post\("vk_post_(-?\d+)_(-?\d+)", (-?\d+), (-?\d+), '(\S+?)'`)
widgetParsed := re.FindSubmatch(htmlText)
if widgetParsed == nil {
return nil, fmt.Errorf("cannot parse vk widget")
}
// 1st and 3rd, 2nd and 4th should match
if string(widgetParsed[1]) != string(widgetParsed[3]) || string(widgetParsed[2]) != string(widgetParsed[4]) {
return nil, fmt.Errorf("parsed string does not match Vk widget post format")
}
_, err := strconv.ParseInt(string(widgetParsed[1]), 10, 0)
if err != nil {
return nil, fmt.Errorf("cannot parse owner id")
}
_, err = strconv.ParseInt(string(widgetParsed[2]), 10, 0)
if err != nil {
return nil, fmt.Errorf("cannot parse post id")
}
return htmlText, nil
}
// TwitToTurbo convertes given twitter embeddable html for Yandex Turbo
func TwitToTurbo(htmlText []byte) ([]byte, error) {
pointerNode, err := html.Parse(bytes.NewReader(htmlText))
if err != nil {
return nil, fmt.Errorf("cannot parse twitter html")
}
var post tweetPost
var f func(*html.Node)
f = func(n *html.Node) {
if n.DataAtom == atom.A {
for _, a := range n.Attr {
if a.Key == "href" {
re := regexp.MustCompile(`https://twitter.com/[a-zA-Z_]{1,15}/status/(\d+)`)
submatch := re.FindStringSubmatch(a.Val)
if submatch == nil {
continue
}
post.ID = submatch[1]
post.Src = a.Val
return
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(pointerNode)
if len(post.Src) < 1 {
return nil, fmt.Errorf("no twitter ID in the url")
}
return htmlText, nil
}
// InstaToTurbo validates given instagram embeddable html for Yandex Turbo
func InstaToTurbo(htmlText []byte) ([]byte, error) {
pointerNode, err := html.Parse(bytes.NewReader(htmlText))
if err != nil {
return nil, fmt.Errorf("cannot parse insta html")
}
var post instaPost
var f func(*html.Node)
f = func(n *html.Node) {
if n.DataAtom == atom.Blockquote {
for _, bq := range n.Attr {
switch bq.Key {
case "data-instgrm-permalink":
post.Src = bq.Val
case "width":
w, err := strconv.ParseInt(bq.Val, 10, 0)
if err == nil {
post.Width = w
}
case "height":
h, err := strconv.ParseInt(bq.Val, 10, 0)
if err == nil {
post.Height = h
}
}
}
if len(post.Src) > 0 {
return
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(pointerNode)
if len(post.Src) < 1 {
return nil, fmt.Errorf("no src in the url")
}
urlPtr, err := url.Parse(post.Src)
if err != nil {
return nil, fmt.Errorf("cannot parse url")
}
if !strings.Contains(urlPtr.Hostname(), "instagram.com") {
return nil, fmt.Errorf("it is not instagram url")
}
re := regexp.MustCompile(`p/(\S+?)/`)
submatch := re.FindStringSubmatch(urlPtr.Path)
if submatch == nil {
return nil, fmt.Errorf("instagram url is malformed")
}
return htmlText, nil
}
// FbToTurbo validates Facebook html for Yandex Turbo
func FbToTurbo(htmlText []byte) ([]byte, error) {
pointerNode, err := html.Parse(bytes.NewReader(htmlText))
if err != nil {
return nil, fmt.Errorf("cannot parse fb iframe")
}
var post fbPost
var f func(*html.Node)
f = func(n *html.Node) {
if n.DataAtom == atom.Iframe {
for _, iframe := range n.Attr {
switch iframe.Key {
case "src":
post.Src = iframe.Val
case "width":
w, err := strconv.ParseInt(iframe.Val, 10, 0)
if err == nil {
post.Width = w
}
case "height":
h, err := strconv.ParseInt(iframe.Val, 10, 0)
if err == nil {
post.Height = h
}
}
}
if len(post.Src) > 0 {
return
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(pointerNode)
if len(post.Src) < 1 {
return nil, fmt.Errorf("no src in the url")
}
urlPtr, err := url.Parse(post.Src)
if err != nil {
return nil, fmt.Errorf("cannot parse fb url")
}
if !strings.Contains(urlPtr.Hostname(), "facebook.com") {
return nil, fmt.Errorf("it is not facebook url")
}
return htmlText, nil
}
// YoutubeToTurbo convertes Youtube embeddable html to Yandex Turbo
func YoutubeToTurbo(htmlText []byte) ([]byte, error) {
pointerNode, err := html.Parse(bytes.NewReader(htmlText))
if err != nil {
return nil, fmt.Errorf("cannot parse youtube iframe")
}
var post youtubePost
var f func(*html.Node)
f = func(n *html.Node) {
if n.DataAtom == atom.Iframe {
for _, iframe := range n.Attr {
switch iframe.Key {
case "src":
post.Src = iframe.Val
case "width":
w, err := strconv.ParseInt(iframe.Val, 10, 0)
if err == nil {
post.Width = w
}
case "height":
h, err := strconv.ParseInt(iframe.Val, 10, 0)
if err == nil {
post.Height = h
}
case "allowfullscreen":
post.AllowFS = true
}
}
if len(post.Src) > 0 {
return
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(pointerNode)
if !(len(post.Src) > 0) {
return nil, fmt.Errorf("no src in the url")
}
urlPtr, err := url.Parse(post.Src)
if err != nil {
return nil, fmt.Errorf("cannot parse youtube url")
}
if !strings.Contains(urlPtr.Hostname(), "youtube.com") {
return nil, fmt.Errorf("it is not youtube url")
}
re := regexp.MustCompile(`embed/([A-Za-z0-9_-]{11})`)
submatch := re.FindStringSubmatch(urlPtr.Path)
if submatch == nil {
return nil, fmt.Errorf("youtube url is malformed")
}
post.VideoID = submatch[1]
return post.printTurbo(), nil
}
// IframeToTurbo convertes some custom iframe embeddable html to Yandex Turbo
func IframeToTurbo(htmlText []byte) ([]byte, error) {
pointerNode, err := html.Parse(bytes.NewReader(htmlText))
if err != nil {
return nil, fmt.Errorf("cannot parse iframe")
}
var post iframePost
var f func(*html.Node)
f = func(n *html.Node) {
if n.DataAtom == atom.Iframe {
for _, iframe := range n.Attr {
switch iframe.Key {
case "src":
post.Src = iframe.Val
case "width":
w, err := strconv.ParseInt(iframe.Val, 10, 0)
if err == nil {
post.Width = w
}
case "height":
h, err := strconv.ParseInt(iframe.Val, 10, 0)
if err == nil {
post.Height = h
}
case "allowfullscreen":
post.AllowFS = true
case "frameborder":
fb, err := strconv.ParseInt(iframe.Val, 10, 0)
if err == nil {
post.Frameborder = fb
}
}
}
if len(post.Src) > 0 {
return
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
f(c)
}
}
f(pointerNode)
if len(post.Src) < 1 {
return nil, fmt.Errorf("no src in the url")
}
urlPtr, err := url.Parse(post.Src)
if err != nil {
return nil, fmt.Errorf("cannot parse iframe url")
}
if urlPtr.Scheme != `https` {
return nil, fmt.Errorf("yandex Turbo supports only https iframe scheme")
}
return post.printTurbo(), nil
}