Fix unrestrict issue

This commit is contained in:
Ben Sarmiento
2023-11-28 00:41:15 +01:00
parent c8334ecb3b
commit 3d380e468f
9 changed files with 131 additions and 68 deletions

View File

@@ -26,7 +26,7 @@ func NewRealDebrid(client *zurghttp.HTTPClient, log *zap.SugaredLogger) *RealDeb
}
}
func (rd *RealDebrid) UnrestrictCheck(link string) (*UnrestrictResponse, error) {
func (rd *RealDebrid) UnrestrictCheck(link string) (*Download, error) {
data := url.Values{}
data.Set("link", link)
@@ -50,7 +50,7 @@ func (rd *RealDebrid) UnrestrictCheck(link string) (*UnrestrictResponse, error)
return nil, err
}
var response UnrestrictResponse
var response Download
err = json.Unmarshal(body, &response)
if err != nil {
rd.log.Errorf("Error when decoding unrestrict check JSON: %v", err)
@@ -62,7 +62,7 @@ func (rd *RealDebrid) UnrestrictCheck(link string) (*UnrestrictResponse, error)
}
// GetTorrents returns all torrents, paginated
// if customLimit is 0, the default limit of 2500 is used
// if customLimit is 0, the default limit of 1000 is used
func (rd *RealDebrid) GetTorrents(customLimit int) ([]Torrent, int, error) {
baseURL := "https://api.real-debrid.com/rest/1.0/torrents"
var allTorrents []Torrent
@@ -116,6 +116,8 @@ func (rd *RealDebrid) GetTorrents(customLimit int) ([]Torrent, int, error) {
break
}
rd.log.Debugf("Got %d torrents (page %d), total count is %d", len(allTorrents), page, totalCount)
page++
}
return allTorrents, totalCount, nil
@@ -263,7 +265,8 @@ func (rd *RealDebrid) GetActiveTorrentCount() (*ActiveTorrentCountResponse, erro
return &response, nil
}
func (rd *RealDebrid) UnrestrictLink(link string, checkFirstByte bool) (*UnrestrictResponse, error) {
func (rd *RealDebrid) UnrestrictLink(link string, checkFirstByte bool) (*Download, error) {
fmt.Println("Unrestricting link via api", link)
data := url.Values{}
data.Set("link", link)
@@ -294,7 +297,7 @@ func (rd *RealDebrid) UnrestrictLink(link string, checkFirstByte bool) (*Unrestr
return nil, fmt.Errorf("unreadable body so likely it has expired")
}
var response UnrestrictResponse
var response Download
err = json.Unmarshal(body, &response)
if err != nil {
// rd.log.Errorf("Error when decoding unrestrict link JSON: %v", err)
@@ -308,3 +311,61 @@ func (rd *RealDebrid) UnrestrictLink(link string, checkFirstByte bool) (*Unrestr
// rd.log.Debugf("Unrestricted link %s into %s", link, response.Download)
return &response, nil
}
// GetDownloads returns all torrents, paginated
func (rd *RealDebrid) GetDownloads() ([]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")
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++
}
return allDownloads, totalCount, nil
}