Skip to content

Commit

Permalink
Made checks on video less dependent on API, since the data is availab…
Browse files Browse the repository at this point in the history
…le in feed
  • Loading branch information
ashishjh-bst committed Sep 30, 2023
1 parent 700f3f5 commit f3c3ad0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
38 changes: 22 additions & 16 deletions youtube/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions youtube/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f3c3ad0

Please sign in to comment.