Finish config mapping
This commit is contained in:
@@ -84,11 +84,17 @@ func UnrestrictLink(accessToken, link string) (*UnrestrictResponse, error) {
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func GetTorrents(accessToken string) ([]Torrent, error) {
|
||||
// GetTorrents returns all torrents, paginated
|
||||
// if customLimit is 0, the default limit of 2500 is used
|
||||
func GetTorrents(accessToken string, customLimit int) ([]Torrent, int, error) {
|
||||
baseURL := "https://api.real-debrid.com/rest/1.0/torrents"
|
||||
var allTorrents []Torrent
|
||||
page := 1
|
||||
limit := 100
|
||||
limit := customLimit
|
||||
if limit == 0 {
|
||||
limit = 2500
|
||||
}
|
||||
totalCount := 0
|
||||
|
||||
for {
|
||||
params := url.Values{}
|
||||
@@ -99,7 +105,7 @@ func GetTorrents(accessToken string) ([]Torrent, error) {
|
||||
|
||||
req, err := http.NewRequest("GET", reqURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
@@ -107,37 +113,37 @@ func GetTorrents(accessToken string) ([]Torrent, error) {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
return nil, 0, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
var torrents []Torrent
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
err = decoder.Decode(&torrents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
allTorrents = append(allTorrents, torrents...)
|
||||
|
||||
totalCountHeader := "100" // resp.Header.Get("x-total-count")
|
||||
totalCountHeader := resp.Header.Get("x-total-count")
|
||||
totalCount, err := strconv.Atoi(totalCountHeader)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
if len(torrents) < limit || len(allTorrents) >= totalCount {
|
||||
if len(allTorrents) >= totalCount || (customLimit != 0 && customLimit <= len(allTorrents) && customLimit <= totalCount) {
|
||||
break
|
||||
}
|
||||
|
||||
page++
|
||||
}
|
||||
|
||||
return allTorrents, nil
|
||||
return allTorrents, totalCount, nil
|
||||
}
|
||||
|
||||
func GetTorrentInfo(accessToken, id string) (*Torrent, error) {
|
||||
|
||||
@@ -7,11 +7,11 @@ import (
|
||||
)
|
||||
|
||||
func RetryUntilOk[T any](fn func() (T, error)) T {
|
||||
const initialDelay = 2 * time.Second
|
||||
const initialDelay = 1 * time.Second
|
||||
const maxDelay = 128 * time.Second
|
||||
for i := 0; ; i++ {
|
||||
result, err := fn()
|
||||
if err == nil || strings.Contains(err.Error(), "404") {
|
||||
if err == nil || !strings.Contains(err.Error(), "429") {
|
||||
return result
|
||||
}
|
||||
delay := time.Duration(math.Min(float64(initialDelay*time.Duration(math.Pow(2, float64(i)))), float64(maxDelay)))
|
||||
|
||||
Reference in New Issue
Block a user