/
feed.go
427 lines (351 loc) · 11.5 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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package youtube
import (
"context"
"errors"
"fmt"
"strconv"
"sync"
"time"
"github.com/mrbentarikau/pagst/analytics"
"github.com/mrbentarikau/pagst/common"
"github.com/mrbentarikau/pagst/common/mqueue"
"github.com/mrbentarikau/pagst/feeds"
"github.com/jonas747/discordgo/v2"
"github.com/mediocregopher/radix/v3"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/oauth2/google"
"google.golang.org/api/youtube/v3"
)
const (
MaxChannelsPerPoll = 30
PollInterval = time.Second * 10
WebSubCheckInterval = time.Second * 10
// PollInterval = time.Second * 5 // <- used for debug purposes
)
func (p *Plugin) StartFeed() {
p.Stop = make(chan *sync.WaitGroup)
p.runWebsubChecker()
}
func (p *Plugin) StopFeed(wg *sync.WaitGroup) {
if p.Stop != nil {
p.Stop <- wg
} else {
wg.Done()
}
}
func (p *Plugin) SetupClient() error {
httpClient, err := google.DefaultClient(context.Background(), youtube.YoutubeScope)
if err != nil {
return common.ErrWithCaller(err)
}
yt, err := youtube.New(httpClient)
if err != nil {
return common.ErrWithCaller(err)
}
p.YTService = yt
return nil
}
// keeps the subscriptions up to date by updating the ones soon to be expiring
func (p *Plugin) runWebsubChecker() {
p.syncWebSubs()
websubTicker := time.NewTicker(WebSubCheckInterval)
for {
select {
case wg := <-p.Stop:
wg.Done()
return
case <-websubTicker.C:
p.checkExpiringWebsubs()
}
}
}
func (p *Plugin) checkExpiringWebsubs() {
err := common.BlockingLockRedisKey(RedisChannelsLockKey, 0, 10)
if err != nil {
logger.WithError(err).Error("Failed locking channels lock")
return
}
defer common.UnlockRedisKey(RedisChannelsLockKey)
maxScore := time.Now().Unix()
var expiring []string
err = common.RedisPool.Do(radix.FlatCmd(&expiring, "ZRANGEBYSCORE", RedisKeyWebSubChannels, "-inf", maxScore))
if err != nil {
logger.WithError(err).Error("Failed checking websubs")
return
}
for _, v := range expiring {
err := p.WebSubSubscribe(v)
if err != nil {
logger.WithError(err).WithField("yt_channel", v).Error("Failed subscribing to channel")
}
time.Sleep(time.Second)
}
}
func (p *Plugin) syncWebSubs() {
var activeChannels []string
err := common.SQLX.Select(&activeChannels, "SELECT DISTINCT(youtube_channel_id) FROM youtube_channel_subscriptions;")
if err != nil {
logger.WithError(err).Error("Failed syncing websubs, failed retrieving subbed channels")
return
}
common.RedisPool.Do(radix.WithConn(RedisKeyWebSubChannels, func(client radix.Conn) error {
locked := false
for _, channel := range activeChannels {
if !locked {
err := common.BlockingLockRedisKey(RedisChannelsLockKey, 0, 5000)
if err != nil {
logger.WithError(err).Error("Failed locking channels lock")
return err
}
locked = true
}
mn := radix.MaybeNil{}
client.Do(radix.Cmd(&mn, "ZSCORE", RedisKeyWebSubChannels, channel))
if mn.Nil {
// Not added
err := p.WebSubSubscribe(channel)
if err != nil {
logger.WithError(err).WithField("yt_channel", channel).Error("Failed subscribing to channel")
}
common.UnlockRedisKey(RedisChannelsLockKey)
locked = false
time.Sleep(time.Second)
}
}
if locked {
common.UnlockRedisKey(RedisChannelsLockKey)
}
return nil
}))
}
func (p *Plugin) removeAllSubsForChannel(channel string) {
err := common.GORM.Where("youtube_channel_id = ?", channel).Delete(ChannelSubscription{}).Error
if err != nil {
logger.WithError(err).WithField("yt_channel", channel).Error("failed removing channel")
}
go p.MaybeRemoveChannelWatch(channel)
}
func (p *Plugin) sendNewVidMessage(guild, discordChannel string, channelTitle string, videoID string, mentionEveryone bool, mentionRole string, liveBroadcastContent string) {
var content string
parsedChannel, _ := strconv.ParseInt(discordChannel, 10, 64)
parsedGuild, _ := strconv.ParseInt(guild, 10, 64)
parseMentionRole, _ := strconv.ParseInt(mentionRole, 10, 64)
videoYT := "https://www.youtube.com/watch?v=" + videoID
if mentionEveryone {
if liveBroadcastContent != "none" {
content += fmt.Sprintf("Hey @everyone, incoming %s YouTube video by **%s**!\n%s\n", liveBroadcastContent, channelTitle, videoYT)
} else {
content += fmt.Sprintf("Hey @everyone, incoming YouTube video by **%s**!\n%s\n", channelTitle, videoYT)
//content += "Hey @everyone, incoming YouTube video!\n\n"
}
} else if parseMentionRole > 0 {
if liveBroadcastContent != "none" {
content += fmt.Sprintf("Hey <@&%s>, incoming %s YouTube video by **%s**!\n%s\n", mentionRole, liveBroadcastContent, channelTitle, videoYT)
} else {
content += fmt.Sprintf("Hey <@&%s>, incoming YouTube video by **%s!**\n%s\n", mentionRole, channelTitle, videoYT)
}
} else {
content += fmt.Sprintf("**%s** uploaded a new YouTube video!\n%s", channelTitle, videoYT)
}
parseMentions := []discordgo.AllowedMentionType{}
if mentionEveryone {
parseMentions = []discordgo.AllowedMentionType{discordgo.AllowedMentionTypeEveryone}
} else if parseMentionRole > 0 {
parseMentions = []discordgo.AllowedMentionType{discordgo.AllowedMentionTypeRoles}
}
go analytics.RecordActiveUnit(parsedGuild, p, "posted_youtube_message")
feeds.MetricPostedMessages.With(prometheus.Labels{"source": "youtube"}).Inc()
mqueue.QueueMessage(&mqueue.QueuedElement{
GuildID: parsedGuild,
ChannelID: parsedChannel,
Source: "youtube",
SourceItemID: "",
MessageStr: content,
Priority: 2,
AllowedMentions: discordgo.AllowedMentions{
Parse: parseMentions,
},
})
}
var (
ErrIDNotFound = errors.New("ID not found")
)
func SubsForChannel(channel string) (result []*ChannelSubscription, err error) {
err = common.GORM.Where("youtube_channel_id = ?", channel).Find(&result).Error
return
}
var (
ErrNoChannel = errors.New("No channel with that id found")
)
func (p *Plugin) AddFeed(guildID, discordChannelID int64, youtubeChannelID, youtubeUsername string, mentionEveryone bool, mentionRole int64) (*ChannelSubscription, error) {
sub := &ChannelSubscription{
GuildID: discordgo.StrID(guildID),
ChannelID: discordgo.StrID(discordChannelID),
MentionEveryone: mentionEveryone,
MentionRole: discordgo.StrID(mentionRole),
}
call := p.YTService.Channels.List([]string{"snippet"})
if youtubeChannelID != "" {
call = call.Id(youtubeChannelID)
} else {
call = call.ForUsername(youtubeUsername)
}
cResp, err := call.Do()
if err != nil {
return nil, common.ErrWithCaller(err)
}
if len(cResp.Items) < 1 {
return nil, ErrNoChannel
}
sub.YoutubeChannelName = cResp.Items[0].Snippet.Title
sub.YoutubeChannelID = cResp.Items[0].Id
err = common.BlockingLockRedisKey(RedisChannelsLockKey, 0, 10)
if err != nil {
return nil, err
}
defer common.UnlockRedisKey(RedisChannelsLockKey)
err = common.GORM.Create(sub).Error
if err != nil {
return nil, err
}
err = p.MaybeAddChannelWatch(false, sub.YoutubeChannelID)
return sub, err
}
// maybeRemoveChannelWatch checks the channel for subs, if it has none then it removes it from the watchlist in redis.
func (p *Plugin) MaybeRemoveChannelWatch(channel string) {
err := common.BlockingLockRedisKey(RedisChannelsLockKey, 0, 10)
if err != nil {
return
}
defer common.UnlockRedisKey(RedisChannelsLockKey)
var count int
err = common.GORM.Model(&ChannelSubscription{}).Where("youtube_channel_id = ?", channel).Count(&count).Error
if err != nil || count > 0 {
if err != nil {
logger.WithError(err).WithField("yt_channel", channel).Error("Failed getting sub count")
}
return
}
err = common.MultipleCmds(
radix.Cmd(nil, "DEL", KeyLastVidTime(channel)),
radix.Cmd(nil, "DEL", KeyLastVidID(channel)),
radix.Cmd(nil, "ZREM", RedisKeyWebSubChannels, channel),
)
if err != nil {
return
}
err = p.WebSubUnsubscribe(channel)
if err != nil {
logger.WithError(err).Error("Failed unsubscribing to channel ", channel)
}
logger.WithField("yt_channel", channel).Info("Removed orphaned youtube channel from subbed channel sorted set")
}
// maybeAddChannelWatch adds a channel watch to redis, if there wasn't one before
func (p *Plugin) MaybeAddChannelWatch(lock bool, channel string) error {
if lock {
err := common.BlockingLockRedisKey(RedisChannelsLockKey, 0, 10)
if err != nil {
return common.ErrWithCaller(err)
}
defer common.UnlockRedisKey(RedisChannelsLockKey)
}
now := time.Now().Unix()
mn := radix.MaybeNil{}
err := common.RedisPool.Do(radix.Cmd(&mn, "ZSCORE", RedisKeyWebSubChannels, channel))
if err != nil {
return err
}
if !mn.Nil {
// Websub subscription already active, don't do anything more
return nil
}
err = common.RedisPool.Do(radix.FlatCmd(nil, "SET", KeyLastVidTime(channel), now))
if err != nil {
return err
}
// Also add websub subscription
err = p.WebSubSubscribe(channel)
if err != nil {
logger.WithError(err).Error("Failed subscribing to channel ", channel)
}
logger.WithField("yt_channel", channel).Info("Added new youtube channel watch")
return nil
}
func (p *Plugin) CheckVideo(videoID string, channelID string) error {
subs, err := p.getRemoveSubs(channelID)
if err != nil || len(subs) < 1 {
return err
}
lastVid, lastVidTime, err := p.getLastVidTimes(channelID)
if err != nil {
return err
}
if lastVid == videoID {
// the video was already posted and was probably just edited
return nil
}
resp, err := p.YTService.Videos.List([]string{"snippet"}).Id(videoID).Do()
if err != nil || len(resp.Items) < 1 {
return err
}
item := resp.Items[0]
//if item.Snippet.LiveBroadcastContent != "none" {
// ignore livestreams for now, might enable them at some point
// return nil
// }
parsedPublishedAt, err := time.Parse(time.RFC3339, item.Snippet.PublishedAt)
if err != nil {
return errors.New("Failed parsing youtube timestamp: " + err.Error() + ": " + item.Snippet.PublishedAt)
}
if time.Since(parsedPublishedAt) > time.Hour {
// just a safeguard against empty lastVidTime's
return nil
}
if lastVidTime.After(parsedPublishedAt) {
// wasn't a new vid
return nil
}
// This is a new video, post it
return p.postVideo(subs, parsedPublishedAt, item, channelID)
}
func (p *Plugin) postVideo(subs []*ChannelSubscription, publishedAt time.Time, video *youtube.Video, channelID string) error {
err := common.MultipleCmds(
radix.FlatCmd(nil, "SET", KeyLastVidTime(channelID), publishedAt.Unix()),
radix.FlatCmd(nil, "SET", KeyLastVidID(channelID), video.Id),
)
if err != nil {
return err
}
for _, sub := range subs {
p.sendNewVidMessage(sub.GuildID, sub.ChannelID, video.Snippet.ChannelTitle, video.Id, sub.MentionEveryone, sub.MentionRole, video.Snippet.LiveBroadcastContent)
}
return nil
}
func (p *Plugin) getRemoveSubs(channelID string) ([]*ChannelSubscription, error) {
var subs []*ChannelSubscription
err := common.GORM.Where("youtube_channel_id = ?", channelID).Find(&subs).Error
if err != nil {
return subs, err
}
if len(subs) < 1 {
time.AfterFunc(time.Second*10, func() {
p.MaybeRemoveChannelWatch(channelID)
})
return subs, nil
}
return subs, nil
}
func (p *Plugin) getLastVidTimes(channelID string) (lastVid string, lastVidTime time.Time, err error) {
// Find the last video time for this channel
var unixSeconds int64
err = common.RedisPool.Do(radix.Cmd(&unixSeconds, "GET", KeyLastVidTime(channelID)))
var lastProcessedVidTime time.Time
if err != nil || unixSeconds == 0 {
lastProcessedVidTime = time.Time{}
} else {
lastProcessedVidTime = time.Unix(unixSeconds, 0)
}
var lastVidID string
err = common.RedisPool.Do(radix.Cmd(&lastVidID, "GET", KeyLastVidID(channelID)))
return lastVidID, lastProcessedVidTime, err
}