-
Notifications
You must be signed in to change notification settings - Fork 3
/
rumble.go
164 lines (139 loc) · 3.68 KB
/
rumble.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
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/gofiber/fiber/v2"
"github.com/senseyeio/duration"
log "github.com/vyneer/orvods-go/logger"
)
const rumbleUA string = "Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0"
type RumbleSchemaBase struct {
T string `json:"@type"`
}
type RumbleSchemaVideo struct {
RumbleSchemaBase
EmbedURL string `json:"embedUrl"`
Duration string `json:"duration"`
}
type Rumble struct {
ID string `json:"id"`
start time.Time
StartTime string `json:"starttime"`
end time.Time
EndTime string `json:"endtime"`
}
func rumbleScrape(u *url.URL) (*Rumble, error) {
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", rumbleUA)
response, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return nil, fmt.Errorf("%d", response.StatusCode)
}
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
return nil, err
}
var rumble Rumble
sel := doc.FindMatcher(goquery.Single("div.streamed-on time"))
if sel.Size() > 0 {
startTime, ok := sel.Attr("datetime")
if !ok {
return nil, err
}
rumble.start, err = time.Parse(time.RFC3339, startTime)
if err != nil {
return nil, err
}
rumble.StartTime = rumble.start.Format("2006-01-02T15:04:05Z")
} else {
return nil, err
}
sel = doc.FindMatcher(goquery.Single("script[type='application/ld+json']"))
if sel.Size() > 0 {
var base []RumbleSchemaVideo
err := json.Unmarshal([]byte(sel.Text()), &base)
if err != nil {
return nil, err
}
for _, v := range base {
if v.T == "VideoObject" {
rumble.ID = strings.TrimSuffix(strings.TrimPrefix(v.EmbedURL, "https://rumble.com/embed/"), "/")
dur, err := duration.ParseISO8601(v.Duration)
if err != nil {
return nil, err
}
rumble.end = dur.Shift(rumble.start)
rumble.EndTime = rumble.end.Format("2006-01-02T15:04:05Z")
}
}
} else {
return nil, err
}
return &rumble, nil
}
func rumbleEmbedScrape(u *url.URL) (*url.URL, error) {
response, err := http.Get(u.String())
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return nil, fmt.Errorf("%d", response.StatusCode)
}
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
return nil, err
}
sel := doc.FindMatcher(goquery.Single("link[rel='canonical']"))
if sel.Size() > 0 {
href, ok := sel.Attr("href")
if !ok {
return nil, errors.New("no href")
}
hrefParsed, err := url.Parse(href)
if err != nil {
return nil, err
}
return hrefParsed, nil
} else {
return nil, errors.New("no link")
}
}
func getRumbleInfo(c *fiber.Ctx) error {
queryUrl := c.Query("url")
u, err := url.Parse(queryUrl)
if err != nil {
log.FiberErrorf("URL parse error during the Rumble scrape (%s): %s", c, queryUrl, err)
return c.Status(fiber.StatusBadRequest).SendString("Invalid URL")
}
if !strings.Contains(u.Hostname(), "rumble.com") {
return c.Status(fiber.StatusBadRequest).SendString("Invalid URL")
}
var realURL *url.URL
if strings.Contains(u.Path, "embed") {
realURL, err = rumbleEmbedScrape(u)
if err != nil {
log.FiberErrorf("HTTP error during the Rumble scraping (%s): %s", c, u.String(), err)
return c.Status(500).SendString("Rumble info error")
}
}
rumble, err := rumbleScrape(realURL)
if err != nil {
log.FiberErrorf("HTTP error during the Rumble scraping (%s): %s", c, u.String(), err)
return c.Status(500).SendString("Rumble info error")
}
return c.JSON(rumble)
}