-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfeed.go
233 lines (189 loc) · 5.29 KB
/
feed.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
package main
import (
"context"
"fmt"
"log/slog"
"net/http"
"net/url"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/eduncan911/podcast"
"github.com/labstack/echo/v4"
)
const (
rumbleHost = "rumble.com"
dateLayout = "2006-01-02T15:04:05-07:00"
httpClientTimeout = 10 * time.Second
httpServerReadTimeout = 5 * time.Second
httpServerWriteTimeout = 300 * time.Second
)
type Request struct {
Channel string
ChannelPath string
}
type Item struct {
Title string
Description string
Duration string
PublishTime string
ThumbnailSrc string
Link string
IsLiveBroadcast bool
}
func FeedHandler(c echo.Context) error {
var req Request
link := c.QueryParam("link")
url, err := url.Parse(link)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "could not parse link")
}
if url.Scheme == "" {
link = "https://" + link
url, err = url.Parse(link)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "could not parse link")
}
}
slog.Debug("url", "url", fmt.Sprintf("%#v", url))
if url.Host != rumbleHost {
return echo.NewHTTPError(http.StatusBadRequest, "link must use host "+rumbleHost)
}
// Trim anything from link after channel name
bits := strings.Split(url.Path, "/")
switch {
case len(bits) == 2:
req.Channel = bits[1]
req.ChannelPath = "/" + bits[1]
case len(bits) > 2:
if bits[1] == "c" {
req.Channel = bits[2]
req.ChannelPath = strings.Join(bits[:3], "/")
} else {
req.Channel = bits[1]
req.ChannelPath = "/" + bits[1]
}
}
if req.ChannelPath == "" {
return echo.NewHTTPError(http.StatusBadRequest, "channel name could not be found in link")
}
feed, err := GetFeed(c.Request().Context(), req)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("there was an error fetching the feed: %s", err))
}
err = feed.Encode(c.Response().Writer)
if err != nil {
return err
}
return nil
}
func GetFeed(ctx context.Context, r Request) (*podcast.Podcast, error) {
ctx2, cancel2 := context.WithTimeout(ctx, httpClientTimeout)
defer cancel2()
channelLink := "https://" + rumbleHost + r.ChannelPath
req, err := http.NewRequestWithContext(ctx2, "GET", channelLink, nil)
if err != nil {
return nil, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("rumble.com returned unexpected status %q", res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}
items := []Item{}
feedHeader := doc.Find("div.channel-header--content")
feedTitle := feedHeader.Find("div.channel-header--title h1").Text()
feedThumb, _ := feedHeader.Find("div.channel-header--thumb img").Attr("src")
doc.Find("section.channel-listing__container div.videostream.thumbnail__grid--item").Each(func(i int, s *goquery.Selection) {
if maxItemCount > 0 && len(items) == maxItemCount {
return
}
item := Item{}
info := s.Find("div.videostream__info")
item.Duration = strings.TrimSpace(info.Find(".videostream__status--duration").Text())
live := info.Find(".videostream__status--live")
// AFAIK there is no way to flag a podcast as live in iTunes RSS, but may be handy in future
if len(live.Nodes) > 0 {
item.IsLiveBroadcast = true
}
item.Title = strings.TrimSpace(s.Find("h3.thumbnail__title").Text())
if item.Title == "" {
item.Title = "unknown title"
}
item.Description = strings.TrimSpace(s.Find("div.videostream__description").Text())
if item.Description == "" {
item.Description = "unknown description"
}
if maxTextLength > 0 {
// trim title and description lengths
if len(item.Title) > maxTextLength {
item.Title = item.Title[:maxTextLength] + "..."
}
if len(item.Description) > maxTextLength {
item.Description = item.Description[:maxTextLength] + "..."
}
}
publishTimeEl := s.Find("div.videostream__data time")
item.PublishTime, _ = publishTimeEl.Attr("datetime")
item.Link = "https://" + rumbleHost
link := s.Find("a.videostream__link")
href, _ := link.Attr("href")
if href != "" {
item.Link += href
}
item.ThumbnailSrc, _ = s.Find("img.thumbnail__image").Attr("src")
items = append(items, item)
})
now := time.Now()
p := podcast.New(
feedTitle,
channelLink,
"", // TODO fix empty feed description
&now, // pubDate
&now, // lastBuildDate
)
if feedThumb != "" {
p.AddImage(feedThumb)
}
for _, i := range items {
publishTime := time.Time{}
if err != nil {
return nil, err
}
if i.PublishTime != "" {
publishTime, err = time.Parse(dateLayout, i.PublishTime)
if err != nil {
return nil, err
}
}
item := podcast.Item{
Title: i.Title,
Link: i.Link,
Description: i.Description,
PubDate: &publishTime,
}
if i.Duration != "" {
duration, err := parseDuration(i.Duration)
if err != nil {
// Error is non-fatal, just log
slog.Error("error parsing duration", "err", err)
}
item.AddDuration(int64(duration.Seconds()))
}
if i.ThumbnailSrc != "" {
item.AddImage(i.ThumbnailSrc)
}
if _, err := p.AddItem(item); err != nil {
return nil, fmt.Errorf("error adding item: %w", err)
}
}
slog.Info("feed", "url", req.URL, "item count", len(p.Items))
return &p, nil
}