navidrome/core/agents/lastfm.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

148 lines
3.4 KiB
Go
Raw Normal View History

2021-02-07 22:46:15 +01:00
package agents
import (
"context"
"net/http"
"github.com/navidrome/navidrome/conf"
2021-02-08 05:26:05 +01:00
"github.com/navidrome/navidrome/consts"
2021-02-07 22:46:15 +01:00
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/utils/lastfm"
2021-02-07 22:46:15 +01:00
)
const (
lastFMAgentName = "lastfm"
lastFMAPIKey = "c2918986bf01b6ba353c0bc1bdd27bea"
//lastFMAPISecret = "3ff2aa214a6d8f2242515083bbb70e79" // Will be needed when implementing Scrobbling
)
2021-02-08 22:33:09 +01:00
2021-02-07 22:46:15 +01:00
type lastfmAgent struct {
ctx context.Context
apiKey string
lang string
client *lastfm.Client
}
func lastFMConstructor(ctx context.Context) Interface {
l := &lastfmAgent{
ctx: ctx,
lang: conf.Server.LastFM.Language,
}
if conf.Server.LastFM.ApiKey != "" {
l.apiKey = conf.Server.LastFM.ApiKey
} else {
l.apiKey = lastFMAPIKey
2021-02-07 22:46:15 +01:00
}
2021-02-08 05:26:05 +01:00
hc := NewCachedHTTPClient(http.DefaultClient, consts.DefaultCachedHttpClientTTL)
l.client = lastfm.NewClient(l.apiKey, l.lang, hc)
2021-02-07 22:46:15 +01:00
return l
}
2021-02-08 16:14:29 +01:00
func (l *lastfmAgent) AgentName() string {
2021-02-08 22:33:09 +01:00
return lastFMAgentName
2021-02-08 16:14:29 +01:00
}
2021-02-09 17:19:32 +01:00
func (l *lastfmAgent) GetMBID(id string, name string) (string, error) {
2021-02-07 22:46:15 +01:00
a, err := l.callArtistGetInfo(name, "")
if err != nil {
return "", err
}
if a.MBID == "" {
return "", ErrNotFound
}
return a.MBID, nil
}
2021-02-09 17:19:32 +01:00
func (l *lastfmAgent) GetURL(id, name, mbid string) (string, error) {
2021-02-07 22:46:15 +01:00
a, err := l.callArtistGetInfo(name, mbid)
if err != nil {
return "", err
}
if a.URL == "" {
return "", ErrNotFound
}
return a.URL, nil
}
2021-02-09 17:19:32 +01:00
func (l *lastfmAgent) GetBiography(id, name, mbid string) (string, error) {
2021-02-07 22:46:15 +01:00
a, err := l.callArtistGetInfo(name, mbid)
if err != nil {
return "", err
}
if a.Bio.Summary == "" {
return "", ErrNotFound
}
return a.Bio.Summary, nil
}
2021-02-09 17:19:32 +01:00
func (l *lastfmAgent) GetSimilar(id, name, mbid string, limit int) ([]Artist, error) {
2021-02-07 22:46:15 +01:00
resp, err := l.callArtistGetSimilar(name, mbid, limit)
if err != nil {
return nil, err
}
if len(resp) == 0 {
return nil, ErrNotFound
}
var res []Artist
for _, a := range resp {
res = append(res, Artist{
Name: a.Name,
MBID: a.MBID,
})
}
return res, nil
}
2021-02-09 17:19:32 +01:00
func (l *lastfmAgent) GetTopSongs(id, artistName, mbid string, count int) ([]Song, error) {
2021-02-07 22:46:15 +01:00
resp, err := l.callArtistGetTopTracks(artistName, mbid, count)
if err != nil {
return nil, err
}
if len(resp) == 0 {
return nil, ErrNotFound
}
2021-02-08 21:53:07 +01:00
var res []Song
2021-02-07 22:46:15 +01:00
for _, t := range resp {
2021-02-08 21:53:07 +01:00
res = append(res, Song{
2021-02-07 22:46:15 +01:00
Name: t.Name,
MBID: t.MBID,
})
}
return res, nil
}
func (l *lastfmAgent) callArtistGetInfo(name string, mbid string) (*lastfm.Artist, error) {
a, err := l.client.ArtistGetInfo(l.ctx, name, mbid)
2021-02-07 22:46:15 +01:00
if err != nil {
2021-02-08 05:26:05 +01:00
log.Error(l.ctx, "Error calling LastFM/artist.getInfo", "artist", name, "mbid", mbid, err)
2021-02-07 22:46:15 +01:00
return nil, err
}
return a, nil
}
func (l *lastfmAgent) callArtistGetSimilar(name string, mbid string, limit int) ([]lastfm.Artist, error) {
s, err := l.client.ArtistGetSimilar(l.ctx, name, mbid, limit)
2021-02-07 22:46:15 +01:00
if err != nil {
2021-02-08 05:26:05 +01:00
log.Error(l.ctx, "Error calling LastFM/artist.getSimilar", "artist", name, "mbid", mbid, err)
2021-02-07 22:46:15 +01:00
return nil, err
}
return s, nil
}
func (l *lastfmAgent) callArtistGetTopTracks(artistName, mbid string, count int) ([]lastfm.Track, error) {
t, err := l.client.ArtistGetTopTracks(l.ctx, artistName, mbid, count)
2021-02-07 22:46:15 +01:00
if err != nil {
2021-02-08 05:26:05 +01:00
log.Error(l.ctx, "Error calling LastFM/artist.getTopTracks", "artist", artistName, "mbid", mbid, err)
2021-02-07 22:46:15 +01:00
return nil, err
}
return t, nil
}
func init() {
conf.AddHook(func() {
if conf.Server.LastFM.Enabled {
2021-02-08 22:33:09 +01:00
Register(lastFMAgentName, lastFMConstructor)
2021-02-07 22:46:15 +01:00
}
})
}