mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-30 13:47:05 +02:00
Unified code for backend clients and tests
This commit is contained in:
parent
9316838d59
commit
aa01ae1342
11 changed files with 220 additions and 42 deletions
|
@ -22,6 +22,7 @@ THE SOFTWARE.
|
|||
package listenbrainz
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
@ -52,17 +53,21 @@ func NewClient(token string) Client {
|
|||
return client
|
||||
}
|
||||
|
||||
func (c Client) GetListens(user string, maxTime time.Time, minTime time.Time) (GetListensResult, error) {
|
||||
func (c Client) GetListens(user string, maxTime time.Time, minTime time.Time) (result GetListensResult, err error) {
|
||||
const path = "/user/{username}/listens"
|
||||
result := &GetListensResult{}
|
||||
_, err := c.HttpClient.R().
|
||||
response, err := c.HttpClient.R().
|
||||
SetPathParam("username", user).
|
||||
SetQueryParams(map[string]string{
|
||||
"max_ts": strconv.FormatInt(maxTime.Unix(), 10),
|
||||
"min_ts": strconv.FormatInt(minTime.Unix(), 10),
|
||||
"count": strconv.FormatInt(int64(c.MaxResults), 10),
|
||||
}).
|
||||
SetResult(result).
|
||||
SetResult(&result).
|
||||
Get(path)
|
||||
return *result, err
|
||||
|
||||
if response.StatusCode() != 200 {
|
||||
err = errors.New(response.String())
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -53,6 +53,7 @@ func TestGetListens(t *testing.T) {
|
|||
|
||||
assert := assert.New(t)
|
||||
assert.Equal(2, result.Payload.Count)
|
||||
require.Len(t, result.Payload.Listens, 2)
|
||||
assert.Equal("Shadowplay", result.Payload.Listens[0].TrackMetadata.TrackName)
|
||||
}
|
||||
|
||||
|
|
|
@ -63,7 +63,7 @@ out:
|
|||
|
||||
for _, listen := range result.Payload.Listens {
|
||||
if listen.ListenedAt > oldestTimestamp.Unix() {
|
||||
listens = append(listens, ListenFromListenBrainz(listen))
|
||||
listens = append(listens, listen.ToListen())
|
||||
} else {
|
||||
// result contains listens older then oldestTimestamp,
|
||||
// we can stop requesting more
|
||||
|
@ -76,7 +76,7 @@ out:
|
|||
return listens, nil
|
||||
}
|
||||
|
||||
func ListenFromListenBrainz(lbListen Listen) models.Listen {
|
||||
func (lbListen Listen) ToListen() models.Listen {
|
||||
track := lbListen.TrackMetadata
|
||||
listen := models.Listen{
|
||||
ListenedAt: time.Unix(lbListen.ListenedAt, 0),
|
||||
|
|
|
@ -30,36 +30,36 @@ import (
|
|||
"go.uploadedlobster.com/scotty/models"
|
||||
)
|
||||
|
||||
func TestListenFromListenBrainz(t *testing.T) {
|
||||
func TestListenBrainzListenToListen(t *testing.T) {
|
||||
lbListen := listenbrainz.Listen{
|
||||
ListenedAt: 1699289873,
|
||||
UserName: "outsidecontext",
|
||||
TrackMetadata: listenbrainz.Track{
|
||||
TrackName: "The Track",
|
||||
ArtistName: "The Artist",
|
||||
ReleaseName: "The Release",
|
||||
ArtistName: "Dool",
|
||||
ReleaseName: "Here Now, There Then",
|
||||
AdditionalInfo: map[string]any{
|
||||
"duration_ms": 528235,
|
||||
"duration_ms": 413787,
|
||||
"foo": "bar",
|
||||
"isrc": "DES561720901",
|
||||
"tracknumber": 8,
|
||||
"recording_mbid": "e225fb84-dc9a-419e-adcd-9890f59ec432",
|
||||
"isrc": "DES561620801",
|
||||
"tracknumber": 5,
|
||||
"recording_mbid": "c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12",
|
||||
"release_group_mbid": "80aca1ee-aa51-41be-9f75-024710d92ff4",
|
||||
"release_mbid": "d7f22677-9803-4d21-ba42-081b633a6f68",
|
||||
},
|
||||
},
|
||||
}
|
||||
listen := listenbrainz.ListenFromListenBrainz(lbListen)
|
||||
listen := lbListen.ToListen()
|
||||
assert.Equal(t, time.Unix(1699289873, 0), listen.ListenedAt)
|
||||
assert.Equal(t, lbListen.UserName, listen.UserName)
|
||||
assert.Equal(t, time.Duration(528235*time.Millisecond), listen.Duration)
|
||||
assert.Equal(t, time.Duration(413787*time.Millisecond), listen.Duration)
|
||||
assert.Equal(t, lbListen.TrackMetadata.TrackName, listen.TrackName)
|
||||
assert.Equal(t, lbListen.TrackMetadata.ReleaseName, listen.ReleaseName)
|
||||
assert.Equal(t, []string{lbListen.TrackMetadata.ArtistName}, listen.ArtistNames)
|
||||
assert.Equal(t, 8, listen.TrackNumber)
|
||||
assert.Equal(t, models.MBID("e225fb84-dc9a-419e-adcd-9890f59ec432"), listen.RecordingMbid)
|
||||
assert.Equal(t, 5, listen.TrackNumber)
|
||||
assert.Equal(t, models.MBID("c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12"), listen.RecordingMbid)
|
||||
assert.Equal(t, models.MBID("d7f22677-9803-4d21-ba42-081b633a6f68"), listen.ReleaseMbid)
|
||||
assert.Equal(t, models.MBID("80aca1ee-aa51-41be-9f75-024710d92ff4"), listen.ReleaseGroupMbid)
|
||||
assert.Equal(t, "DES561720901", listen.Isrc)
|
||||
assert.Equal(t, "DES561620801", listen.Isrc)
|
||||
assert.Equal(t, lbListen.TrackMetadata.AdditionalInfo["foo"], listen.AdditionalInfo["foo"])
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue