Skip to content

Commit

Permalink
feat(subsonic): improve getArtistInfo2.view similar artist results (#203
Browse files Browse the repository at this point in the history
)

Co-authored-by: xavier <xavier@futurae.com>
  • Loading branch information
2 people authored and sentriz committed Feb 25, 2022
1 parent 401c17b commit 3161817
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions server/ctrlsubsonic/handlers_by_tags.go
Expand Up @@ -296,31 +296,33 @@ func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {

count := params.GetOrInt("count", 20)
inclNotPresent := params.GetOrBool("includeNotPresent", false)
for i, similarInfo := range info.Similar.Artists {
if i == count {
break
}
var artist db.Artist
err = c.DB.
Select("artists.*, count(albums.id) album_count").
Where("name=?", similarInfo.Name).
Joins("LEFT JOIN albums ON artists.id=albums.tag_artist_id").
Group("artists.id").
Find(&artist).
Error
if errors.Is(err, gorm.ErrRecordNotFound) && !inclNotPresent {
continue
}
similar := &spec.SimilarArtist{
ID: &specid.ID{},
}
if artist.ID != 0 {
similar.ID = artist.SID()

similarArtists, err := lastfm.ArtistGetSimilar(apiKey, artist.Name)
if err != nil {
return spec.NewError(0, "fetching artist info: %v", err)
}
similarArtistNames := make([]string, len(similarArtists.Artists))
for i, a := range similarArtists.Artists {
similarArtistNames[i] = a.Name
}

var artists []*db.Artist
err = c.DB.
Where("name IN (?)", similarArtistNames).
Find(&artists).
Limit(count).
Error
if errors.Is(err, gorm.ErrRecordNotFound) && !inclNotPresent {
return spec.NewError(10, "couldn't find any similar artists")
}

sub.ArtistInfoTwo.SimilarArtist = make([]*spec.SimilarArtist, len(artists))
for i, artist := range artists {
sub.ArtistInfoTwo.SimilarArtist[i] = &spec.SimilarArtist{
ID: artist.SID(),
Name: artist.Name,
AlbumCount: artist.AlbumCount,
}
similar.Name = similarInfo.Name
similar.AlbumCount = artist.AlbumCount
sub.ArtistInfoTwo.SimilarArtist = append(
sub.ArtistInfoTwo.SimilarArtist, similar)
}

return sub
Expand Down

0 comments on commit 3161817

Please sign in to comment.