implement getArtistInfo

This commit is contained in:
sentriz
2020-02-12 14:56:16 +00:00
parent 36829c69e3
commit c16897dfe4
5 changed files with 102 additions and 24 deletions

View File

@@ -11,6 +11,7 @@ import (
"senan.xyz/g/gonic/model"
"senan.xyz/g/gonic/server/ctrlsubsonic/params"
"senan.xyz/g/gonic/server/ctrlsubsonic/spec"
"senan.xyz/g/gonic/server/lastfm"
)
func (c *Controller) ServeGetArtists(r *http.Request) *spec.Response {
@@ -217,3 +218,44 @@ func (c *Controller) ServeSearchThree(r *http.Request) *spec.Response {
sub.SearchResultThree = results
return sub
}
func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {
params := r.Context().Value(CtxParams).(params.Params)
id, err := params.GetInt("id")
if err != nil {
return spec.NewError(10, "please provide an `id` parameter")
}
apiKey := c.DB.GetSetting("lastfm_api_key")
if apiKey == "" {
return spec.NewError(0, "please set ask your admin to set the last.fm api key")
}
artist := &model.Artist{}
err = c.DB.
Where("id = ?", id).
Find(artist).
Error
if gorm.IsRecordNotFoundError(err) {
return spec.NewError(70, "artist with id `%d` not found", id)
}
info, err := lastfm.ArtistGetInfo(apiKey, artist)
if err != nil {
return spec.NewError(0, "fetching artist info: %v", err)
}
sub := spec.NewResponse()
sub.ArtistInfoTwo = &spec.ArtistInfo{
Biography: info.Bio.Summary,
MusicBrainzID: info.MBID,
LastFMURL: info.URL,
}
for _, image := range info.Image {
switch image.Size {
case "small":
sub.ArtistInfoTwo.SmallImageURL = image.Text
case "medium":
sub.ArtistInfoTwo.MediumImageURL = image.Text
case "large":
sub.ArtistInfoTwo.LargeImageURL = image.Text
}
}
return sub
}

View File

@@ -9,18 +9,6 @@ import (
// NOTE: when these are implemented, they should be moved to their
// respective _by_folder or _by_tag file
func (c *Controller) ServeGetArtistInfo(r *http.Request) *spec.Response {
sub := spec.NewResponse()
sub.ArtistInfo = &spec.ArtistInfo{}
return sub
}
func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {
sub := spec.NewResponse()
sub.ArtistInfoTwo = &spec.ArtistInfo{}
return sub
}
func (c *Controller) ServeGetGenres(r *http.Request) *spec.Response {
sub := spec.NewResponse()
sub.Genres = &spec.Genres{}