mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 10:09:28 +02:00
68 lines
2.1 KiB
Go
68 lines
2.1 KiB
Go
/*
|
|
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package listenbrainz
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
)
|
|
|
|
const listenBrainzBaseURL = "https://api.listenbrainz.org/1/"
|
|
|
|
const DefaultItemsPerGet = 25
|
|
const MaxItemsPerGet = 1000
|
|
|
|
type Client struct {
|
|
HttpClient *resty.Client
|
|
MaxResults int
|
|
}
|
|
|
|
func New(token string) Client {
|
|
resty := resty.New()
|
|
resty.SetBaseURL(listenBrainzBaseURL)
|
|
resty.SetAuthScheme("Token")
|
|
resty.SetAuthToken(token)
|
|
resty.SetHeader("Accept", "application/json")
|
|
client := Client{
|
|
HttpClient: resty,
|
|
MaxResults: DefaultItemsPerGet,
|
|
}
|
|
|
|
return client
|
|
}
|
|
|
|
func (c Client) GetListens(user string, maxTime time.Time, minTime time.Time) (GetListensResult, error) {
|
|
const path = "/user/{username}/listens"
|
|
result := &GetListensResult{}
|
|
_, 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).
|
|
Get(path)
|
|
return *result, err
|
|
}
|