Use ExtendTrackMetadata also for LB API loves export

This commit is contained in:
Philipp Wolfer 2025-05-24 17:08:15 +02:00
parent f70b6248b6
commit ef6780701a
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B

View file

@ -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,
}
}