feat: add last fm importer

This commit is contained in:
Gabe Farrell 2025-06-12 21:54:35 -04:00
parent ceaba6f1a3
commit d2277aea32
7 changed files with 186 additions and 30 deletions

View file

@ -163,7 +163,7 @@ func Run(
// Import
if !cfg.SkipImport() {
go func() {
RunImporter(l, store)
RunImporter(l, store, mbzC)
}()
}
@ -186,7 +186,7 @@ func Run(
return nil
}
func RunImporter(l *zerolog.Logger, store db.DB) {
func RunImporter(l *zerolog.Logger, store db.DB, mbzc mbz.MusicBrainzCaller) {
l.Debug().Msg("Checking for import files...")
files, err := os.ReadDir(path.Join(cfg.ConfigDir(), "import"))
if err != nil {
@ -218,6 +218,12 @@ func RunImporter(l *zerolog.Logger, store db.DB) {
if err != nil {
l.Err(err).Msgf("Failed to import file: %s", file.Name())
}
} else if strings.Contains(file.Name(), "recenttracks") {
l.Info().Msgf("Import file %s detecting as being ghan.nl LastFM export", file.Name())
err := importer.ImportLastFMFile(logger.NewContext(l), store, mbzc, file.Name())
if err != nil {
l.Err(err).Msgf("Failed to import file: %s", file.Name())
}
} else {
l.Warn().Msgf("File %s not recognized as a valid import file; make sure it is valid and named correctly", file.Name())
}

View file

@ -5,11 +5,14 @@ import (
"os"
"path/filepath"
"testing"
"time"
"github.com/gabehf/koito/engine"
"github.com/gabehf/koito/internal/cfg"
"github.com/gabehf/koito/internal/db"
"github.com/gabehf/koito/internal/logger"
"github.com/gabehf/koito/internal/mbz"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -27,7 +30,7 @@ func TestImportMaloja(t *testing.T) {
require.NoError(t, os.WriteFile(dest, input, os.ModePerm))
engine.RunImporter(logger.Get(), store)
engine.RunImporter(logger.Get(), store, &mbz.MbzErrorCaller{})
// maloja test import is 38 Magnify Tokyo streams
a, err := store.GetArtist(context.Background(), db.GetArtistOpts{Name: "Magnify Tokyo"})
@ -50,7 +53,7 @@ func TestImportSpotify(t *testing.T) {
require.NoError(t, os.WriteFile(dest, input, os.ModePerm))
engine.RunImporter(logger.Get(), store)
engine.RunImporter(logger.Get(), store, &mbz.MbzErrorCaller{})
a, err := store.GetArtist(context.Background(), db.GetArtistOpts{Name: "The Story So Far"})
require.NoError(t, err)
@ -62,3 +65,31 @@ func TestImportSpotify(t *testing.T) {
// this is the only track with valid duration data
assert.EqualValues(t, 181, track.Duration)
}
func TestImportLastFM(t *testing.T) {
src := "../static/recenttracks-shoko2-1749776100.json"
destDir := filepath.Join(cfg.ConfigDir(), "import")
dest := filepath.Join(destDir, "recenttracks-shoko2-1749776100.json")
// not going to make the dest dir because engine should make it already
input, err := os.ReadFile(src)
require.NoError(t, err)
require.NoError(t, os.WriteFile(dest, input, os.ModePerm))
engine.RunImporter(logger.Get(), store, &mbz.MbzErrorCaller{})
album, err := store.GetAlbum(context.Background(), db.GetAlbumOpts{MusicBrainzID: uuid.MustParse("e9e78802-0bf8-4ca3-9655-1d943d2d2fa0")})
require.NoError(t, err)
assert.Equal(t, "ZOO!!", album.Title)
artist, err := store.GetArtist(context.Background(), db.GetArtistOpts{Name: "CHUU"})
require.NoError(t, err)
track, err := store.GetTrack(context.Background(), db.GetTrackOpts{Title: "because I'm stupid?", ArtistIDs: []int32{artist.ID}})
require.NoError(t, err)
listens, err := store.GetListensPaginated(context.Background(), db.GetItemsOpts{TrackID: int(track.ID)})
require.NoError(t, err)
assert.Len(t, listens.Items, 1)
assert.WithinDuration(t, time.Unix(1749776100, 0), listens.Items[0].Time, 1*time.Second)
}