From 1af484e0f6103a5c213c915e6baf1d9cf4653cb2 Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Tue, 21 Nov 2023 18:14:11 +0100 Subject: [PATCH] Fixed storing last import timestamp --- backends/spotify/spotify.go | 44 +++++++++++++++++++++++-------------- cmd/listens.go | 3 +++ cmd/loves.go | 3 +++ 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/backends/spotify/spotify.go b/backends/spotify/spotify.go index cd69696..a1e1bbc 100644 --- a/backends/spotify/spotify.go +++ b/backends/spotify/spotify.go @@ -23,7 +23,6 @@ THE SOFTWARE. package spotify import ( - "errors" "sort" "strconv" "time" @@ -75,6 +74,7 @@ func (b *SpotifyApiBackend) ExportListens(oldestTimestamp time.Time, results cha p := models.Progress{Total: int64(totalDuration.Seconds())} +out: for { result, err := b.client.RecentlyPlayedAfter(minTime, MaxItemsPerGet) if err != nil { @@ -83,6 +83,23 @@ func (b *SpotifyApiBackend) ExportListens(oldestTimestamp time.Time, results cha return } + if result.Cursors.After == "" { + break + } + + // Set minTime to the newest returned listen + after, err := strconv.ParseInt(result.Cursors.After, 10, 64) + if err != nil { + progress <- p.Complete() + results <- models.ListensResult{Error: err} + return + } else if after <= minTime.Unix() { + // new cursor timestamp did not progress + break + } + minTime = time.Unix(after, 0) + remainingTime := startTime.Sub(minTime) + count := len(result.Items) if count == 0 { break @@ -90,29 +107,24 @@ func (b *SpotifyApiBackend) ExportListens(oldestTimestamp time.Time, results cha listens := make(models.ListensList, 0, len(result.Items)) - // Set minTime to the newest returned listen - after, err := strconv.ParseInt(result.Cursors.After, 10, 64) - if err != nil && after <= minTime.Unix() { - err = errors.New("new cursor timestamp did not progress") - } - if err != nil { - progress <- p.Complete() - results <- models.ListensResult{Error: err} - return - } - minTime = time.Unix(after, 0) - remainingTime := startTime.Sub(minTime) - for _, listen := range result.Items { - listens = append(listens, listen.ToListen()) + l := listen.ToListen() + if l.ListenedAt.Unix() > oldestTimestamp.Unix() { + listens = append(listens, l) + } else { + // result contains listens older then oldestTimestamp, + // we can stop requesting more + break out + } } sort.Sort(listens) p.Elapsed = int64(totalDuration.Seconds() - remainingTime.Seconds()) progress <- p - results <- models.ListensResult{Listens: listens, OldestTimestamp: oldestTimestamp} + results <- models.ListensResult{Listens: listens, OldestTimestamp: minTime} } + results <- models.ListensResult{OldestTimestamp: minTime} progress <- p.Complete() } diff --git a/cmd/listens.go b/cmd/listens.go index a564866..4226605 100644 --- a/cmd/listens.go +++ b/cmd/listens.go @@ -102,6 +102,9 @@ var listensCmd = &cobra.Command{ result.ImportCount, result.TotalCount, targetName) // Update timestamp + if result.LastTimestamp.Unix() < timestamp.Unix() { + result.LastTimestamp = timestamp + } fmt.Printf("Latest timestamp: %v (%v)\n", result.LastTimestamp, result.LastTimestamp.Unix()) err = db.SetImportTimestamp(sourceName, targetName, "listens", result.LastTimestamp) cobra.CheckErr(err) diff --git a/cmd/loves.go b/cmd/loves.go index 2ec458b..cc2a94f 100644 --- a/cmd/loves.go +++ b/cmd/loves.go @@ -102,6 +102,9 @@ var lovesCmd = &cobra.Command{ result.ImportCount, result.TotalCount, targetName) // Update timestamp + if result.LastTimestamp.Unix() < timestamp.Unix() { + result.LastTimestamp = timestamp + } fmt.Printf("Latest timestamp: %v (%v)\n", result.LastTimestamp, result.LastTimestamp.Unix()) err = db.SetImportTimestamp(sourceName, targetName, "loves", result.LastTimestamp) cobra.CheckErr(err)