From 64ccb4d1889ea9a27b08d332556c50d7e0fa9d18 Mon Sep 17 00:00:00 2001 From: Deluan Date: Tue, 20 Oct 2020 16:00:29 -0400 Subject: [PATCH] Add SimilarSongs functionality --- core/external_info.go | 34 ++++++++++++++++++++++++++++++---- server/subsonic/browsing.go | 2 +- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/core/external_info.go b/core/external_info.go index 523445312..e6a88ddb9 100644 --- a/core/external_info.go +++ b/core/external_info.go @@ -8,6 +8,7 @@ import ( "sync" "time" + "github.com/Masterminds/squirrel" "github.com/deluan/navidrome/core/lastfm" "github.com/deluan/navidrome/core/spotify" "github.com/deluan/navidrome/log" @@ -63,10 +64,31 @@ func (e *externalInfo) getArtist(ctx context.Context, id string) (artist *model. } func (e *externalInfo) SimilarSongs(ctx context.Context, id string, count int) (model.MediaFiles, error) { - // TODO - // Get Similar Artists - // Get `count` songs from all similar artists, sorted randomly - return nil, nil + if e.lfm == nil { + log.Warn(ctx, "Last.FM client not configured") + return nil, model.ErrNotAvailable + } + + artist, err := e.getArtist(ctx, id) + if err != nil { + return nil, err + } + + artists, err := e.similarArtists(ctx, artist, count, false) + if err != nil { + return nil, err + } + ids := make([]string, len(artists)+1) + ids[0] = artist.ID + for i, a := range artists { + ids[i+1] = a.ID + } + + return e.ds.MediaFile(ctx).GetAll(model.QueryOptions{ + Filters: squirrel.Eq{"artist_id": ids}, + Max: count, + Sort: "random()", + }) } func (e *externalInfo) SimilarArtists(ctx context.Context, id string, includeNotPresent bool, count int) (model.Artists, error) { @@ -80,6 +102,10 @@ func (e *externalInfo) SimilarArtists(ctx context.Context, id string, includeNot return nil, err } + return e.similarArtists(ctx, artist, count, includeNotPresent) +} + +func (e *externalInfo) similarArtists(ctx context.Context, artist *model.Artist, count int, includeNotPresent bool) (model.Artists, error) { var result model.Artists var notPresent []string diff --git a/server/subsonic/browsing.go b/server/subsonic/browsing.go index 9eef6a471..b02025bd2 100644 --- a/server/subsonic/browsing.go +++ b/server/subsonic/browsing.go @@ -320,7 +320,7 @@ func (c *BrowsingController) GetSimilarSongs2(w http.ResponseWriter, r *http.Req response := newResponse() response.SimilarSongs2 = &responses.SimilarSongs2{ - Song: res.SimilarSongs2.Song, + Song: res.SimilarSongs.Song, } return response, nil }