mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 10:09:28 +02:00
Fixed rate limit check for ListenBrainz and Funkwhale. Also retry always on server side errors.
154 lines
4.5 KiB
Go
154 lines
4.5 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 (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
)
|
|
|
|
const listenBrainzBaseURL = "https://api.listenbrainz.org/1/"
|
|
|
|
const DefaultItemsPerGet = 25
|
|
const MaxItemsPerGet = 1000
|
|
const DefaultRateLimitWaitSeconds = 5
|
|
|
|
type Client struct {
|
|
HttpClient *resty.Client
|
|
MaxResults int
|
|
}
|
|
|
|
func NewClient(token string) Client {
|
|
client := resty.New()
|
|
client.SetBaseURL(listenBrainzBaseURL)
|
|
client.SetAuthScheme("Token")
|
|
client.SetAuthToken(token)
|
|
client.SetHeader("Accept", "application/json")
|
|
|
|
// Handle rate limiting (see https://listenbrainz.readthedocs.io/en/latest/users/api/index.html#rate-limiting)
|
|
client.SetRetryCount(5)
|
|
client.AddRetryCondition(
|
|
func(r *resty.Response, err error) bool {
|
|
code := r.StatusCode()
|
|
return code == http.StatusTooManyRequests || code >= http.StatusInternalServerError
|
|
},
|
|
)
|
|
client.SetRetryMaxWaitTime(time.Duration(1 * time.Minute))
|
|
client.SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
|
|
var err error
|
|
var retryAfter int = DefaultRateLimitWaitSeconds
|
|
if resp.StatusCode() == http.StatusTooManyRequests {
|
|
retryAfter, err = strconv.Atoi(resp.Header().Get("X-RateLimit-Reset-In"))
|
|
if err != nil {
|
|
retryAfter = DefaultRateLimitWaitSeconds
|
|
}
|
|
}
|
|
return time.Duration(retryAfter * int(time.Second)), err
|
|
})
|
|
|
|
return Client{
|
|
HttpClient: client,
|
|
MaxResults: DefaultItemsPerGet,
|
|
}
|
|
}
|
|
|
|
func (c Client) GetListens(user string, maxTime time.Time, minTime time.Time) (result GetListensResult, err error) {
|
|
const path = "/user/{username}/listens"
|
|
errorResult := ErrorResult{}
|
|
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.Itoa(c.MaxResults),
|
|
}).
|
|
SetResult(&result).
|
|
SetError(&errorResult).
|
|
Get(path)
|
|
|
|
if response.StatusCode() != 200 {
|
|
err = errors.New(errorResult.Error)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (c Client) GetFeedback(user string, status int, offset int) (result GetFeedbackResult, err error) {
|
|
const path = "/feedback/user/{username}/get-feedback"
|
|
errorResult := ErrorResult{}
|
|
response, err := c.HttpClient.R().
|
|
SetPathParam("username", user).
|
|
SetQueryParams(map[string]string{
|
|
"status": strconv.Itoa(status),
|
|
"offset": strconv.Itoa(offset),
|
|
"count": strconv.Itoa(c.MaxResults),
|
|
"metadata": "true",
|
|
}).
|
|
SetResult(&result).
|
|
SetError(&errorResult).
|
|
Get(path)
|
|
|
|
if response.StatusCode() != 200 {
|
|
err = errors.New(errorResult.Error)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (c Client) SendFeedback(feedback Feedback) (result StatusResult, err error) {
|
|
const path = "/feedback/recording-feedback"
|
|
errorResult := ErrorResult{}
|
|
response, err := c.HttpClient.R().
|
|
SetBody(feedback).
|
|
SetResult(&result).
|
|
SetError(&errorResult).
|
|
Post(path)
|
|
|
|
if response.StatusCode() != 200 {
|
|
err = errors.New(errorResult.Error)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func (c Client) Lookup(recordingName string, artistName string) (result LookupResult, err error) {
|
|
const path = "/metadata/lookup"
|
|
errorResult := ErrorResult{}
|
|
response, err := c.HttpClient.R().
|
|
SetQueryParams(map[string]string{
|
|
"recording_name": recordingName,
|
|
"artist_name": artistName,
|
|
}).
|
|
SetResult(&result).
|
|
SetError(&errorResult).
|
|
Get(path)
|
|
|
|
if response.StatusCode() != 200 {
|
|
err = errors.New(errorResult.Error)
|
|
return
|
|
}
|
|
return
|
|
}
|