Add SimilarSongs functionality
This commit is contained in:
+30
-4
@@ -8,6 +8,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Masterminds/squirrel"
|
||||||
"github.com/deluan/navidrome/core/lastfm"
|
"github.com/deluan/navidrome/core/lastfm"
|
||||||
"github.com/deluan/navidrome/core/spotify"
|
"github.com/deluan/navidrome/core/spotify"
|
||||||
"github.com/deluan/navidrome/log"
|
"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) {
|
func (e *externalInfo) SimilarSongs(ctx context.Context, id string, count int) (model.MediaFiles, error) {
|
||||||
// TODO
|
if e.lfm == nil {
|
||||||
// Get Similar Artists
|
log.Warn(ctx, "Last.FM client not configured")
|
||||||
// Get `count` songs from all similar artists, sorted randomly
|
return nil, model.ErrNotAvailable
|
||||||
return nil, nil
|
}
|
||||||
|
|
||||||
|
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) {
|
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 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 result model.Artists
|
||||||
var notPresent []string
|
var notPresent []string
|
||||||
|
|
||||||
|
|||||||
@@ -320,7 +320,7 @@ func (c *BrowsingController) GetSimilarSongs2(w http.ResponseWriter, r *http.Req
|
|||||||
|
|
||||||
response := newResponse()
|
response := newResponse()
|
||||||
response.SimilarSongs2 = &responses.SimilarSongs2{
|
response.SimilarSongs2 = &responses.SimilarSongs2{
|
||||||
Song: res.SimilarSongs2.Song,
|
Song: res.SimilarSongs.Song,
|
||||||
}
|
}
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user