listenbrainz: Read data from newest first

Very low min_ts parameters lead to empty results from LB and there is no
easy way to determine the oldest valid timestamp.
This commit is contained in:
Philipp Wolfer 2023-11-10 00:56:43 +01:00
parent 6047a9b274
commit 49cb2774e2
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
2 changed files with 19 additions and 10 deletions

View file

@ -22,6 +22,7 @@ THE SOFTWARE.
package backends
import (
"slices"
"time"
"github.com/spf13/viper"
@ -41,11 +42,13 @@ func (b ListenBrainzApiBackend) FromConfig(config *viper.Viper) Backend {
}
func (b ListenBrainzApiBackend) ExportListens(oldestTimestamp time.Time) ([]Listen, error) {
minTs := oldestTimestamp
maxTime := time.Now()
minTime := time.Unix(0, 0)
listens := make([]Listen, 0)
out:
for {
result, err := b.client.GetListens(b.username, minTs)
result, err := b.client.GetListens(b.username, maxTime, minTime)
if err != nil {
return nil, err
}
@ -55,16 +58,21 @@ func (b ListenBrainzApiBackend) ExportListens(oldestTimestamp time.Time) ([]List
break
}
// Set minTs to the newest returned listen
minTs = time.Unix(result.Payload.Listens[0].ListenedAt, 0)
// Set maxTime to the oldest returned listen
maxTime = time.Unix(result.Payload.Listens[count-1].ListenedAt, 0)
// Iterate over the returned listens in reverse order (oldest first)
for i := count - 1; i >= 0; i-- {
lbListen := result.Payload.Listens[i]
listens = append(listens, Listen{}.FromListenBrainz(lbListen))
for _, listen := range result.Payload.Listens {
if listen.ListenedAt > oldestTimestamp.Unix() {
listens = append(listens, Listen{}.FromListenBrainz(listen))
} else {
// result contains listens older then oldestTimestamp,
// we can stop requesting more
break out
}
}
}
slices.Reverse(listens)
return listens, nil
}

View file

@ -52,13 +52,14 @@ func New(token string) Client {
return client
}
func (c Client) GetListens(user string, minTs time.Time) (GetListensResult, error) {
func (c Client) GetListens(user string, maxTime time.Time, minTime time.Time) (GetListensResult, error) {
const path = "/user/{username}/listens"
result := &GetListensResult{}
_, err := c.resty.R().
SetPathParam("username", user).
SetQueryParams(map[string]string{
"min_ts": strconv.FormatInt(minTs.Unix(), 10),
"max_ts": strconv.FormatInt(maxTime.Unix(), 10),
"min_ts": strconv.FormatInt(minTime.Unix(), 10),
"count": strconv.FormatInt(int64(c.MaxResults), 10),
}).
SetResult(result).