refactor scrobblers (#383)

- no need to explicitly pass api key
- move packages up a level
- catch more errors by extended scrobbler interface with IsUserAuthenticated
- move interface to server
- delete scrobbber package, clients implicitly satisfy Scrobble

this also helps with gonic-lastfm-sync
This commit is contained in:
Senan Kelly
2023-09-27 01:13:00 +01:00
committed by GitHub
parent 32064d0279
commit f119659acf
27 changed files with 1100 additions and 1144 deletions

View File

@@ -10,7 +10,7 @@ import (
"github.com/jinzhu/gorm"
"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/scrobble/lastfm"
"go.senan.xyz/gonic/lastfm"
)
const keepFor = 30 * time.Hour * 24
@@ -24,7 +24,7 @@ func New(db *db.DB, lastfmClient *lastfm.Client) *ArtistInfoCache {
return &ArtistInfoCache{db: db, lastfmClient: lastfmClient}
}
func (a *ArtistInfoCache) GetOrLookup(ctx context.Context, apiKey string, artistID int) (*db.ArtistInfo, error) {
func (a *ArtistInfoCache) GetOrLookup(ctx context.Context, artistID int) (*db.ArtistInfo, error) {
var artist db.Artist
if err := a.db.Find(&artist, "id=?", artistID).Error; err != nil {
return nil, fmt.Errorf("find artist in db: %w", err)
@@ -36,7 +36,7 @@ func (a *ArtistInfoCache) GetOrLookup(ctx context.Context, apiKey string, artist
}
if artistInfo.ID == 0 || artistInfo.Biography == "" /* prev not found maybe */ || time.Since(artistInfo.UpdatedAt) > keepFor {
return a.Lookup(ctx, apiKey, &artist)
return a.Lookup(ctx, &artist)
}
return &artistInfo, nil
@@ -50,7 +50,7 @@ func (a *ArtistInfoCache) Get(ctx context.Context, artistID int) (*db.ArtistInfo
return &artistInfo, nil
}
func (a *ArtistInfoCache) Lookup(ctx context.Context, apiKey string, artist *db.Artist) (*db.ArtistInfo, error) {
func (a *ArtistInfoCache) Lookup(ctx context.Context, artist *db.Artist) (*db.ArtistInfo, error) {
var artistInfo db.ArtistInfo
artistInfo.ID = artist.ID
@@ -58,7 +58,7 @@ func (a *ArtistInfoCache) Lookup(ctx context.Context, apiKey string, artist *db.
return nil, fmt.Errorf("first or create artist info: %w", err)
}
info, err := a.lastfmClient.ArtistGetInfo(apiKey, artist.Name)
info, err := a.lastfmClient.ArtistGetInfo(artist.Name)
if err != nil {
return nil, fmt.Errorf("get upstream info: %w", err)
}
@@ -77,7 +77,7 @@ func (a *ArtistInfoCache) Lookup(ctx context.Context, apiKey string, artist *db.
url, _ := a.lastfmClient.StealArtistImage(info.URL)
artistInfo.ImageURL = url
topTracksResponse, err := a.lastfmClient.ArtistGetTopTracks(apiKey, artist.Name)
topTracksResponse, err := a.lastfmClient.ArtistGetTopTracks(artist.Name)
if err != nil {
return nil, fmt.Errorf("get top tracks: %w", err)
}
@@ -94,7 +94,7 @@ func (a *ArtistInfoCache) Lookup(ctx context.Context, apiKey string, artist *db.
return &artistInfo, nil
}
func (a *ArtistInfoCache) Refresh(apiKey string, interval time.Duration) error {
func (a *ArtistInfoCache) Refresh(interval time.Duration) error {
ticker := time.NewTicker(interval)
for range ticker.C {
q := a.db.
@@ -110,7 +110,7 @@ func (a *ArtistInfoCache) Refresh(apiKey string, interval time.Duration) error {
continue
}
if _, err := a.Lookup(context.Background(), apiKey, &artist); err != nil {
if _, err := a.Lookup(context.Background(), &artist); err != nil {
log.Printf("error looking up non cached artist %s: %v", artist.Name, err)
continue
}

View File

@@ -9,9 +9,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/lastfm"
"go.senan.xyz/gonic/lastfm/mockclient"
"go.senan.xyz/gonic/mockfs"
"go.senan.xyz/gonic/scrobble/lastfm"
"go.senan.xyz/gonic/scrobble/lastfm/mockclient"
)
func TestInfoCache(t *testing.T) {
@@ -29,20 +29,25 @@ func TestInfoCache(t *testing.T) {
assert.Nil(artist.Info)
var count atomic.Int32
lastfmClient := lastfm.NewClientCustom(mockclient.New(t, func(w http.ResponseWriter, r *http.Request) {
switch method := r.URL.Query().Get("method"); method {
case "artist.getInfo":
count.Add(1)
w.Write(mockclient.ArtistGetInfoResponse)
case "artist.getTopTracks":
w.Write(mockclient.ArtistGetTopTracksResponse)
}
}))
lastfmClient := lastfm.NewClientCustom(
mockclient.New(t, func(w http.ResponseWriter, r *http.Request) {
switch method := r.URL.Query().Get("method"); method {
case "artist.getInfo":
count.Add(1)
w.Write(mockclient.ArtistGetInfoResponse)
case "artist.getTopTracks":
w.Write(mockclient.ArtistGetTopTracksResponse)
}
}),
func() (apiKey string, secret string, err error) {
return "", "", nil
},
)
cache := New(m.DB(), lastfmClient)
_, err := cache.GetOrLookup(context.Background(), "", artist.ID)
_, err := cache.GetOrLookup(context.Background(), artist.ID)
require.NoError(t, err)
_, err = cache.GetOrLookup(context.Background(), "", artist.ID)
_, err = cache.GetOrLookup(context.Background(), artist.ID)
require.NoError(t, err)
require.Equal(t, int32(1), count.Load())