Cache for directories and torrents

This commit is contained in:
Ben Sarmiento
2023-11-30 03:16:10 +01:00
parent 253b92a3b6
commit 5914af80fd
6 changed files with 226 additions and 177 deletions

View File

@@ -312,59 +312,51 @@ func (rd *RealDebrid) UnrestrictLink(link string, checkFirstByte bool) (*Downloa
}
// GetDownloads returns all torrents, paginated
func (rd *RealDebrid) GetDownloads() ([]Download, int, error) {
func (rd *RealDebrid) GetDownloads(page, offset int) ([]Download, int, error) {
baseURL := "https://api.real-debrid.com/rest/1.0/downloads"
var allDownloads []Download
page := 1
limit := 1000
totalCount := 0
for {
params := url.Values{}
params.Set("page", fmt.Sprintf("%d", page))
params.Set("limit", fmt.Sprintf("%d", limit))
// params.Set("filter", "active")
params := url.Values{}
params.Set("page", fmt.Sprintf("%d", page))
params.Set("offset", fmt.Sprintf("%d", offset))
params.Set("limit", fmt.Sprintf("%d", limit))
// params.Set("filter", "active")
reqURL := baseURL + "?" + params.Encode()
reqURL := baseURL + "?" + params.Encode()
req, err := http.NewRequest("GET", reqURL, nil)
if err != nil {
rd.log.Errorf("Error when creating a get downloads request: %v", err)
return nil, 0, err
}
resp, err := rd.client.Do(req)
if err != nil {
rd.log.Errorf("Error when executing the get downloads request: %v", err)
return nil, 0, err
}
defer resp.Body.Close()
// if status code is not 2xx, return erro
var downloads []Download
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&downloads)
if err != nil {
rd.log.Errorf("Error when decoding get downloads JSON: %v", err)
return nil, 0, err
}
allDownloads = append(allDownloads, downloads...)
totalCountHeader := resp.Header.Get("x-total-count")
totalCount, err = strconv.Atoi(totalCountHeader)
if err != nil {
break
}
if len(allDownloads) >= totalCount {
break
}
rd.log.Debugf("Got %d downloads (page %d), total count is %d", len(allDownloads), page, totalCount)
page++
req, err := http.NewRequest("GET", reqURL, nil)
if err != nil {
rd.log.Errorf("Error when creating a get downloads request: %v", err)
return nil, 0, err
}
resp, err := rd.client.Do(req)
if err != nil {
rd.log.Errorf("Error when executing the get downloads request: %v", err)
return nil, 0, err
}
defer resp.Body.Close()
// if status code is not 2xx, return erro
var downloads []Download
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&downloads)
if err != nil {
rd.log.Errorf("Error when decoding get downloads JSON: %v", err)
return nil, 0, err
}
allDownloads = append(allDownloads, downloads...)
totalCountHeader := resp.Header.Get("x-total-count")
totalCount, err = strconv.Atoi(totalCountHeader)
if err != nil {
totalCount = 0
}
rd.log.Debugf("Got %d downloads (page %d), total count is %d", len(allDownloads), page, totalCount)
return allDownloads, totalCount, nil
}