Finish config mapping

This commit is contained in:
Ben Sarmiento
2023-10-19 18:02:30 +02:00
parent ce0b2445b2
commit faba4e53ab
14 changed files with 397 additions and 89 deletions

View File

@@ -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) {