mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-18 19:19:28 +02:00
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:
parent
6047a9b274
commit
49cb2774e2
2 changed files with 19 additions and 10 deletions
|
@ -22,6 +22,7 @@ THE SOFTWARE.
|
||||||
package backends
|
package backends
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"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) {
|
func (b ListenBrainzApiBackend) ExportListens(oldestTimestamp time.Time) ([]Listen, error) {
|
||||||
minTs := oldestTimestamp
|
maxTime := time.Now()
|
||||||
|
minTime := time.Unix(0, 0)
|
||||||
listens := make([]Listen, 0)
|
listens := make([]Listen, 0)
|
||||||
|
|
||||||
|
out:
|
||||||
for {
|
for {
|
||||||
result, err := b.client.GetListens(b.username, minTs)
|
result, err := b.client.GetListens(b.username, maxTime, minTime)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -55,16 +58,21 @@ func (b ListenBrainzApiBackend) ExportListens(oldestTimestamp time.Time) ([]List
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set minTs to the newest returned listen
|
// Set maxTime to the oldest returned listen
|
||||||
minTs = time.Unix(result.Payload.Listens[0].ListenedAt, 0)
|
maxTime = time.Unix(result.Payload.Listens[count-1].ListenedAt, 0)
|
||||||
|
|
||||||
// Iterate over the returned listens in reverse order (oldest first)
|
for _, listen := range result.Payload.Listens {
|
||||||
for i := count - 1; i >= 0; i-- {
|
if listen.ListenedAt > oldestTimestamp.Unix() {
|
||||||
lbListen := result.Payload.Listens[i]
|
listens = append(listens, Listen{}.FromListenBrainz(listen))
|
||||||
listens = append(listens, Listen{}.FromListenBrainz(lbListen))
|
} else {
|
||||||
|
// result contains listens older then oldestTimestamp,
|
||||||
|
// we can stop requesting more
|
||||||
|
break out
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slices.Reverse(listens)
|
||||||
return listens, nil
|
return listens, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,13 +52,14 @@ func New(token string) Client {
|
||||||
return 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"
|
const path = "/user/{username}/listens"
|
||||||
result := &GetListensResult{}
|
result := &GetListensResult{}
|
||||||
_, err := c.resty.R().
|
_, err := c.resty.R().
|
||||||
SetPathParam("username", user).
|
SetPathParam("username", user).
|
||||||
SetQueryParams(map[string]string{
|
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),
|
"count": strconv.FormatInt(int64(c.MaxResults), 10),
|
||||||
}).
|
}).
|
||||||
SetResult(result).
|
SetResult(result).
|
||||||
|
|
Loading…
Add table
Reference in a new issue