Create a torrentMap
This commit is contained in:
@@ -3,94 +3,77 @@ package realdebrid
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func UnrestrictCheck(accessToken, link string) (*UnrestrictResponse, error) {
|
||||
type RealDebrid struct {
|
||||
log *zap.SugaredLogger
|
||||
client *zurghttp.HTTPClient
|
||||
}
|
||||
|
||||
func NewRealDebrid(accessToken string, log *zap.SugaredLogger) *RealDebrid {
|
||||
maxRetries := 10
|
||||
timeout := 10 * time.Second
|
||||
client := zurghttp.NewHTTPClient(accessToken, maxRetries, timeout)
|
||||
log.Debugf("Created an HTTP client with %d max retries and %s timeout", maxRetries, timeout)
|
||||
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("Authorization", "Bearer "+accessToken)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
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
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from unrestrict check endpoint", resp.Status)
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func UnrestrictLink(accessToken, 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/link", bytes.NewBufferString(data.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
var response UnrestrictResponse
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !canFetchFirstByte(response.Download) {
|
||||
return nil, fmt.Errorf("can't fetch first byte")
|
||||
}
|
||||
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 GetTorrents(accessToken string, customLimit int) ([]Torrent, int, error) {
|
||||
func (rd *RealDebrid) GetTorrents(customLimit int) ([]Torrent, int, error) {
|
||||
baseURL := "https://api.real-debrid.com/rest/1.0/torrents"
|
||||
var allTorrents []Torrent
|
||||
page := 1
|
||||
@@ -110,19 +93,20 @@ func GetTorrents(accessToken string, customLimit int) ([]Torrent, int, error) {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
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 resp.StatusCode != http.StatusOK {
|
||||
// if status code is not 2xx, return error
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from get torrents endpoint", resp.Status)
|
||||
return nil, 0, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
@@ -130,6 +114,7 @@ func GetTorrents(accessToken string, customLimit int) ([]Torrent, int, error) {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -147,126 +132,106 @@ func GetTorrents(accessToken string, customLimit int) ([]Torrent, int, error) {
|
||||
|
||||
page++
|
||||
}
|
||||
|
||||
return allTorrents, totalCount, nil
|
||||
}
|
||||
|
||||
func GetTorrentInfo(accessToken, id string) (*Torrent, error) {
|
||||
func (rd *RealDebrid) GetTorrentInfo(id string) (*Torrent, 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
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
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
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from get info endpoint", resp.Status)
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
var response Torrent
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
rd.log.Errorf("Error when : %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rd.log.Debugf("Fetched info for torrent id=%s", response.ID)
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
// SelectTorrentFiles selects files of a torrent to start it.
|
||||
func SelectTorrentFiles(accessToken string, id string, files string) error {
|
||||
// Prepare request data
|
||||
func (rd *RealDebrid) SelectTorrentFiles(id string, files string) error {
|
||||
data := url.Values{}
|
||||
data.Set("files", files)
|
||||
|
||||
// Construct request URL
|
||||
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
|
||||
}
|
||||
|
||||
// Set request headers
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
// Send the request
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
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()
|
||||
|
||||
// Handle response status codes
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK, http.StatusNoContent:
|
||||
return nil // Success
|
||||
case http.StatusAccepted:
|
||||
return errors.New("action already done")
|
||||
case http.StatusBadRequest:
|
||||
return errors.New("bad request")
|
||||
case http.StatusUnauthorized:
|
||||
return errors.New("bad token (expired or invalid)")
|
||||
case http.StatusForbidden:
|
||||
return errors.New("permission denied (account locked or not premium)")
|
||||
case http.StatusNotFound:
|
||||
return errors.New("wrong parameter (invalid file id(s)) or unknown resource (invalid id)")
|
||||
default:
|
||||
return fmt.Errorf("unexpected HTTP error: %s", resp.Status)
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from select files endpoint", resp.Status)
|
||||
return fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
rd.log.Debugf("Selected files %s for torrent id=%s", len(strings.Split(files, ",")), id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteTorrent deletes a torrent from the torrents list.
|
||||
func DeleteTorrent(accessToken string, id string) error {
|
||||
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
|
||||
}
|
||||
|
||||
// Set request headers
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
|
||||
// Send the request
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
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()
|
||||
|
||||
// Handle response status codes
|
||||
switch resp.StatusCode {
|
||||
case http.StatusNoContent:
|
||||
return nil // Success
|
||||
case http.StatusUnauthorized:
|
||||
return errors.New("bad token (expired or invalid)")
|
||||
case http.StatusForbidden:
|
||||
return errors.New("permission denied (account locked)")
|
||||
case http.StatusNotFound:
|
||||
return errors.New("unknown resource")
|
||||
default:
|
||||
return fmt.Errorf("unexpected HTTP error: %s", resp.Status)
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from delete torrent endpoint", resp.Status)
|
||||
return fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
rd.log.Debugf("Deleted torrent with id=%s", id)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddMagnetHash adds a magnet link to download.
|
||||
func AddMagnetHash(accessToken, magnet string) (*MagnetResponse, error) {
|
||||
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))
|
||||
@@ -275,77 +240,110 @@ func AddMagnetHash(accessToken, magnet string) (*MagnetResponse, error) {
|
||||
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
|
||||
}
|
||||
|
||||
// Set request headers
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
// Send the request
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
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()
|
||||
|
||||
// Handle response status codes
|
||||
switch resp.StatusCode {
|
||||
case http.StatusCreated:
|
||||
var response MagnetResponse
|
||||
err := json.NewDecoder(resp.Body).Decode(&response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &response, nil
|
||||
case http.StatusBadRequest:
|
||||
return nil, errors.New("bad request")
|
||||
case http.StatusUnauthorized:
|
||||
return nil, errors.New("bad token (expired or invalid)")
|
||||
case http.StatusForbidden:
|
||||
return nil, errors.New("permission denied (account locked or not premium)")
|
||||
case http.StatusServiceUnavailable:
|
||||
return nil, errors.New("service unavailable")
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected HTTP error: %s", resp.Status)
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from add magnet endpoint", resp.Status)
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
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 GetActiveTorrentCount(accessToken string) (*ActiveTorrentCountResponse, error) {
|
||||
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
|
||||
}
|
||||
|
||||
// Set request headers
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
|
||||
// Send the request
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
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()
|
||||
|
||||
// Handle response status codes
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
var response ActiveTorrentCountResponse
|
||||
err := json.NewDecoder(resp.Body).Decode(&response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &response, nil
|
||||
case http.StatusUnauthorized:
|
||||
return nil, errors.New("bad token (expired or invalid)")
|
||||
case http.StatusForbidden:
|
||||
return nil, errors.New("permission denied (account locked)")
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected HTTP error: %s", resp.Status)
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from active torrents endpoint", resp.Status)
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
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) (*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")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
rd.log.Errorf("Error when executing the unrestrict link 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 link response: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
rd.log.Errorf("Received a %s from unrestrict link endpoint", resp.Status)
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
var response UnrestrictResponse
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
rd.log.Errorf("Error when decoding unrestrict link JSON: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user