Skip to content

Commit

Permalink
fix(device): Fix search not triggering segment download
Browse files Browse the repository at this point in the history
Ref #27
  • Loading branch information
gabe565 committed Nov 1, 2023
1 parent 322e19b commit b182a21
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 42 deletions.
12 changes: 12 additions & 0 deletions internal/device/query_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package device

//go:generate stringer -type QueryState -linecomment

type QueryState uint8

const (
QueryNone QueryState = iota // none
QueryStarted // started
QuerySuccess // success
QueryFailed // failed
)
26 changes: 26 additions & 0 deletions internal/device/querystate_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions internal/device/video_meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package device

type VideoMeta struct {
CurrVideoId string
CurrArtist string
CurrTitle string

PrevVideoId string
PrevArtist string
PrevTitle string
}

func (v *VideoMeta) Clear() {
v.CurrVideoId = ""
v.CurrArtist = ""
v.CurrTitle = ""
v.PrevVideoId = ""
v.PrevArtist = ""
v.PrevTitle = ""
}

func (v VideoMeta) Empty() bool {
return v.CurrArtist == "" || v.CurrTitle == ""
}

func (v VideoMeta) SameVideo() bool {
return v.CurrArtist == v.PrevArtist && v.CurrTitle == v.PrevTitle
}
97 changes: 55 additions & 42 deletions internal/device/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ type Device struct {
tickInterval time.Duration
ticker *time.Ticker

prevVideoId string
prevArtist string
prevTitle string
meta VideoMeta
queryState QueryState
mediaSessionId int
segments []sponsorblock.Segment
mutedSegmentId int
Expand Down Expand Up @@ -180,18 +179,29 @@ func (d *Device) tick() error {
case StateAd:
d.muteAd(castVol)
default:
if castMedia.Media.ContentId == "" {
d.queryVideoId(castMedia)
if castMedia.Media.Metadata.Artist != "" {
d.meta.CurrArtist = castMedia.Media.Metadata.Artist
} else {
d.meta.CurrArtist = castMedia.Media.Metadata.Subtitle
}
d.meta.CurrTitle = castMedia.Media.Metadata.Title

if castMedia.Media.ContentId != "" {
d.meta.CurrVideoId = castMedia.Media.ContentId
d.queryState = QueryNone
} else {
d.queryVideoId()
break
}

if castMedia.Media.ContentId != d.prevVideoId {
if castMedia.Media.ContentId != "" {
d.logger.Info("Detected video stream.", "video_id", castMedia.Media.ContentId)
if d.meta.CurrVideoId != d.meta.PrevVideoId {
d.segments = nil
if d.meta.CurrVideoId != "" {
d.logger.Info("Detected video stream.", "video_id", d.meta.CurrVideoId)
d.meta.PrevVideoId = d.meta.CurrVideoId
go d.querySegments(castMedia)
}
d.prevVideoId = castMedia.Media.ContentId
d.unmuteSegment()
d.segments = nil
go d.querySegments(castMedia)
break
}

Expand Down Expand Up @@ -279,7 +289,7 @@ func (d *Device) onMessage(msg *api.CastMessage) {
case "CLOSE":
d.unmuteSegment()
d.segments = nil
d.prevTitle, d.prevArtist, d.prevVideoId = "", "", ""
d.meta.Clear()
d.mediaSessionId = 0
}
}
Expand All @@ -304,38 +314,41 @@ func (d *Device) update() error {
return err
}

func (d *Device) queryVideoId(castMedia *cast.Media) {
var currArtist string
if castMedia.Media.Metadata.Artist != "" {
currArtist = castMedia.Media.Metadata.Artist
} else {
currArtist = castMedia.Media.Metadata.Subtitle
func (d *Device) queryVideoId() {
switch d.queryState {
case QueryStarted:
return
case QueryFailed, QuerySuccess:
if d.meta.Empty() || d.meta.SameVideo() {
return
}
}
currTitle := castMedia.Media.Metadata.Title

if currArtist == d.prevArtist && currTitle == d.prevTitle {
castMedia.Media.ContentId = d.prevVideoId
if config.Default.YouTubeAPIKey == "" {
d.logger.Warn("Video ID not found. Please set a YouTube API key.")
} else {
if config.Default.YouTubeAPIKey == "" {
d.logger.Warn("Video ID not found. Please set a YouTube API key.")
} else {
d.logger.Info("Video ID not found. Searching for video on YouTube...")
d.prevArtist = currArtist
d.prevTitle = currTitle
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())
}
d.logger.Info("Video ID not found. Searching for video on YouTube...")
d.queryState = QueryStarted
d.meta.PrevArtist = d.meta.CurrArtist
d.meta.PrevTitle = d.meta.CurrTitle
go func() {
if err := util.Retry(d.ctx, 3, time.Second, func(try uint) (err error) {
contentId, err := youtube.QueryVideoId(d.ctx, d.meta.CurrArtist, d.meta.CurrTitle)
if err != nil {
d.logger.Error("YouTube search failed.", "error", err.Error())
d.queryState = QueryFailed
return err
}); err != nil {
d.logger.Debug("Halting YouTube search retries.")
return
}
d.logger.Debug("YouTube search found video ID", "video_id", castMedia.Media.ContentId)
}()
}

d.meta.CurrVideoId = contentId
d.queryState = QuerySuccess
return nil
}); err != nil {
d.logger.Debug("Halting YouTube search retries.")
return
}
d.logger.Debug("YouTube search found video ID", "video_id", d.meta.CurrVideoId)
}()
}
}

Expand Down Expand Up @@ -399,16 +412,16 @@ func (d *Device) unmuteSegment() {
}

func (d *Device) querySegments(castMedia *cast.Media) {
if castMedia.Media.ContentId == "" {
if d.meta.CurrVideoId == "" {
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)
d.segments, err = sponsorblock.QuerySegments(d.ctx, d.meta.CurrVideoId)
return err
}); err == nil {
if len(d.segments) == 0 {
d.logger.Info("No segments found for video.", "video_id", castMedia.Media.ContentId)
d.logger.Info("No segments found for video.", "video_id", d.meta.CurrVideoId)
} else {
d.logger.Info("Found segments for video.", "segments", len(d.segments))
}
Expand Down

0 comments on commit b182a21

Please sign in to comment.