From ef6780701ad506aad79cea397ed6d758ab1c033a Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Sat, 24 May 2025 17:08:15 +0200 Subject: [PATCH] Use ExtendTrackMetadata also for LB API loves export --- .../backends/listenbrainz/listenbrainz.go | 51 ++++++++++++------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/internal/backends/listenbrainz/listenbrainz.go b/internal/backends/listenbrainz/listenbrainz.go index 8035b22..9e1c9f3 100644 --- a/internal/backends/listenbrainz/listenbrainz.go +++ b/internal/backends/listenbrainz/listenbrainz.go @@ -32,6 +32,8 @@ import ( "go.uploadedlobster.com/scotty/internal/version" ) +const lovesBatchSize = listenbrainz.MaxItemsPerGet + type ListenBrainzApiBackend struct { client listenbrainz.Client mbClient musicbrainzws2.Client @@ -229,7 +231,8 @@ func (b *ListenBrainzApiBackend) ExportLoves(ctx context.Context, oldestTimestam func (b *ListenBrainzApiBackend) exportLoves(ctx context.Context, oldestTimestamp time.Time, results chan models.LovesResult) { offset := 0 defer close(results) - loves := make(models.LovesList, 0, 2*listenbrainz.MaxItemsPerGet) + allLoves := make(models.LovesList, 0, 2*listenbrainz.MaxItemsPerGet) + batch := make([]listenbrainz.Feedback, 0, lovesBatchSize) out: for { @@ -245,31 +248,45 @@ out: } for _, feedback := range result.Feedback { - // Missing track metadata indicates that the recording MBID is no - // longer available and might have been merged. Try fetching details - // from MusicBrainz. - if feedback.TrackMetadata == nil { - track, err := LookupRecording(ctx, &b.mbClient, feedback.RecordingMBID) - if err == nil { - feedback.TrackMetadata = track - } - } - - love := AsLove(feedback) - if love.Created.After(oldestTimestamp) { - loves = append(loves, love) + if time.Unix(feedback.Created, 0).After(oldestTimestamp) { + batch = append(batch, feedback) } else { break out } + + if len(batch) >= lovesBatchSize { + // Missing track metadata indicates that the recording MBID is no + // longer available and might have been merged. Try fetching details + // from MusicBrainz. + lovesBatch, err := ExtendTrackMetadata(ctx, &b.client, &b.mbClient, &batch) + if err != nil { + results <- models.LovesResult{Error: err} + return + } + + for _, l := range lovesBatch { + allLoves = append(allLoves, l) + } + } } offset += listenbrainz.MaxItemsPerGet } - sort.Sort(loves) + lovesBatch, err := ExtendTrackMetadata(ctx, &b.client, &b.mbClient, &batch) + if err != nil { + results <- models.LovesResult{Error: err} + return + } + + for _, l := range lovesBatch { + allLoves = append(allLoves, l) + } + + sort.Sort(allLoves) results <- models.LovesResult{ - Total: len(loves), - Items: loves, + Total: len(allLoves), + Items: allLoves, } }