mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-01 19:38:34 +02:00
Use ExtendTrackMetadata also for LB API loves export
This commit is contained in:
parent
f70b6248b6
commit
ef6780701a
1 changed files with 34 additions and 17 deletions
|
@ -32,6 +32,8 @@ import (
|
||||||
"go.uploadedlobster.com/scotty/internal/version"
|
"go.uploadedlobster.com/scotty/internal/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const lovesBatchSize = listenbrainz.MaxItemsPerGet
|
||||||
|
|
||||||
type ListenBrainzApiBackend struct {
|
type ListenBrainzApiBackend struct {
|
||||||
client listenbrainz.Client
|
client listenbrainz.Client
|
||||||
mbClient musicbrainzws2.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) {
|
func (b *ListenBrainzApiBackend) exportLoves(ctx context.Context, oldestTimestamp time.Time, results chan models.LovesResult) {
|
||||||
offset := 0
|
offset := 0
|
||||||
defer close(results)
|
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:
|
out:
|
||||||
for {
|
for {
|
||||||
|
@ -245,31 +248,45 @@ out:
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, feedback := range result.Feedback {
|
for _, feedback := range result.Feedback {
|
||||||
// Missing track metadata indicates that the recording MBID is no
|
if time.Unix(feedback.Created, 0).After(oldestTimestamp) {
|
||||||
// longer available and might have been merged. Try fetching details
|
batch = append(batch, feedback)
|
||||||
// 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)
|
|
||||||
} else {
|
} else {
|
||||||
break out
|
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
|
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{
|
results <- models.LovesResult{
|
||||||
Total: len(loves),
|
Total: len(allLoves),
|
||||||
Items: loves,
|
Items: allLoves,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue