Pass context to export backends

This commit is contained in:
Philipp Wolfer 2025-05-22 11:09:39 +02:00
parent b5bca1d4ab
commit 26d9f5e840
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
12 changed files with 55 additions and 33 deletions

View file

@ -16,6 +16,7 @@ Scotty. If not, see <https://www.gnu.org/licenses/>.
package lastfm
import (
"context"
"fmt"
"net/url"
"sort"
@ -88,7 +89,7 @@ func (b *LastfmApiBackend) OAuth2Setup(token oauth2.TokenSource) error {
return nil
}
func (b *LastfmApiBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.TransferProgress) {
func (b *LastfmApiBackend) ExportListens(ctx context.Context, oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.TransferProgress) {
page := MaxPage
minTime := oldestTimestamp
perPage := MaxItemsPerGet
@ -102,6 +103,15 @@ func (b *LastfmApiBackend) ExportListens(oldestTimestamp time.Time, results chan
out:
for page > 0 {
select {
case <-ctx.Done():
results <- models.ListensResult{Error: ctx.Err()}
p.Export.Abort()
progress <- p
return
default:
}
args := lastfm.P{
"user": b.username,
"limit": MaxListensPerGet,
@ -258,7 +268,7 @@ func (b *LastfmApiBackend) ImportListens(export models.ListensResult, importResu
return importResult, nil
}
func (b *LastfmApiBackend) ExportLoves(oldestTimestamp time.Time, results chan models.LovesResult, progress chan models.TransferProgress) {
func (b *LastfmApiBackend) ExportLoves(ctx context.Context, oldestTimestamp time.Time, results chan models.LovesResult, progress chan models.TransferProgress) {
// Choose a high offset, we attempt to search the loves backwards starting
// at the oldest one.
page := 1
@ -274,6 +284,15 @@ func (b *LastfmApiBackend) ExportLoves(oldestTimestamp time.Time, results chan m
out:
for {
select {
case <-ctx.Done():
results <- models.LovesResult{Error: ctx.Err()}
p.Export.Abort()
progress <- p
return
default:
}
result, err := b.client.User.GetLovedTracks(lastfm.P{
"user": b.username,
"limit": MaxItemsPerGet,