From 4a9f26d9dbf8c5ee9ccbd32e4b18bf0fb7ef104e Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Wed, 22 Nov 2023 12:39:41 +0100 Subject: [PATCH] Improved progress report for Spotify loves --- backends/process.go | 12 ++++++++++-- backends/spotify/spotify.go | 7 +++++-- models/models.go | 2 ++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/backends/process.go b/backends/process.go index a19f379..e31f2dd 100644 --- a/backends/process.go +++ b/backends/process.go @@ -36,7 +36,11 @@ func ProcessListensImports(importer models.ListensImport, results chan models.Li return } - result.TotalCount += len(exportResult.Listens) + if exportResult.Total > 0 { + result.TotalCount = exportResult.Total + } else { + result.TotalCount += len(exportResult.Listens) + } importResult, err := importer.ImportListens(exportResult, result, progress) if err != nil { handleError(importResult, err, out, progress) @@ -74,7 +78,11 @@ func ProcessLovesImports(importer models.LovesImport, results chan models.LovesR return } - result.TotalCount += len(exportResult.Loves) + if exportResult.Total > 0 { + result.TotalCount = exportResult.Total + } else { + result.TotalCount += len(exportResult.Loves) + } importResult, err := importer.ImportLoves(exportResult, result, progress) if err != nil { handleError(importResult, err, out, progress) diff --git a/backends/spotify/spotify.go b/backends/spotify/spotify.go index bb11c63..426e2af 100644 --- a/backends/spotify/spotify.go +++ b/backends/spotify/spotify.go @@ -139,6 +139,7 @@ func (b *SpotifyApiBackend) ExportLoves(oldestTimestamp time.Time, results chan defer close(progress) p := models.Progress{Total: int64(perPage)} + var totalCount int out: for { @@ -153,6 +154,7 @@ out: // and continue. if offset >= result.Total { p.Total = int64(result.Total) + totalCount = result.Total offset = result.Total - perPage if offset < 0 { offset = 0 @@ -169,15 +171,16 @@ out: for _, track := range result.Items { love := track.ToLove() if love.Created.Unix() > oldestTimestamp.Unix() { - p.Elapsed += 1 loves = append(loves, love) } else { + totalCount -= 1 break } } sort.Sort(loves) - results <- models.LovesResult{Loves: loves} + results <- models.LovesResult{Loves: loves, Total: totalCount} + p.Elapsed += int64(count) progress <- p if offset <= 0 { diff --git a/models/models.go b/models/models.go index fca0ece..a9e91e2 100644 --- a/models/models.go +++ b/models/models.go @@ -106,12 +106,14 @@ func (l LovesList) Swap(i, j int) { } type ListensResult struct { + Total int Listens ListensList OldestTimestamp time.Time Error error } type LovesResult struct { + Total int Loves LovesList Error error }