mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 10:09:28 +02:00
ListenBrainz: Skip importing existing loves
This commit is contained in:
parent
6dd67aedcb
commit
9b5a087974
1 changed files with 27 additions and 6 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue