mirror of
https://git.sr.ht/~phw/scotty
synced 2025-05-31 10:58:35 +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"
|
||||
)
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue