move artistinfocache
This commit is contained in:
122
server/ctrlsubsonic/artistinfocache/artistinfocache.go
Normal file
122
server/ctrlsubsonic/artistinfocache/artistinfocache.go
Normal file
@@ -0,0 +1,122 @@
|
||||
//nolint:revive
|
||||
package artistinfocache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"go.senan.xyz/gonic/db"
|
||||
"go.senan.xyz/gonic/scrobble/lastfm"
|
||||
)
|
||||
|
||||
const keepFor = 30 * time.Hour * 24
|
||||
|
||||
type ArtistInfoCache struct {
|
||||
db *db.DB
|
||||
lastfmClient *lastfm.Client
|
||||
}
|
||||
|
||||
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) {
|
||||
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)
|
||||
}
|
||||
|
||||
var artistInfo db.ArtistInfo
|
||||
if err := a.db.Find(&artistInfo, "id=?", artistID).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, fmt.Errorf("find artist info in db: %w", err)
|
||||
}
|
||||
|
||||
if artistInfo.ID == 0 || artistInfo.Biography == "" /* prev not found maybe */ || time.Since(artistInfo.UpdatedAt) > keepFor {
|
||||
return a.Lookup(ctx, apiKey, &artist)
|
||||
}
|
||||
|
||||
return &artistInfo, nil
|
||||
}
|
||||
|
||||
func (a *ArtistInfoCache) Get(ctx context.Context, artistID int) (*db.ArtistInfo, error) {
|
||||
var artistInfo db.ArtistInfo
|
||||
if err := a.db.Find(&artistInfo, "id=?", artistID).Error; err != nil {
|
||||
return nil, fmt.Errorf("find artist info in db: %w", err)
|
||||
}
|
||||
return &artistInfo, nil
|
||||
}
|
||||
|
||||
func (a *ArtistInfoCache) Lookup(ctx context.Context, apiKey string, artist *db.Artist) (*db.ArtistInfo, error) {
|
||||
var artistInfo db.ArtistInfo
|
||||
artistInfo.ID = artist.ID
|
||||
|
||||
if err := a.db.FirstOrCreate(&artistInfo, "id=?", artistInfo.ID).Error; err != nil {
|
||||
return nil, fmt.Errorf("first or create artist info: %w", err)
|
||||
}
|
||||
|
||||
info, err := a.lastfmClient.ArtistGetInfo(apiKey, artist.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get upstream info: %w", err)
|
||||
}
|
||||
|
||||
artistInfo.ID = artist.ID
|
||||
artistInfo.Biography = info.Bio.Summary
|
||||
artistInfo.MusicBrainzID = info.MBID
|
||||
artistInfo.LastFMURL = info.URL
|
||||
|
||||
var similar []string
|
||||
for _, sim := range info.Similar.Artists {
|
||||
similar = append(similar, sim.Name)
|
||||
}
|
||||
artistInfo.SetSimilarArtists(similar)
|
||||
|
||||
url, _ := a.lastfmClient.StealArtistImage(info.URL)
|
||||
artistInfo.ImageURL = url
|
||||
|
||||
topTracksResponse, err := a.lastfmClient.ArtistGetTopTracks(apiKey, artist.Name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get top tracks: %w", err)
|
||||
}
|
||||
var topTracks []string
|
||||
for _, tr := range topTracksResponse.Tracks {
|
||||
topTracks = append(topTracks, tr.Name)
|
||||
}
|
||||
artistInfo.SetTopTracks(topTracks)
|
||||
|
||||
if err := a.db.Save(&artistInfo).Error; err != nil {
|
||||
return nil, fmt.Errorf("save upstream info: %w", err)
|
||||
}
|
||||
|
||||
return &artistInfo, nil
|
||||
}
|
||||
|
||||
func (a *ArtistInfoCache) Refresh(apiKey string, interval time.Duration) error {
|
||||
ticker := time.NewTicker(interval)
|
||||
for range ticker.C {
|
||||
q := a.db.
|
||||
Where("artist_infos.id IS NULL OR artist_infos.updated_at<?", time.Now().Add(-keepFor)).
|
||||
Joins("LEFT JOIN artist_infos ON artist_infos.id=artists.id")
|
||||
|
||||
var artist db.Artist
|
||||
if err := q.Find(&artist).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("error finding non cached artist: %v", err)
|
||||
continue
|
||||
}
|
||||
if artist.ID == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err := a.Lookup(context.Background(), apiKey, &artist); err != nil {
|
||||
log.Printf("error looking up non cached artist %s: %v", artist.Name, err)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("cached artist info for %q", artist.Name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
52
server/ctrlsubsonic/artistinfocache/artistinfocache_test.go
Normal file
52
server/ctrlsubsonic/artistinfocache/artistinfocache_test.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package artistinfocache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.senan.xyz/gonic/db"
|
||||
"go.senan.xyz/gonic/mockfs"
|
||||
"go.senan.xyz/gonic/scrobble/lastfm"
|
||||
"go.senan.xyz/gonic/scrobble/lastfm/mockclient"
|
||||
)
|
||||
|
||||
func TestInfoCache(t *testing.T) {
|
||||
m := mockfs.New(t)
|
||||
m.AddItems()
|
||||
m.ScanAndClean()
|
||||
|
||||
assert := assert.New(t)
|
||||
|
||||
var artist db.Artist
|
||||
assert.NoError(m.DB().Preload("Info").Find(&artist).Error)
|
||||
assert.Greater(artist.ID, 0)
|
||||
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)
|
||||
}
|
||||
}))
|
||||
|
||||
cache := New(m.DB(), lastfmClient)
|
||||
_, err := cache.GetOrLookup(context.Background(), "", artist.ID)
|
||||
require.NoError(t, err)
|
||||
_, err = cache.GetOrLookup(context.Background(), "", artist.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, int32(1), count.Load())
|
||||
|
||||
assert.NoError(m.DB().Preload("Info").Find(&artist, "id=?", artist.ID).Error)
|
||||
assert.Greater(artist.ID, 0)
|
||||
assert.NotNil(artist.Info)
|
||||
assert.Equal("Summary", artist.Info.Biography)
|
||||
}
|
||||
@@ -9,12 +9,12 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"go.senan.xyz/gonic/artistinfocache"
|
||||
"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/artistinfocache"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
||||
"go.senan.xyz/gonic/transcode"
|
||||
|
||||
@@ -15,8 +15,8 @@ import (
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/jinzhu/gorm"
|
||||
|
||||
"go.senan.xyz/gonic/artistinfocache"
|
||||
"go.senan.xyz/gonic/db"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/artistinfocache"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/params"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/spec"
|
||||
"go.senan.xyz/gonic/server/ctrlsubsonic/specid"
|
||||
|
||||
Reference in New Issue
Block a user