Files
zurg/pkg/realdebrid/api.go
Ben Sarmiento c8334ecb3b Hotfix
2023-11-27 21:50:00 +01:00

311 lines
8.8 KiB
Go

package realdebrid
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
"go.uber.org/zap"
)
type RealDebrid struct {
log *zap.SugaredLogger
client *zurghttp.HTTPClient
}
func NewRealDebrid(client *zurghttp.HTTPClient, log *zap.SugaredLogger) *RealDebrid {
return &RealDebrid{
log: log,
client: client,
}
}
func (rd *RealDebrid) UnrestrictCheck(link string) (*UnrestrictResponse, error) {
data := url.Values{}
data.Set("link", link)
req, err := http.NewRequest("POST", "https://api.real-debrid.com/rest/1.0/unrestrict/check", bytes.NewBufferString(data.Encode()))
if err != nil {
rd.log.Errorf("Error when creating a unrestrict check request: %v", err)
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := rd.client.Do(req)
if err != nil {
rd.log.Errorf("Error when executing the unrestrict check request: %v", err)
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
rd.log.Errorf("Error when reading the body of unrestrict check response: %v", err)
return nil, err
}
var response UnrestrictResponse
err = json.Unmarshal(body, &response)
if err != nil {
rd.log.Errorf("Error when decoding unrestrict check JSON: %v", err)
return nil, err
}
rd.log.Info("Link %s is streamable? %v", response.Streamable)
return &response, nil
}
// GetTorrents returns all torrents, paginated
// if customLimit is 0, the default limit of 2500 is used
func (rd *RealDebrid) GetTorrents(customLimit int) ([]Torrent, int, error) {
baseURL := "https://api.real-debrid.com/rest/1.0/torrents"
var allTorrents []Torrent
page := 1
limit := customLimit
if limit == 0 {
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 torrents request: %v", err)
return nil, 0, err
}
resp, err := rd.client.Do(req)
if err != nil {
rd.log.Errorf("Error when executing the get torrents request: %v", err)
return nil, 0, err
}
defer resp.Body.Close()
// if status code is not 2xx, return erro
var torrents []Torrent
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&torrents)
if err != nil {
rd.log.Errorf("Error when decoding get torrents JSON: %v", err)
return nil, 0, err
}
allTorrents = append(allTorrents, torrents...)
totalCountHeader := resp.Header.Get("x-total-count")
totalCount, err = strconv.Atoi(totalCountHeader)
if err != nil {
break
}
if len(allTorrents) >= totalCount || (customLimit != 0 && customLimit <= len(allTorrents) && customLimit <= totalCount) {
break
}
page++
}
return allTorrents, totalCount, nil
}
func (rd *RealDebrid) GetTorrentInfo(id string) (*TorrentInfo, error) {
url := "https://api.real-debrid.com/rest/1.0/torrents/info/" + id
req, err := http.NewRequest("GET", url, nil)
if err != nil {
rd.log.Errorf("Error when creating a get info request: %v", err)
return nil, err
}
resp, err := rd.client.Do(req)
if err != nil {
rd.log.Errorf("Error when executing the get info request: %v", err)
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
rd.log.Errorf("Error when reading the body of get info response: %v", err)
return nil, err
}
var response TorrentInfo
err = json.Unmarshal(body, &response)
if err != nil {
rd.log.Errorf("Error when : %v", err)
return nil, err
}
rd.log.Debugf("Got info for torrent %s (progress=%d%%)", id, response.Progress)
return &response, nil
}
// SelectTorrentFiles selects files of a torrent to start it.
func (rd *RealDebrid) SelectTorrentFiles(id string, files string) error {
data := url.Values{}
data.Set("files", files)
reqURL := fmt.Sprintf("https://api.real-debrid.com/rest/1.0/torrents/selectFiles/%s", id)
req, err := http.NewRequest("POST", reqURL, bytes.NewBufferString(data.Encode()))
if err != nil {
rd.log.Errorf("Error when creating a select files request: %v", err)
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := rd.client.Do(req)
if err != nil {
rd.log.Errorf("Error when executing the select files request: %v", err)
return err
}
defer resp.Body.Close()
rd.log.Debugf("Selected %d files and started the download for torrent id=%s", len(strings.Split(files, ",")), id)
return nil
}
// DeleteTorrent deletes a torrent from the torrents list.
func (rd *RealDebrid) DeleteTorrent(id string) error {
// Construct request URL
reqURL := fmt.Sprintf("https://api.real-debrid.com/rest/1.0/torrents/delete/%s", id)
req, err := http.NewRequest("DELETE", reqURL, nil)
if err != nil {
rd.log.Errorf("Error when creating a delete torrent request: %v", err)
return err
}
// Send the request
resp, err := rd.client.Do(req)
if err != nil {
rd.log.Errorf("Error when executing the delete torrent request: %v", err)
return err
}
defer resp.Body.Close()
rd.log.Debugf("Deleted torrent with id=%s", id)
return nil
}
// AddMagnetHash adds a magnet link to download.
func (rd *RealDebrid) AddMagnetHash(magnet string) (*MagnetResponse, error) {
// Prepare request data
data := url.Values{}
data.Set("magnet", fmt.Sprintf("magnet:?xt=urn:btih:%s", magnet))
// Construct request URL
reqURL := "https://api.real-debrid.com/rest/1.0/torrents/addMagnet"
req, err := http.NewRequest("POST", reqURL, bytes.NewBufferString(data.Encode()))
if err != nil {
rd.log.Errorf("Error when creating an add magnet request: %v", err)
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Send the request
resp, err := rd.client.Do(req)
if err != nil {
rd.log.Errorf("Error when executing the add magnet request: %v", err)
return nil, err
}
defer resp.Body.Close()
var response MagnetResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
rd.log.Errorf("Error when decoding add magnet JSON: %v", err)
return nil, err
}
rd.log.Debugf("Added magnet %s with id=%s", magnet, response.ID)
return &response, nil
}
// GetActiveTorrentCount gets the number of currently active torrents and the current maximum limit.
func (rd *RealDebrid) GetActiveTorrentCount() (*ActiveTorrentCountResponse, error) {
// Construct request URL
reqURL := "https://api.real-debrid.com/rest/1.0/torrents/activeCount"
req, err := http.NewRequest("GET", reqURL, nil)
if err != nil {
rd.log.Errorf("Error when creating a active torrents request: %v", err)
return nil, err
}
// Send the request
resp, err := rd.client.Do(req)
if err != nil {
rd.log.Errorf("Error when executing the active torrents request: %v", err)
return nil, err
}
defer resp.Body.Close()
var response ActiveTorrentCountResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
rd.log.Errorf("Error when decoding active torrents JSON: %v", err)
return nil, err
}
return &response, nil
}
func (rd *RealDebrid) UnrestrictLink(link string, checkFirstByte bool) (*UnrestrictResponse, error) {
data := url.Values{}
data.Set("link", link)
req, err := http.NewRequest("POST", "https://api.real-debrid.com/rest/1.0/unrestrict/link", bytes.NewBufferString(data.Encode()))
if err != nil {
rd.log.Errorf("Error when creating a unrestrict link request: %v", err)
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// at this point, any errors mean that the link has expired and we need to repair it
resp, err := rd.client.Do(req)
if err != nil {
// rd.log.Errorf("Error when executing the unrestrict link request: %v", err)
return nil, fmt.Errorf("unrestrict link request failed so likely it has expired")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
rd.log.Errorf("Unrestrict link request returned status code %d for link %s", resp.StatusCode, link)
// return nil, fmt.Errorf("unrestrict link request returned status code %d so likely it has expired", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
// rd.log.Errorf("Error when reading the body of unrestrict link response: %v", err)
return nil, fmt.Errorf("unreadable body so likely it has expired")
}
var response UnrestrictResponse
err = json.Unmarshal(body, &response)
if err != nil {
// rd.log.Errorf("Error when decoding unrestrict link JSON: %v", err)
return nil, fmt.Errorf("undecodable response so likely it has expired")
}
if checkFirstByte && !rd.canFetchFirstByte(response.Download) {
return nil, fmt.Errorf("can't fetch first byte")
}
// rd.log.Debugf("Unrestricted link %s into %s", link, response.Download)
return &response, nil
}