Skip to content

Commit

Permalink
add similar songs fallback when server returns none
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jun 24, 2024
1 parent 17dfa11 commit 7f6335c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
22 changes: 22 additions & 0 deletions backend/mediaprovider/helpers/similar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package helpers

import (
"github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/sharedutil"
)

func GetSimilarSongsFallback(mp mediaprovider.MediaProvider, track *mediaprovider.Track, count int) []*mediaprovider.Track {
var tracks []*mediaprovider.Track
if len(track.ArtistIDs) > 0 {
tracks, _ = mp.GetSimilarTracks(track.ArtistIDs[0], count)
if len(tracks) > 0 {
return tracks
}
}
tracks, _ = mp.GetRandomTracks(track.Genre, count)

// make sure to exclude the song itself from the similar list
return sharedutil.FilterSlice(tracks, func(t *mediaprovider.Track) bool {
return t.ID != track.ID
})
}
8 changes: 8 additions & 0 deletions backend/mediaprovider/jellyfin/jellyfinmediaprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/dweymouth/go-jellyfin"
"github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/backend/mediaprovider/helpers"
"github.com/dweymouth/supersonic/sharedutil"
)

Expand Down Expand Up @@ -480,5 +481,12 @@ func (j *jellyfinMediaProvider) GetSongRadio(trackID string, count int) ([]*medi
if err != nil {
return nil, err
}
if len(tr) == 0 {
track, err := j.GetTrack(trackID)
if err != nil {
return nil, err
}
return helpers.GetSimilarSongsFallback(j, track, count), nil
}
return sharedutil.MapSlice(tr, toTrack), nil
}
8 changes: 8 additions & 0 deletions backend/mediaprovider/subsonic/subsonicmediaprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/dweymouth/go-subsonic/subsonic"
"github.com/dweymouth/supersonic/backend/mediaprovider"
"github.com/dweymouth/supersonic/backend/mediaprovider/helpers"
"github.com/dweymouth/supersonic/sharedutil"
)

Expand Down Expand Up @@ -622,5 +623,12 @@ func (s *subsonicMediaProvider) GetSongRadio(trackID string, count int) ([]*medi
if err != nil {
return nil, err
}
if len(tr) == 0 {
track, err := s.GetTrack(trackID)
if err != nil {
return nil, err
}
return helpers.GetSimilarSongsFallback(s, track, count), nil
}
return sharedutil.MapSlice(tr, toTrack), nil
}

0 comments on commit 7f6335c

Please sign in to comment.