ListenBrainz: Skip importing existing loves

This commit is contained in:
Philipp Wolfer 2023-11-13 18:11:18 +01:00
parent 6dd67aedcb
commit 9b5a087974
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B

View file

@ -116,6 +116,17 @@ func (b ListenBrainzApiBackend) ImportLoves(loves []models.Love, oldestTimestamp
LastTimestamp: oldestTimestamp,
ImportErrors: make([]string, 0),
}
existingLoves, err := b.ExportLoves(time.Unix(0, 0))
if err != nil {
return result, err
}
existingMbids := make(map[string]bool, len(existingLoves))
for _, love := range existingLoves {
existingMbids[string(love.RecordingMbid)] = true
}
for _, love := range loves {
if love.Created.Unix() <= oldestTimestamp.Unix() {
continue
@ -123,19 +134,29 @@ func (b ListenBrainzApiBackend) ImportLoves(loves []models.Love, oldestTimestamp
// TODO: Support love import without recording MBID
if love.RecordingMbid != "" {
ok := false
errMsg := ""
if existingMbids[string(love.RecordingMbid)] {
ok = true
} else {
resp, err := b.client.SendFeedback(Feedback{
RecordingMbid: string(love.RecordingMbid),
Score: 1,
})
ok = err == nil && resp.Status == "ok"
if err != nil {
errMsg = err.Error()
}
}
if err == nil && resp.Status == "ok" {
if ok {
result.ImportCount += 1
if love.Created.Unix() > result.LastTimestamp.Unix() {
result.LastTimestamp = love.Created
}
} else {
msg := fmt.Sprintf("Failed import of \"%s\" by %s: %v",
love.TrackName, love.ArtistName(), err.Error())
love.TrackName, love.ArtistName(), errMsg)
result.ImportErrors = append(result.ImportErrors, msg)
}
}