From 85b1bb1183e70958f427be5e3bf966adc70f0be4 Mon Sep 17 00:00:00 2001 From: devgianlu Date: Sat, 9 Nov 2024 13:48:35 +0100 Subject: [PATCH] feat: set track provider from metadata There is enough information in the track metadata to select the provider. No need to pass it from outside. --- cmd/daemon/player.go | 2 +- ids.go | 9 ++++++++- tracks/tracks.go | 20 +++++--------------- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/cmd/daemon/player.go b/cmd/daemon/player.go index 5d8cf36..f3b7710 100644 --- a/cmd/daemon/player.go +++ b/cmd/daemon/player.go @@ -184,7 +184,7 @@ func (p *AppPlayer) handlePlayerCommand(req dealer.RequestPayload) error { } contextSpotType := librespot.InferSpotifyIdTypeFromContextUri(p.state.player.ContextUri) - currentTrack := librespot.ContextTrackToProvidedTrack(contextSpotType, transferState.Playback.CurrentTrack, "context") + currentTrack := librespot.ContextTrackToProvidedTrack(contextSpotType, transferState.Playback.CurrentTrack) if err := ctxTracks.TrySeek(tracks.ProvidedTrackComparator(contextSpotType, currentTrack)); err != nil { return fmt.Errorf("failed seeking to track: %w", err) } diff --git a/ids.go b/ids.go index 070a719..7171713 100644 --- a/ids.go +++ b/ids.go @@ -20,7 +20,7 @@ func InferSpotifyIdTypeFromContextUri(uri string) SpotifyIdType { return SpotifyIdTypeTrack } -func ContextTrackToProvidedTrack(typ SpotifyIdType, track *connectpb.ContextTrack, provider string) *connectpb.ProvidedTrack { +func ContextTrackToProvidedTrack(typ SpotifyIdType, track *connectpb.ContextTrack) *connectpb.ProvidedTrack { var uri string if len(track.Uri) > 0 { uri = track.Uri @@ -33,6 +33,13 @@ func ContextTrackToProvidedTrack(typ SpotifyIdType, track *connectpb.ContextTrac artistUri, _ := track.Metadata["artist_uri"] albumUri, _ := track.Metadata["album_uri"] + provider := "context" + if val := track.Metadata["is_queued"]; val == "true" { + provider = "queue" + } else if val = track.Metadata["autoplay.is_autoplay"]; val == "true" { + provider = "autoplay" + } + return &connectpb.ProvidedTrack{ Uri: uri, Uid: track.Uid, diff --git a/tracks/tracks.go b/tracks/tracks.go index 99dc8c8..d9bb412 100644 --- a/tracks/tracks.go +++ b/tracks/tracks.go @@ -78,7 +78,7 @@ func (tl *List) AllTracks() []*connectpb.ProvidedTrack { iter := tl.tracks.iterStart() for iter.next() { curr := iter.get() - tracks = append(tracks, librespot.ContextTrackToProvidedTrack(tl.ctx.Type(), curr.item, "context")) + tracks = append(tracks, librespot.ContextTrackToProvidedTrack(tl.ctx.Type(), curr.item)) } if err := iter.error(); err != nil { @@ -96,7 +96,7 @@ func (tl *List) PrevTracks() []*connectpb.ProvidedTrack { iter := tl.tracks.iterHere() for len(tracks) < MaxTracksInContext && iter.prev() { curr := iter.get() - tracks = append(tracks, librespot.ContextTrackToProvidedTrack(tl.ctx.Type(), curr.item, "context")) + tracks = append(tracks, librespot.ContextTrackToProvidedTrack(tl.ctx.Type(), curr.item)) } if err := iter.error(); err != nil { @@ -119,14 +119,14 @@ func (tl *List) NextTracks() []*connectpb.ProvidedTrack { } for i := 0; i < len(queue) && len(tracks) < MaxTracksInContext; i++ { - tracks = append(tracks, librespot.ContextTrackToProvidedTrack(tl.ctx.Type(), queue[i], "queue")) + tracks = append(tracks, librespot.ContextTrackToProvidedTrack(tl.ctx.Type(), queue[i])) } } iter := tl.tracks.iterHere() for len(tracks) < MaxTracksInContext && iter.next() { curr := iter.get() - tracks = append(tracks, librespot.ContextTrackToProvidedTrack(tl.ctx.Type(), curr.item, "context")) + tracks = append(tracks, librespot.ContextTrackToProvidedTrack(tl.ctx.Type(), curr.item)) } if err := iter.error(); err != nil { @@ -156,17 +156,7 @@ func (tl *List) current() *connectpb.ContextTrack { func (tl *List) CurrentTrack() *connectpb.ProvidedTrack { item := tl.current() - - var provider string - if autoplay, ok := item.Metadata["autoplay.is_autoplay"]; ok && autoplay == "true" { - provider = "autoplay" - } else if tl.playingQueue { - provider = "queue" - } else { - provider = "context" - } - - return librespot.ContextTrackToProvidedTrack(tl.ctx.Type(), item, provider) + return librespot.ContextTrackToProvidedTrack(tl.ctx.Type(), item) } func (tl *List) GoStart() bool {