feat(subsonic): add getAlbumInfo with cache

Release-As: 0.16.1
This commit is contained in:
sentriz
2023-11-07 23:43:11 +00:00
parent 3f5cf56c88
commit cc1a99f033
14 changed files with 268 additions and 48 deletions

View File

@@ -0,0 +1,81 @@
//nolint:revive
package albuminfocache
import (
"context"
"errors"
"fmt"
"time"
"github.com/jinzhu/gorm"
"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/lastfm"
)
const keepFor = 30 * time.Hour * 24
type AlbumInfoCache struct {
db *db.DB
lastfmClient *lastfm.Client
}
func New(db *db.DB, lastfmClient *lastfm.Client) *AlbumInfoCache {
return &AlbumInfoCache{db: db, lastfmClient: lastfmClient}
}
func (a *AlbumInfoCache) GetOrLookup(ctx context.Context, albumID int) (*db.AlbumInfo, error) {
var album db.Album
if err := a.db.Find(&album, "id=?", albumID).Error; err != nil {
return nil, fmt.Errorf("find album in db: %w", err)
}
if album.TagAlbumArtist == "" || album.TagTitle == "" {
return nil, fmt.Errorf("no metadata to look up")
}
var albumInfo db.AlbumInfo
if err := a.db.Find(&albumInfo, "id=?", albumID).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, fmt.Errorf("find album info in db: %w", err)
}
if albumInfo.ID == 0 || time.Since(albumInfo.UpdatedAt) > keepFor {
return a.Lookup(ctx, &album)
}
return &albumInfo, nil
}
func (a *AlbumInfoCache) Get(ctx context.Context, albumID int) (*db.AlbumInfo, error) {
var albumInfo db.AlbumInfo
if err := a.db.Find(&albumInfo, "id=?", albumID).Error; err != nil {
return nil, fmt.Errorf("find album info in db: %w", err)
}
return &albumInfo, nil
}
func (a *AlbumInfoCache) Lookup(ctx context.Context, album *db.Album) (*db.AlbumInfo, error) {
var albumInfo db.AlbumInfo
albumInfo.ID = album.ID
if err := a.db.FirstOrCreate(&albumInfo, "id=?", albumInfo.ID).Error; err != nil {
return nil, fmt.Errorf("first or create album info: %w", err)
}
if err := a.db.Save(&albumInfo).Error; err != nil {
return nil, fmt.Errorf("bump updated_at time: %w", err)
}
info, err := a.lastfmClient.AlbumGetInfo(album.TagAlbumArtist, album.TagTitle)
if err != nil {
return nil, fmt.Errorf("get upstream info: %w", err)
}
albumInfo.ID = album.ID
albumInfo.Notes = info.Wiki.Content
albumInfo.MusicBrainzID = info.MBID
albumInfo.LastFMURL = info.URL
if err := a.db.Save(&albumInfo).Error; err != nil {
return nil, fmt.Errorf("save upstream info: %w", err)
}
return &albumInfo, nil
}

View File

@@ -0,0 +1,123 @@
//nolint:revive
package artistinfocache
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/jinzhu/gorm"
"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/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, 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)
}
if artist.Name == "" {
return nil, fmt.Errorf("no metadata to look up")
}
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, &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, 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)
}
if err := a.db.Save(&artistInfo).Error; err != nil {
return nil, fmt.Errorf("bump updated_at time: %w", err)
}
info, err := a.lastfmClient.ArtistGetInfo(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(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() error {
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) {
return fmt.Errorf("finding non cached artist: %w", err)
}
if artist.ID == 0 {
return nil
}
if _, err := a.Lookup(context.Background(), &artist); err != nil {
return fmt.Errorf("looking up non cached artist %s: %w", artist.Name, err)
}
log.Printf("cached artist info for %q", artist.Name)
return nil
}

View File

@@ -0,0 +1,59 @@
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/lastfm"
"go.senan.xyz/gonic/lastfm/mockclient"
"go.senan.xyz/gonic/mockfs"
)
func TestInfoCache(t *testing.T) {
t.Parallel()
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)
}
}),
func() (apiKey string, secret string, err error) {
return "", "", nil
},
)
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)
}