Skip to content

Commit

Permalink
feat: set track provider from metadata
Browse files Browse the repository at this point in the history
There is enough information in the track metadata to select the provider. No need to pass it from outside.
  • Loading branch information
devgianlu committed Nov 9, 2024
1 parent 0950205 commit 85b1bb1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/daemon/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 8 additions & 1 deletion ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
20 changes: 5 additions & 15 deletions tracks/tracks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 85b1bb1

Please sign in to comment.