From d10487969619dd549b96c1b2b3b6fc359bb91b68 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Tue, 3 Oct 2023 02:43:44 -0500 Subject: [PATCH] fix(device): Fix ad skip being blocked by API lookups Ref #27 --- internal/device/watch.go | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/internal/device/watch.go b/internal/device/watch.go index 0a010d5..79ebb25 100644 --- a/internal/device/watch.go +++ b/internal/device/watch.go @@ -177,20 +177,18 @@ func (d *Device) tick() error { d.muteAd(castVol) default: if castMedia.Media.ContentId == "" { - if err := d.queryVideoId(castMedia); err != nil { - d.logger.Error("Failed to find video on YouTube.", "error", err.Error()) - } - if castMedia.Media.ContentId == "" { - d.changeTickInterval(config.Default.PausedInterval) - return nil - } + d.queryVideoId(castMedia) } if castMedia.Media.ContentId != d.prevVideoId { - d.logger.Info("Detected video stream.", "video_id", castMedia.Media.ContentId) + if castMedia.Media.ContentId != "" { + d.logger.Info("Detected video stream.", "video_id", castMedia.Media.ContentId) + } d.prevVideoId = castMedia.Media.ContentId d.unmuteSegment() - d.querySegments(castMedia) + d.segments = nil + go d.querySegments(castMedia) + break } for i, segment := range d.segments { @@ -291,7 +289,7 @@ func (d *Device) update() error { }) } -func (d *Device) queryVideoId(castMedia *cast.Media) error { +func (d *Device) queryVideoId(castMedia *cast.Media) { var currArtist string if castMedia.Media.Metadata.Artist != "" { currArtist = castMedia.Media.Metadata.Artist @@ -309,16 +307,20 @@ func (d *Device) queryVideoId(castMedia *cast.Media) error { d.logger.Info("Video ID not found. Searching for video on YouTube...") d.prevArtist = currArtist d.prevTitle = currTitle - return util.Retry(d.ctx, 10, 500*time.Millisecond, func(try uint) (err error) { - castMedia.Media.ContentId, err = youtube.QueryVideoId(d.ctx, currArtist, currTitle) - if errors.Is(err, youtube.ErrNoVideos) || errors.Is(err, youtube.ErrNoId) { - return util.HaltRetries(err) + go func() { + if err := util.Retry(d.ctx, 3, time.Second, func(try uint) (err error) { + castMedia.Media.ContentId, err = youtube.QueryVideoId(d.ctx, currArtist, currTitle) + if err != nil { + d.logger.Error("YouTube search failed.", "error", err.Error()) + } + return err + }); err != nil { + d.logger.Debug("Halting YouTube search retries.") } - return err - }) + d.logger.Debug("YouTube search found video ID", "video_id", castMedia.Media.ContentId) + }() } } - return nil } func (d *Device) muteAd(castVol *cast.Volume) { @@ -381,6 +383,10 @@ func (d *Device) unmuteSegment() { } func (d *Device) querySegments(castMedia *cast.Media) { + if castMedia.Media.ContentId == "" { + return + } + if err := util.Retry(d.ctx, 10, 500*time.Millisecond, func(try uint) (err error) { d.segments, err = sponsorblock.QuerySegments(d.ctx, castMedia.Media.ContentId) return err