Prepare materials for auto heal functionality
This commit is contained in:
@@ -3,6 +3,7 @@ package realdebrid
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -87,39 +88,6 @@ func UnrestrictLink(accessToken, link string) (*UnrestrictResponse, error) {
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func canFetchFirstByte(url string) bool {
|
||||
// Create a new HTTP request
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// Set the Range header to request only the first byte
|
||||
req.Header.Set("Range", "bytes=0-0")
|
||||
|
||||
// Execute the request
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// If server supports partial content
|
||||
if resp.StatusCode == http.StatusPartialContent {
|
||||
buffer := make([]byte, 1)
|
||||
_, err := resp.Body.Read(buffer)
|
||||
return err == nil
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return false
|
||||
}
|
||||
// If server doesn't support partial content, try reading the first byte and immediately close
|
||||
buffer := make([]byte, 1)
|
||||
_, err = resp.Body.Read(buffer)
|
||||
resp.Body.Close() // Close immediately after reading
|
||||
return err == 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) {
|
||||
@@ -216,3 +184,168 @@ func GetTorrentInfo(accessToken, id string) (*Torrent, error) {
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
// SelectTorrentFiles selects files of a torrent to start it.
|
||||
func SelectTorrentFiles(accessToken string, id string, files string) error {
|
||||
// Prepare request data
|
||||
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 {
|
||||
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)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteTorrent deletes a torrent from the torrents list.
|
||||
func DeleteTorrent(accessToken string, 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 {
|
||||
return err
|
||||
}
|
||||
|
||||
// Set request headers
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
|
||||
// Send the request
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// AddMagnet adds a magnet link to download.
|
||||
func AddMagnet(accessToken, magnet, host string) (*MagnetResponse, error) {
|
||||
// Prepare request data
|
||||
data := url.Values{}
|
||||
data.Set("magnet", magnet)
|
||||
data.Set("host", host)
|
||||
|
||||
// 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 {
|
||||
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)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// GetActiveTorrentCount gets the number of currently active torrents and the current maximum limit.
|
||||
func GetActiveTorrentCount(accessToken string) (*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 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Set request headers
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
|
||||
// Send the request
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user