add initial Last.FM tests (#329)

* Move model into separate file

* Separate Last.FM client and scrobbler

* Use separate Last.FM client and scrobbler

* Fix playcount attribute name

* Add initial test for Last.FM client
This commit is contained in:
Gregor Zurowski
2023-05-20 13:24:03 +02:00
committed by GitHub
parent 6144ac7979
commit dbcccdc811
12 changed files with 572 additions and 359 deletions

View File

@@ -24,6 +24,7 @@ import (
"go.senan.xyz/gonic"
"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/podcasts"
"go.senan.xyz/gonic/scrobble/lastfm"
"go.senan.xyz/gonic/server/ctrladmin/adminui"
"go.senan.xyz/gonic/server/ctrlbase"
)
@@ -74,13 +75,14 @@ func funcMap() template.FuncMap {
type Controller struct {
*ctrlbase.Controller
buffPool *bpool.BufferPool
template *template.Template
sessDB *gormstore.Store
Podcasts *podcasts.Podcasts
buffPool *bpool.BufferPool
template *template.Template
sessDB *gormstore.Store
Podcasts *podcasts.Podcasts
lastfmClient *lastfm.Client
}
func New(b *ctrlbase.Controller, sessDB *gormstore.Store, podcasts *podcasts.Podcasts) (*Controller, error) {
func New(b *ctrlbase.Controller, sessDB *gormstore.Store, podcasts *podcasts.Podcasts, lastfmClient *lastfm.Client) (*Controller, error) {
tmpl, err := template.
New("layout").
Funcs(template.FuncMap(sprig.FuncMap())).
@@ -93,11 +95,12 @@ func New(b *ctrlbase.Controller, sessDB *gormstore.Store, podcasts *podcasts.Pod
return nil, fmt.Errorf("build template: %w", err)
}
return &Controller{
Controller: b,
buffPool: bpool.NewBufferPool(64),
template: tmpl,
sessDB: sessDB,
Podcasts: podcasts,
Controller: b,
buffPool: bpool.NewBufferPool(64),
template: tmpl,
sessDB: sessDB,
Podcasts: podcasts,
lastfmClient: lastfmClient,
}, nil
}

View File

@@ -20,7 +20,6 @@ import (
"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/scanner"
"go.senan.xyz/gonic/scrobble/lastfm"
"go.senan.xyz/gonic/scrobble/listenbrainz"
"go.senan.xyz/gonic/transcode"
)
@@ -106,7 +105,7 @@ func (c *Controller) ServeLinkLastFMDo(r *http.Request) *Response {
if err != nil {
return &Response{redirect: r.Referer(), flashW: []string{fmt.Sprintf("couldn't get secret: %v", err)}}
}
sessionKey, err := lastfm.GetSession(apiKey, secret, token)
sessionKey, err := c.lastfmClient.GetSession(apiKey, secret, token)
if err != nil {
return &Response{
redirect: "/admin/home",

View File

@@ -12,6 +12,7 @@ import (
"go.senan.xyz/gonic/jukebox"
"go.senan.xyz/gonic/podcasts"
"go.senan.xyz/gonic/scrobble"
"go.senan.xyz/gonic/scrobble/lastfm"
"go.senan.xyz/gonic/server/ctrlbase"
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
@@ -48,6 +49,7 @@ type Controller struct {
Scrobblers []scrobble.Scrobbler
Podcasts *podcasts.Podcasts
Transcoder transcode.Transcoder
LastFMClient *lastfm.Client
}
type metaResponse struct {

View File

@@ -13,7 +13,6 @@ import (
"github.com/jinzhu/gorm"
"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/scrobble/lastfm"
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
@@ -318,7 +317,7 @@ func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {
if apiKey == "" {
return sub
}
info, err := lastfm.ArtistGetInfo(apiKey, artist.Name)
info, err := c.LastFMClient.ArtistGetInfo(apiKey, artist.Name)
if err != nil {
return spec.NewError(0, "fetching artist info: %v", err)
}
@@ -338,7 +337,7 @@ func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {
sub.ArtistInfoTwo.LargeImageURL = image.Text
}
}
if url, _ := lastfm.StealArtistImage(info.URL); url != "" {
if url, _ := c.LastFMClient.StealArtistImage(info.URL); url != "" {
sub.ArtistInfoTwo.SmallImageURL = url
sub.ArtistInfoTwo.MediumImageURL = url
sub.ArtistInfoTwo.LargeImageURL = url
@@ -348,7 +347,7 @@ func (c *Controller) ServeGetArtistInfoTwo(r *http.Request) *spec.Response {
count := params.GetOrInt("count", 20)
inclNotPresent := params.GetOrBool("includeNotPresent", false)
similarArtists, err := lastfm.ArtistGetSimilar(apiKey, artist.Name)
similarArtists, err := c.LastFMClient.ArtistGetSimilar(apiKey, artist.Name)
if err != nil {
return spec.NewError(0, "fetching artist similar: %v", err)
}
@@ -542,7 +541,7 @@ func (c *Controller) ServeGetTopSongs(r *http.Request) *spec.Response {
if apiKey == "" {
return spec.NewResponse()
}
topTracks, err := lastfm.ArtistGetTopTracks(apiKey, artist.Name)
topTracks, err := c.LastFMClient.ArtistGetTopTracks(apiKey, artist.Name)
if err != nil {
return spec.NewError(0, "fetching artist top tracks: %v", err)
}
@@ -610,7 +609,7 @@ func (c *Controller) ServeGetSimilarSongs(r *http.Request) *spec.Response {
return spec.NewError(10, "couldn't find a track with that id")
}
similarTracks, err := lastfm.TrackGetSimilarTracks(apiKey, track.Artist.Name, track.TagTitle)
similarTracks, err := c.LastFMClient.TrackGetSimilarTracks(apiKey, track.Artist.Name, track.TagTitle)
if err != nil {
return spec.NewError(0, "fetching track similar tracks: %v", err)
}
@@ -680,7 +679,7 @@ func (c *Controller) ServeGetSimilarSongsTwo(r *http.Request) *spec.Response {
return spec.NewError(0, "artist with id `%s` not found", id)
}
similarArtists, err := lastfm.ArtistGetSimilar(apiKey, artist.Name)
similarArtists, err := c.LastFMClient.ArtistGetSimilar(apiKey, artist.Name)
if err != nil {
return spec.NewError(0, "fetching artist similar artists: %v", err)
}

View File

@@ -97,19 +97,25 @@ func New(opts Options) (*Server, error) {
opts.CacheAudioPath,
)
ctrlAdmin, err := ctrladmin.New(base, sessDB, podcast)
lastfmClient := lastfm.NewClient()
ctrlAdmin, err := ctrladmin.New(base, sessDB, podcast, lastfmClient)
if err != nil {
return nil, fmt.Errorf("create admin controller: %w", err)
}
ctrlSubsonic := &ctrlsubsonic.Controller{
Controller: base,
MusicPaths: opts.MusicPaths,
PodcastsPath: opts.PodcastPath,
CacheAudioPath: opts.CacheAudioPath,
CoverCachePath: opts.CoverCachePath,
Scrobblers: []scrobble.Scrobbler{&lastfm.Scrobbler{DB: opts.DB}, listenbrainz.NewScrobbler()},
Podcasts: podcast,
Transcoder: cacheTranscoder,
Scrobblers: []scrobble.Scrobbler{
lastfm.NewScrobbler(opts.DB, lastfmClient),
listenbrainz.NewScrobbler(),
},
Podcasts: podcast,
Transcoder: cacheTranscoder,
}
setupMisc(r, base)