mirror of
https://github.com/gabehf/Koito.git
synced 2026-04-22 20:11:50 -07:00
Fix Maloja importer resilience: nil MBZ client, null album, error handling
Root cause of the panic at ~1,693 items: importers created
`&mbz.MusicBrainzClient{}` (nil requestQueue) instead of using the
engine's properly initialized client. When any code path called an MBZ
method, it panicked on the nil channel.
Changes:
- Pass engine's MBZ client to Maloja and Spotify importers
- Change MalojaTrack.Album to pointer type to handle null album JSON
- Continue on error instead of aborting the entire import
- Accept both Maloja export formats ("scrobbles" and "list" keys)
- Extract per-file import into importFile() with its own defer/recover
- Add progress logging every 500 items
Test fixtures:
- maloja_import_null_album_test.json (null album, valid album, empty artists)
- maloja_api_format_test.json (API "list" format)
New tests: TestImportMaloja_NullAlbum, TestImportMaloja_ApiFormat
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
0ec7b458cc
commit
c2d393aa03
6 changed files with 224 additions and 55 deletions
|
|
@ -17,22 +17,25 @@ import (
|
|||
"github.com/gabehf/koito/internal/utils"
|
||||
)
|
||||
|
||||
type MalojaExport struct {
|
||||
type MalojaAlbum struct {
|
||||
Title string `json:"albumtitle"`
|
||||
}
|
||||
|
||||
type MalojaFile struct {
|
||||
Scrobbles []MalojaExportItem `json:"scrobbles"`
|
||||
List []MalojaExportItem `json:"list"`
|
||||
}
|
||||
type MalojaExportItem struct {
|
||||
Time int64 `json:"time"`
|
||||
Track MalojaTrack `json:"track"`
|
||||
}
|
||||
type MalojaTrack struct {
|
||||
Artists []string `json:"artists"`
|
||||
Title string `json:"title"`
|
||||
Album struct {
|
||||
Title string `json:"albumtitle"`
|
||||
} `json:"album"`
|
||||
Artists []string `json:"artists"`
|
||||
Title string `json:"title"`
|
||||
Album *MalojaAlbum `json:"album"`
|
||||
}
|
||||
|
||||
func ImportMalojaFile(ctx context.Context, store db.DB, filename string) error {
|
||||
func ImportMalojaFile(ctx context.Context, store db.DB, mbzc mbz.MusicBrainzCaller, filename string) error {
|
||||
l := logger.FromContext(ctx)
|
||||
l.Info().Msgf("Beginning maloja import on file: %s", filename)
|
||||
file, err := os.Open(path.Join(cfg.ConfigDir(), "import", filename))
|
||||
|
|
@ -47,14 +50,20 @@ func ImportMalojaFile(ctx context.Context, store db.DB, filename string) error {
|
|||
time.Sleep(time.Duration(ms) * time.Millisecond)
|
||||
}
|
||||
}
|
||||
export := new(MalojaExport)
|
||||
export := new(MalojaFile)
|
||||
err = json.NewDecoder(file).Decode(&export)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ImportMalojaFile: %w", err)
|
||||
}
|
||||
for _, item := range export.Scrobbles {
|
||||
items := export.Scrobbles
|
||||
if len(items) == 0 {
|
||||
items = export.List
|
||||
}
|
||||
count := 0
|
||||
total := len(items)
|
||||
for i, item := range items {
|
||||
martists := make([]string, 0)
|
||||
// Maloja has a tendency to have the the artist order ['feature', 'main \u2022 feature'], so
|
||||
// Maloja has a tendency to have the the artist order ['feature', 'main ● feature'], so
|
||||
// here we try to turn that artist array into ['main', 'feature']
|
||||
item.Track.Artists = utils.MoveFirstMatchToFront(item.Track.Artists, " \u2022 ")
|
||||
for _, an := range item.Track.Artists {
|
||||
|
|
@ -71,12 +80,16 @@ func ImportMalojaFile(ctx context.Context, store db.DB, filename string) error {
|
|||
l.Debug().Msgf("Skipping import due to import time rules")
|
||||
continue
|
||||
}
|
||||
releaseTitle := ""
|
||||
if item.Track.Album != nil {
|
||||
releaseTitle = item.Track.Album.Title
|
||||
}
|
||||
opts := catalog.SubmitListenOpts{
|
||||
MbzCaller: &mbz.MusicBrainzClient{},
|
||||
MbzCaller: mbzc,
|
||||
Artist: item.Track.Artists[0],
|
||||
ArtistNames: artists,
|
||||
TrackTitle: item.Track.Title,
|
||||
ReleaseTitle: item.Track.Album.Title,
|
||||
ReleaseTitle: releaseTitle,
|
||||
Time: ts.Local(),
|
||||
Client: "maloja",
|
||||
UserID: 1,
|
||||
|
|
@ -84,10 +97,14 @@ func ImportMalojaFile(ctx context.Context, store db.DB, filename string) error {
|
|||
}
|
||||
err = catalog.SubmitListen(ctx, store, opts)
|
||||
if err != nil {
|
||||
l.Err(err).Msg("Failed to import maloja playback item")
|
||||
return fmt.Errorf("ImportMalojaFile: %w", err)
|
||||
l.Err(err).Msgf("Failed to import maloja item %d/%d", i+1, total)
|
||||
continue
|
||||
}
|
||||
count++
|
||||
if count%500 == 0 {
|
||||
l.Info().Msgf("Maloja import progress: %d/%d", count, total)
|
||||
}
|
||||
throttleFunc()
|
||||
}
|
||||
return finishImport(ctx, filename, len(export.Scrobbles))
|
||||
return finishImport(ctx, filename, count)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ type SpotifyExportItem struct {
|
|||
MsPlayed int32 `json:"ms_played"`
|
||||
}
|
||||
|
||||
func ImportSpotifyFile(ctx context.Context, store db.DB, filename string) error {
|
||||
func ImportSpotifyFile(ctx context.Context, store db.DB, mbzc mbz.MusicBrainzCaller, filename string) error {
|
||||
l := logger.FromContext(ctx)
|
||||
l.Info().Msgf("Beginning spotify import on file: %s", filename)
|
||||
file, err := os.Open(path.Join(cfg.ConfigDir(), "import", filename))
|
||||
|
|
@ -59,7 +59,7 @@ func ImportSpotifyFile(ctx context.Context, store db.DB, filename string) error
|
|||
continue
|
||||
}
|
||||
opts := catalog.SubmitListenOpts{
|
||||
MbzCaller: &mbz.MusicBrainzClient{},
|
||||
MbzCaller: mbzc,
|
||||
Artist: item.ArtistName,
|
||||
TrackTitle: item.TrackName,
|
||||
ReleaseTitle: item.AlbumName,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue