Add dedicated SimilarArtists call
This commit is contained in:
+78
-34
@@ -19,11 +19,14 @@ const placeholderArtistImageMediumUrl = "https://lastfm.freetls.fastly.net/i/u/1
|
|||||||
const placeholderArtistImageLargeUrl = "https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png"
|
const placeholderArtistImageLargeUrl = "https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png"
|
||||||
|
|
||||||
type ExternalInfo interface {
|
type ExternalInfo interface {
|
||||||
ArtistInfo(ctx context.Context, artistId string, includeNotPresent bool, count int) (*model.ArtistInfo, error)
|
ArtistInfo(ctx context.Context, id string) (*model.ArtistInfo, error)
|
||||||
|
SimilarArtists(ctx context.Context, id string, includeNotPresent bool, count int) (model.Artists, error)
|
||||||
|
SimilarSongs(ctx context.Context, id string, count int) (model.MediaFiles, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type LastFMClient interface {
|
type LastFMClient interface {
|
||||||
ArtistGetInfo(ctx context.Context, name string) (*lastfm.Artist, error)
|
ArtistGetInfo(ctx context.Context, name string) (*lastfm.Artist, error)
|
||||||
|
ArtistGetSimilar(ctx context.Context, name string, limit int) ([]lastfm.Artist, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type SpotifyClient interface {
|
type SpotifyClient interface {
|
||||||
@@ -40,20 +43,86 @@ type externalInfo struct {
|
|||||||
spf SpotifyClient
|
spf SpotifyClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *externalInfo) ArtistInfo(ctx context.Context, artistId string,
|
func (e *externalInfo) getArtist(ctx context.Context, id string) (artist *model.Artist, err error) {
|
||||||
includeNotPresent bool, count int) (*model.ArtistInfo, error) {
|
var entity interface{}
|
||||||
info := model.ArtistInfo{ID: artistId}
|
entity, err = GetEntityByID(ctx, e.ds, id)
|
||||||
|
|
||||||
artist, err := e.ds.Artist(ctx).Get(artistId)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
info.Name = artist.Name
|
|
||||||
|
switch v := entity.(type) {
|
||||||
|
case *model.Artist:
|
||||||
|
artist = v
|
||||||
|
case *model.MediaFile:
|
||||||
|
artist = &model.Artist{
|
||||||
|
ID: v.ArtistID,
|
||||||
|
Name: v.Artist,
|
||||||
|
}
|
||||||
|
case *model.Album:
|
||||||
|
artist = &model.Artist{
|
||||||
|
ID: v.AlbumArtistID,
|
||||||
|
Name: v.Artist,
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
err = model.ErrNotFound
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *externalInfo) SimilarArtists(ctx context.Context, id string, includeNotPresent bool, count int) (model.Artists, error) {
|
||||||
|
artist, err := e.getArtist(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var result model.Artists
|
||||||
|
var notPresent []string
|
||||||
|
|
||||||
|
similar, err := e.lfm.ArtistGetSimilar(ctx, artist.Name, count)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// First select artists that are present.
|
||||||
|
for _, s := range similar {
|
||||||
|
sa, err := e.ds.Artist(ctx).FindByName(s.Name)
|
||||||
|
if err != nil {
|
||||||
|
notPresent = append(notPresent, s.Name)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
result = append(result, *sa)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then fill up with non-present artists
|
||||||
|
if includeNotPresent {
|
||||||
|
for _, s := range notPresent {
|
||||||
|
sa := model.Artist{ID: "-1", Name: s}
|
||||||
|
result = append(result, sa)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *externalInfo) ArtistInfo(ctx context.Context, id string) (*model.ArtistInfo, error) {
|
||||||
|
artist, err := e.getArtist(ctx, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
info := model.ArtistInfo{ID: artist.ID, Name: artist.Name}
|
||||||
|
|
||||||
// TODO Load from local: artist.jpg/png/webp, artist.json (with the remaining info)
|
// TODO Load from local: artist.jpg/png/webp, artist.json (with the remaining info)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
e.callArtistInfo(ctx, artist, includeNotPresent, &wg, &info)
|
e.callArtistInfo(ctx, artist, &wg, &info)
|
||||||
e.callArtistImages(ctx, artist, &wg, &info)
|
e.callArtistImages(ctx, artist, &wg, &info)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
@@ -68,7 +137,7 @@ func (e *externalInfo) ArtistInfo(ctx context.Context, artistId string,
|
|||||||
return &info, nil
|
return &info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *externalInfo) callArtistInfo(ctx context.Context, artist *model.Artist, includeNotPresent bool,
|
func (e *externalInfo) callArtistInfo(ctx context.Context, artist *model.Artist,
|
||||||
wg *sync.WaitGroup, info *model.ArtistInfo) {
|
wg *sync.WaitGroup, info *model.ArtistInfo) {
|
||||||
if e.lfm != nil {
|
if e.lfm != nil {
|
||||||
log.Debug(ctx, "Calling Last.FM ArtistGetInfo", "artist", artist.Name)
|
log.Debug(ctx, "Calling Last.FM ArtistGetInfo", "artist", artist.Name)
|
||||||
@@ -85,7 +154,6 @@ func (e *externalInfo) callArtistInfo(ctx context.Context, artist *model.Artist,
|
|||||||
e.setBio(info, lfmArtist.Bio.Summary)
|
e.setBio(info, lfmArtist.Bio.Summary)
|
||||||
e.setLastFMUrl(info, lfmArtist.URL)
|
e.setLastFMUrl(info, lfmArtist.URL)
|
||||||
e.setMbzID(info, lfmArtist.MBID)
|
e.setMbzID(info, lfmArtist.MBID)
|
||||||
e.setSimilar(ctx, info, lfmArtist.Similar.Artists, includeNotPresent)
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -157,27 +225,3 @@ func (e *externalInfo) setLargeImageUrl(info *model.ArtistInfo, url string) {
|
|||||||
info.LargeImageUrl = url
|
info.LargeImageUrl = url
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *externalInfo) setSimilar(ctx context.Context, info *model.ArtistInfo, artists []lastfm.Artist, includeNotPresent bool) {
|
|
||||||
if len(info.Similar) == 0 {
|
|
||||||
var notPresent []string
|
|
||||||
|
|
||||||
// First select artists that are present.
|
|
||||||
for _, s := range artists {
|
|
||||||
sa, err := e.ds.Artist(ctx).FindByName(s.Name)
|
|
||||||
if err != nil {
|
|
||||||
notPresent = append(notPresent, s.Name)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
info.Similar = append(info.Similar, *sa)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Then fill up with non-present artists
|
|
||||||
if includeNotPresent {
|
|
||||||
for _, s := range notPresent {
|
|
||||||
sa := model.Artist{ID: "-1", Name: s}
|
|
||||||
info.Similar = append(info.Similar, sa)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/deluan/navidrome/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TODO: Should the type be encoded in the ID?
|
||||||
|
func GetEntityByID(ctx context.Context, ds model.DataStore, id string) (interface{}, error) {
|
||||||
|
ar, err := ds.Artist(ctx).Get(id)
|
||||||
|
if err == nil {
|
||||||
|
return ar, nil
|
||||||
|
}
|
||||||
|
al, err := ds.Album(ctx).Get(id)
|
||||||
|
if err == nil {
|
||||||
|
return al, nil
|
||||||
|
}
|
||||||
|
pls, err := ds.Playlist(ctx).Get(id)
|
||||||
|
if err == nil {
|
||||||
|
return pls, nil
|
||||||
|
}
|
||||||
|
mf, err := ds.MediaFile(ctx).Get(id)
|
||||||
|
if err == nil {
|
||||||
|
return mf, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
+29
-7
@@ -7,6 +7,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -27,14 +28,10 @@ type Client struct {
|
|||||||
hc HttpClient
|
hc HttpClient
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO SimilarArtists()
|
func (c *Client) makeRequest(params url.Values) (*Response, error) {
|
||||||
func (c *Client) ArtistGetInfo(ctx context.Context, name string) (*Artist, error) {
|
|
||||||
params := url.Values{}
|
|
||||||
params.Add("method", "artist.getInfo")
|
|
||||||
params.Add("format", "json")
|
params.Add("format", "json")
|
||||||
params.Add("api_key", c.apiKey)
|
params.Add("api_key", c.apiKey)
|
||||||
params.Add("artist", name)
|
|
||||||
params.Add("lang", c.lang)
|
|
||||||
req, _ := http.NewRequest("GET", apiBaseUrl, nil)
|
req, _ := http.NewRequest("GET", apiBaseUrl, nil)
|
||||||
req.URL.RawQuery = params.Encode()
|
req.URL.RawQuery = params.Encode()
|
||||||
|
|
||||||
@@ -55,7 +52,32 @@ func (c *Client) ArtistGetInfo(ctx context.Context, name string) (*Artist, error
|
|||||||
|
|
||||||
var response Response
|
var response Response
|
||||||
err = json.Unmarshal(data, &response)
|
err = json.Unmarshal(data, &response)
|
||||||
return &response.Artist, err
|
|
||||||
|
return &response, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) ArtistGetInfo(ctx context.Context, name string) (*Artist, error) {
|
||||||
|
params := url.Values{}
|
||||||
|
params.Add("method", "artist.getInfo")
|
||||||
|
params.Add("artist", name)
|
||||||
|
params.Add("lang", c.lang)
|
||||||
|
response, err := c.makeRequest(params)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &response.Artist, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) ArtistGetSimilar(ctx context.Context, name string, limit int) ([]Artist, error) {
|
||||||
|
params := url.Values{}
|
||||||
|
params.Add("method", "artist.getSimilar")
|
||||||
|
params.Add("artist", name)
|
||||||
|
params.Add("limit", strconv.Itoa(limit))
|
||||||
|
response, err := c.makeRequest(params)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return response.SimilarArtists.Artists, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) parseError(data []byte) error {
|
func (c *Client) parseError(data []byte) error {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ var _ = Describe("Client", func() {
|
|||||||
client = NewClient("API_KEY", "pt", httpClient)
|
client = NewClient("API_KEY", "pt", httpClient)
|
||||||
})
|
})
|
||||||
|
|
||||||
Describe("ArtistInfo", func() {
|
Describe("ArtistGetInfo", func() {
|
||||||
It("returns an artist for a successful response", func() {
|
It("returns an artist for a successful response", func() {
|
||||||
f, _ := os.Open("tests/fixtures/lastfm.artist.getinfo.json")
|
f, _ := os.Open("tests/fixtures/lastfm.artist.getinfo.json")
|
||||||
httpClient.res = http.Response{Body: f, StatusCode: 200}
|
httpClient.res = http.Response{Body: f, StatusCode: 200}
|
||||||
@@ -60,6 +60,46 @@ var _ = Describe("Client", func() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Describe("ArtistGetSimilar", func() {
|
||||||
|
It("returns an artist for a successful response", func() {
|
||||||
|
f, _ := os.Open("tests/fixtures/lastfm.artist.getsimilar.json")
|
||||||
|
httpClient.res = http.Response{Body: f, StatusCode: 200}
|
||||||
|
|
||||||
|
artists, err := client.ArtistGetSimilar(context.TODO(), "U2", 2)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
Expect(len(artists)).To(Equal(2))
|
||||||
|
Expect(httpClient.savedRequest.URL.String()).To(Equal(apiBaseUrl + "?api_key=API_KEY&artist=U2&format=json&limit=2&method=artist.getSimilar"))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("fails if Last.FM returns an error", func() {
|
||||||
|
httpClient.res = http.Response{
|
||||||
|
Body: ioutil.NopCloser(bytes.NewBufferString(`{"error":3,"message":"Invalid Method - No method with that name in this package"}`)),
|
||||||
|
StatusCode: 400,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := client.ArtistGetSimilar(context.TODO(), "U2", 2)
|
||||||
|
Expect(err).To(MatchError("last.fm error(3): Invalid Method - No method with that name in this package"))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("fails if HttpClient.Do() returns error", func() {
|
||||||
|
httpClient.err = errors.New("generic error")
|
||||||
|
|
||||||
|
_, err := client.ArtistGetSimilar(context.TODO(), "U2", 2)
|
||||||
|
Expect(err).To(MatchError("generic error"))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("fails if returned body is not a valid JSON", func() {
|
||||||
|
httpClient.res = http.Response{
|
||||||
|
Body: ioutil.NopCloser(bytes.NewBufferString(`<xml>NOT_VALID_JSON</xml>`)),
|
||||||
|
StatusCode: 200,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := client.ArtistGetSimilar(context.TODO(), "U2", 2)
|
||||||
|
Expect(err).To(MatchError("invalid character '<' looking for beginning of value"))
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
type fakeHttpClient struct {
|
type fakeHttpClient struct {
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package lastfm
|
package lastfm
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Artist Artist `json:"artist"`
|
Artist Artist `json:"artist"`
|
||||||
|
SimilarArtists SimilarArtists `json:"similarartists"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Artist struct {
|
type Artist struct {
|
||||||
@@ -14,15 +15,17 @@ type Artist struct {
|
|||||||
Listeners string `json:"listeners"`
|
Listeners string `json:"listeners"`
|
||||||
Plays string `json:"plays"`
|
Plays string `json:"plays"`
|
||||||
} `json:"stats"`
|
} `json:"stats"`
|
||||||
Similar struct {
|
Similar SimilarArtists `json:"similar"`
|
||||||
Artists []Artist `json:"artist"`
|
Tags struct {
|
||||||
} `json:"similar"`
|
|
||||||
Tags struct {
|
|
||||||
Tag []ArtistTag `json:"tag"`
|
Tag []ArtistTag `json:"tag"`
|
||||||
} `json:"tags"`
|
} `json:"tags"`
|
||||||
Bio ArtistBio `json:"bio"`
|
Bio ArtistBio `json:"bio"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SimilarArtists struct {
|
||||||
|
Artists []Artist `json:"artist"`
|
||||||
|
}
|
||||||
|
|
||||||
type ArtistImage struct {
|
type ArtistImage struct {
|
||||||
URL string `json:"#text"`
|
URL string `json:"#text"`
|
||||||
Size string `json:"size"`
|
Size string `json:"size"`
|
||||||
|
|||||||
@@ -28,6 +28,19 @@ var _ = Describe("LastFM responses", func() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Describe("SimilarArtists", func() {
|
||||||
|
It("parses the response correctly", func() {
|
||||||
|
var resp Response
|
||||||
|
body, _ := ioutil.ReadFile("tests/fixtures/lastfm.artist.getsimilar.json")
|
||||||
|
err := json.Unmarshal(body, &resp)
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
|
||||||
|
Expect(resp.SimilarArtists.Artists).To(HaveLen(2))
|
||||||
|
Expect(resp.SimilarArtists.Artists[0].Name).To(Equal("Passengers"))
|
||||||
|
Expect(resp.SimilarArtists.Artists[1].Name).To(Equal("INXS"))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
Describe("Error", func() {
|
Describe("Error", func() {
|
||||||
It("parses the error response correctly", func() {
|
It("parses the error response correctly", func() {
|
||||||
var error Error
|
var error Error
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ type ArtistInfo struct {
|
|||||||
Name string
|
Name string
|
||||||
MBID string
|
MBID string
|
||||||
Biography string
|
Biography string
|
||||||
Similar []Artist
|
|
||||||
SmallImageUrl string
|
SmallImageUrl string
|
||||||
MediumImageUrl string
|
MediumImageUrl string
|
||||||
LargeImageUrl string
|
LargeImageUrl string
|
||||||
|
|||||||
@@ -79,6 +79,8 @@ func (api *Router) routes() http.Handler {
|
|||||||
H(withPlayer, "getArtistInfo", c.GetArtistInfo)
|
H(withPlayer, "getArtistInfo", c.GetArtistInfo)
|
||||||
H(withPlayer, "getArtistInfo2", c.GetArtistInfo2)
|
H(withPlayer, "getArtistInfo2", c.GetArtistInfo2)
|
||||||
H(withPlayer, "getTopSongs", c.GetTopSongs)
|
H(withPlayer, "getTopSongs", c.GetTopSongs)
|
||||||
|
H(withPlayer, "getSimilarSongs", c.GetSimilarSongs)
|
||||||
|
H(withPlayer, "getSimilarSongs2", c.GetSimilarSongs2)
|
||||||
})
|
})
|
||||||
r.Group(func(r chi.Router) {
|
r.Group(func(r chi.Router) {
|
||||||
c := initAlbumListController(api)
|
c := initAlbumListController(api)
|
||||||
|
|||||||
+37
-18
@@ -109,7 +109,7 @@ func (c *BrowsingController) GetMusicDirectory(w http.ResponseWriter, r *http.Re
|
|||||||
id := utils.ParamString(r, "id")
|
id := utils.ParamString(r, "id")
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
|
|
||||||
entity, err := getEntityByID(ctx, c.ds, id)
|
entity, err := core.GetEntityByID(ctx, c.ds, id)
|
||||||
switch {
|
switch {
|
||||||
case err == model.ErrNotFound:
|
case err == model.ErrNotFound:
|
||||||
log.Error(r, "Requested ID not found ", "id", id)
|
log.Error(r, "Requested ID not found ", "id", id)
|
||||||
@@ -241,26 +241,12 @@ func (c *BrowsingController) GetArtistInfo(w http.ResponseWriter, r *http.Reques
|
|||||||
count := utils.ParamInt(r, "count", 20)
|
count := utils.ParamInt(r, "count", 20)
|
||||||
includeNotPresent := utils.ParamBool(r, "includeNotPresent", false)
|
includeNotPresent := utils.ParamBool(r, "includeNotPresent", false)
|
||||||
|
|
||||||
entity, err := getEntityByID(ctx, c.ds, id)
|
info, err := c.ei.ArtistInfo(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch v := entity.(type) {
|
similar, err := c.ei.SimilarArtists(ctx, id, includeNotPresent, count)
|
||||||
case *model.MediaFile:
|
|
||||||
id = v.ArtistID
|
|
||||||
case *model.Album:
|
|
||||||
id = v.AlbumArtistID
|
|
||||||
case *model.Artist:
|
|
||||||
id = v.ID
|
|
||||||
default:
|
|
||||||
err = model.ErrNotFound
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
info, err := c.ei.ArtistInfo(ctx, id, includeNotPresent, count)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -273,7 +259,7 @@ func (c *BrowsingController) GetArtistInfo(w http.ResponseWriter, r *http.Reques
|
|||||||
response.ArtistInfo.LargeImageUrl = info.LargeImageUrl
|
response.ArtistInfo.LargeImageUrl = info.LargeImageUrl
|
||||||
response.ArtistInfo.LastFmUrl = info.LastFMUrl
|
response.ArtistInfo.LastFmUrl = info.LastFMUrl
|
||||||
response.ArtistInfo.MusicBrainzID = info.MBID
|
response.ArtistInfo.MusicBrainzID = info.MBID
|
||||||
for _, s := range info.Similar {
|
for _, s := range similar {
|
||||||
similar := responses.Artist{}
|
similar := responses.Artist{}
|
||||||
similar.Id = s.ID
|
similar.Id = s.ID
|
||||||
similar.Name = s.Name
|
similar.Name = s.Name
|
||||||
@@ -306,6 +292,39 @@ func (c *BrowsingController) GetArtistInfo2(w http.ResponseWriter, r *http.Reque
|
|||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *BrowsingController) GetSimilarSongs(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
||||||
|
ctx := r.Context()
|
||||||
|
id, err := requiredParamString(r, "id", "id parameter required")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
count := utils.ParamInt(r, "count", 20)
|
||||||
|
|
||||||
|
songs, err := c.ei.SimilarSongs(ctx, id, count)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
response := newResponse()
|
||||||
|
response.SimilarSongs = &responses.SimilarSongs{
|
||||||
|
Song: childrenFromMediaFiles(ctx, songs),
|
||||||
|
}
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *BrowsingController) GetSimilarSongs2(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
||||||
|
res, err := c.GetSimilarSongs(w, r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
response := newResponse()
|
||||||
|
response.SimilarSongs2 = &responses.SimilarSongs2{
|
||||||
|
Song: res.SimilarSongs2.Song,
|
||||||
|
}
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
// TODO Integrate with Last.FM
|
// TODO Integrate with Last.FM
|
||||||
func (c *BrowsingController) GetTopSongs(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
func (c *BrowsingController) GetTopSongs(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
||||||
response := newResponse()
|
response := newResponse()
|
||||||
|
|||||||
@@ -258,24 +258,3 @@ func childrenFromAlbums(ctx context.Context, als model.Albums) []responses.Child
|
|||||||
}
|
}
|
||||||
return children
|
return children
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Should the type be encoded in the ID?
|
|
||||||
func getEntityByID(ctx context.Context, ds model.DataStore, id string) (interface{}, error) {
|
|
||||||
ar, err := ds.Artist(ctx).Get(id)
|
|
||||||
if err == nil {
|
|
||||||
return ar, nil
|
|
||||||
}
|
|
||||||
al, err := ds.Album(ctx).Get(id)
|
|
||||||
if err == nil {
|
|
||||||
return al, nil
|
|
||||||
}
|
|
||||||
pls, err := ds.Playlist(ctx).Get(id)
|
|
||||||
if err == nil {
|
|
||||||
return pls, nil
|
|
||||||
}
|
|
||||||
mf, err := ds.MediaFile(ctx).Get(id)
|
|
||||||
if err == nil {
|
|
||||||
return mf, nil
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"status":"ok","version":"1.8.0","type":"navidrome","serverVersion":"v0.0.0","similarSongs":{"song":[{"id":"1","isDir":false,"title":"title","isVideo":false}]}}
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0" type="navidrome" serverVersion="v0.0.0"><similarSongs><song id="1" isDir="false" title="title" isVideo="false"></song></similarSongs></subsonic-response>
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"status":"ok","version":"1.8.0","type":"navidrome","serverVersion":"v0.0.0","similarSongs":{}}
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0" type="navidrome" serverVersion="v0.0.0"><similarSongs></similarSongs></subsonic-response>
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"status":"ok","version":"1.8.0","type":"navidrome","serverVersion":"v0.0.0","similarSongs2":{"song":[{"id":"1","isDir":false,"title":"title","isVideo":false}]}}
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0" type="navidrome" serverVersion="v0.0.0"><similarSongs2><song id="1" isDir="false" title="title" isVideo="false"></song></similarSongs2></subsonic-response>
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
{"status":"ok","version":"1.8.0","type":"navidrome","serverVersion":"v0.0.0","similarSongs2":{}}
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
<subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="1.8.0" type="navidrome" serverVersion="v0.0.0"><similarSongs2></similarSongs2></subsonic-response>
|
||||||
@@ -36,9 +36,11 @@ type Subsonic struct {
|
|||||||
ArtistWithAlbumsID3 *ArtistWithAlbumsID3 `xml:"artist,omitempty" json:"artist,omitempty"`
|
ArtistWithAlbumsID3 *ArtistWithAlbumsID3 `xml:"artist,omitempty" json:"artist,omitempty"`
|
||||||
AlbumWithSongsID3 *AlbumWithSongsID3 `xml:"album,omitempty" json:"album,omitempty"`
|
AlbumWithSongsID3 *AlbumWithSongsID3 `xml:"album,omitempty" json:"album,omitempty"`
|
||||||
|
|
||||||
ArtistInfo *ArtistInfo `xml:"artistInfo,omitempty" json:"artistInfo,omitempty"`
|
ArtistInfo *ArtistInfo `xml:"artistInfo,omitempty" json:"artistInfo,omitempty"`
|
||||||
ArtistInfo2 *ArtistInfo2 `xml:"artistInfo2,omitempty" json:"artistInfo2,omitempty"`
|
ArtistInfo2 *ArtistInfo2 `xml:"artistInfo2,omitempty" json:"artistInfo2,omitempty"`
|
||||||
TopSongs *TopSongs `xml:"topSongs,omitempty" json:"topSongs,omitempty"`
|
SimilarSongs *SimilarSongs `xml:"similarSongs,omitempty" json:"similarSongs,omitempty"`
|
||||||
|
SimilarSongs2 *SimilarSongs2 `xml:"similarSongs2,omitempty" json:"similarSongs2,omitempty"`
|
||||||
|
TopSongs *TopSongs `xml:"topSongs,omitempty" json:"topSongs,omitempty"`
|
||||||
|
|
||||||
PlayQueue *PlayQueue `xml:"playQueue,omitempty" json:"playQueue,omitempty"`
|
PlayQueue *PlayQueue `xml:"playQueue,omitempty" json:"playQueue,omitempty"`
|
||||||
Bookmarks *Bookmarks `xml:"bookmarks,omitempty" json:"bookmarks,omitempty"`
|
Bookmarks *Bookmarks `xml:"bookmarks,omitempty" json:"bookmarks,omitempty"`
|
||||||
@@ -298,6 +300,14 @@ type ArtistInfo2 struct {
|
|||||||
SimilarArtist []ArtistID3 `xml:"similarArtist,omitempty" json:"similarArtist,omitempty"`
|
SimilarArtist []ArtistID3 `xml:"similarArtist,omitempty" json:"similarArtist,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SimilarSongs struct {
|
||||||
|
Song []Child `xml:"song,omitempty" json:"song,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SimilarSongs2 struct {
|
||||||
|
Song []Child `xml:"song,omitempty" json:"song,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
type TopSongs struct {
|
type TopSongs struct {
|
||||||
Song []Child `xml:"song,omitempty" json:"song,omitempty"`
|
Song []Child `xml:"song,omitempty" json:"song,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -361,6 +361,64 @@ var _ = Describe("Responses", func() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Describe("SimilarSongs", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
response.SimilarSongs = &SimilarSongs{}
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("without data", func() {
|
||||||
|
It("should match .XML", func() {
|
||||||
|
Expect(xml.Marshal(response)).To(MatchSnapshot())
|
||||||
|
})
|
||||||
|
It("should match .JSON", func() {
|
||||||
|
Expect(json.Marshal(response)).To(MatchSnapshot())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("with data", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
child := make([]Child, 1)
|
||||||
|
child[0] = Child{Id: "1", Title: "title", IsDir: false}
|
||||||
|
response.SimilarSongs.Song = child
|
||||||
|
})
|
||||||
|
It("should match .XML", func() {
|
||||||
|
Expect(xml.Marshal(response)).To(MatchSnapshot())
|
||||||
|
})
|
||||||
|
It("should match .JSON", func() {
|
||||||
|
Expect(json.Marshal(response)).To(MatchSnapshot())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Describe("SimilarSongs2", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
response.SimilarSongs2 = &SimilarSongs2{}
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("without data", func() {
|
||||||
|
It("should match .XML", func() {
|
||||||
|
Expect(xml.Marshal(response)).To(MatchSnapshot())
|
||||||
|
})
|
||||||
|
It("should match .JSON", func() {
|
||||||
|
Expect(json.Marshal(response)).To(MatchSnapshot())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("with data", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
child := make([]Child, 1)
|
||||||
|
child[0] = Child{Id: "1", Title: "title", IsDir: false}
|
||||||
|
response.SimilarSongs2.Song = child
|
||||||
|
})
|
||||||
|
It("should match .XML", func() {
|
||||||
|
Expect(xml.Marshal(response)).To(MatchSnapshot())
|
||||||
|
})
|
||||||
|
It("should match .JSON", func() {
|
||||||
|
Expect(json.Marshal(response)).To(MatchSnapshot())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
Describe("PlayQueue", func() {
|
Describe("PlayQueue", func() {
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
response.PlayQueue = &PlayQueue{}
|
response.PlayQueue = &PlayQueue{}
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ func (c *StreamController) Download(w http.ResponseWriter, r *http.Request) (*re
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
entity, err := getEntityByID(ctx, c.ds, id)
|
entity, err := core.GetEntityByID(ctx, c.ds, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
{"similarartists":{"artist":[{"name":"Passengers","mbid":"e110c11f-1c94-4471-a350-c38f46b29389","match":"1","url":"https://www.last.fm/music/Passengers","image":[{"#text":"https://lastfm.freetls.fastly.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png","size":"small"},{"#text":"https://lastfm.freetls.fastly.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png","size":"medium"},{"#text":"https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png","size":"large"},{"#text":"https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png","size":"extralarge"},{"#text":"https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png","size":"mega"},{"#text":"https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png","size":""}],"streamable":"0"},{"name":"INXS","mbid":"481bf5f9-2e7c-4c44-b08a-05b32bc7c00d","match":"0.511468","url":"https://www.last.fm/music/INXS","image":[{"#text":"https://lastfm.freetls.fastly.net/i/u/34s/2a96cbd8b46e442fc41c2b86b821562f.png","size":"small"},{"#text":"https://lastfm.freetls.fastly.net/i/u/64s/2a96cbd8b46e442fc41c2b86b821562f.png","size":"medium"},{"#text":"https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png","size":"large"},{"#text":"https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png","size":"extralarge"},{"#text":"https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png","size":"mega"},{"#text":"https://lastfm.freetls.fastly.net/i/u/300x300/2a96cbd8b46e442fc41c2b86b821562f.png","size":""}],"streamable":"0"}],"@attr":{"artist":"U2"}}}
|
||||||
Reference in New Issue
Block a user