From f3c3ad009259c50bbb02bfd6c82277ccbe5b8a43 Mon Sep 17 00:00:00 2001 From: Ashish Jhanwar Date: Sat, 30 Sep 2023 15:32:10 +0530 Subject: [PATCH] Made checks on video less dependent on API, since the data is available in feed --- youtube/feed.go | 38 ++++++++++++++++++++++---------------- youtube/web.go | 6 +----- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/youtube/feed.go b/youtube/feed.go index 47b8a7eb1a..024e7b7895 100644 --- a/youtube/feed.go +++ b/youtube/feed.go @@ -522,7 +522,22 @@ func (p *Plugin) MaybeAddChannelWatch(lock bool, channel string) error { return nil } -func (p *Plugin) CheckVideo(videoID string, channelID string) error { +func (p *Plugin) CheckVideo(parsedVideo XMLFeed) error { + if parsedVideo.VideoId == "" || parsedVideo.ChannelID == "" { + return nil + } + + parsedPublishedTime, err := time.Parse(time.RFC3339, parsedVideo.Published) + if err != nil { + return errors.New("Failed parsing youtube timestamp: " + err.Error() + ": " + parsedVideo.Published) + } + + if time.Since(parsedPublishedTime) > time.Hour { + return nil + } + + videoID := parsedVideo.VideoId + channelID := parsedVideo.ChannelID logger.Debugf("Checking video request with videoID %s and channelID %s ", videoID, channelID) subs, err := p.getRemoveSubs(channelID) if err != nil || len(subs) < 1 { @@ -534,6 +549,11 @@ func (p *Plugin) CheckVideo(videoID string, channelID string) error { return err } + if lastVidTime.After(parsedPublishedTime) { + // wasn't a new vid + return nil + } + if lastVid == videoID { // the video was already posted and was probably just edited return nil @@ -545,23 +565,9 @@ func (p *Plugin) CheckVideo(videoID string, channelID string) error { } item := resp.Items[0] - 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) + return p.postVideo(subs, parsedPublishedTime, item, channelID) } func (p *Plugin) isShortsVideo(video *youtube.Video) bool { diff --git a/youtube/web.go b/youtube/web.go index b1f36c04b1..b4af046886 100644 --- a/youtube/web.go +++ b/youtube/web.go @@ -315,11 +315,7 @@ func (p *Plugin) HandleFeedUpdate(w http.ResponseWriter, r *http.Request) { return } - if parsed.VideoId == "" || parsed.ChannelID == "" { - return - } - - err = p.CheckVideo(parsed.VideoId, parsed.ChannelID) + err = p.CheckVideo(parsed) if err != nil { web.CtxLogger(ctx).WithError(err).Error("Failed parsing checking new youtube video") w.WriteHeader(http.StatusInternalServerError)