-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(subsonic): cache and use lastfm responses for covers, bios, top …
…songs
- Loading branch information
Showing
7 changed files
with
238 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.