Use LB API to lookup missing metadata for loves

This is faster than using the MBID API individually
This commit is contained in:
Philipp Wolfer 2025-05-24 16:46:10 +02:00
parent dddd2e4eec
commit 7542657925
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
4 changed files with 194 additions and 51 deletions

View file

@ -36,11 +36,12 @@ import (
const (
listensBatchSize = 2000
lovesBatchSize = 10
lovesBatchSize = listenbrainz.MaxItemsPerGet
)
type ListenBrainzArchiveBackend struct {
filePath string
lbClient listenbrainz.Client
mbClient musicbrainzws2.Client
}
@ -56,6 +57,7 @@ func (b *ListenBrainzArchiveBackend) Options() []models.BackendOption {
func (b *ListenBrainzArchiveBackend) InitConfig(config *config.ServiceConfig) error {
b.filePath = config.GetString("file-path")
b.lbClient = listenbrainz.NewClient("", version.UserAgent())
b.mbClient = *musicbrainzws2.NewClient(musicbrainzws2.AppInfo{
Name: version.AppName,
Version: version.AppVersion,
@ -164,7 +166,7 @@ func (b *ListenBrainzArchiveBackend) ExportLoves(
return
}
loves := make(models.LovesList, 0, lovesBatchSize)
batch := make([]listenbrainz.Feedback, 0, lovesBatchSize)
for feedback, err := range archive.IterFeedback(oldestTimestamp) {
if err != nil {
p.Export.Abort()
@ -173,37 +175,43 @@ func (b *ListenBrainzArchiveBackend) ExportLoves(
return
}
// The export file does not include track metadata. Try fetching details
// from MusicBrainz.
if feedback.TrackMetadata == nil {
track, err := lbapi.LookupRecording(ctx, &b.mbClient, feedback.RecordingMBID)
if err == nil {
feedback.TrackMetadata = track
}
if feedback.UserName == "" {
feedback.UserName = userInfo.Name
}
love := lbapi.AsLove(feedback)
if love.UserName == "" {
love.UserName = userInfo.Name
}
// TODO: The dump does not contain TrackMetadata for feedback.
// We need to look it up in the archive.
loves = append(loves, love)
batch = append(batch, feedback)
// Update the progress
p.Export.TotalItems += 1
remainingTime := startTime.Sub(love.Created)
remainingTime := startTime.Sub(time.Unix(feedback.Created, 0))
p.Export.Elapsed = int64(totalDuration.Seconds() - remainingTime.Seconds())
// Allow the importer to start processing the listens by
// sending them in batches.
if len(loves) >= lovesBatchSize {
if len(batch) >= lovesBatchSize {
// The dump does not contain track metadata. Extend it with additional
// lookups
loves, err := lbapi.ExtendTrackMetadata(ctx, &b.lbClient, &b.mbClient, &batch)
if err != nil {
p.Export.Abort()
progress <- p
results <- models.LovesResult{Error: err}
return
}
results <- models.LovesResult{Items: loves}
progress <- p
loves = loves[:0]
batch = batch[:0]
}
}
loves, err := lbapi.ExtendTrackMetadata(ctx, &b.lbClient, &b.mbClient, &batch)
if err != nil {
p.Export.Abort()
progress <- p
results <- models.LovesResult{Error: err}
return
}
results <- models.LovesResult{Items: loves}
p.Export.Complete()
progress <- p