173 lines
4.0 KiB
Go
173 lines
4.0 KiB
Go
package realdebrid
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
)
|
|
|
|
func UnrestrictCheck(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/check", bytes.NewBufferString(data.Encode()))
|
|
if err != nil {
|
|
return UnrestrictResponse{}, 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 UnrestrictResponse{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return UnrestrictResponse{}, err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return UnrestrictResponse{}, fmt.Errorf("HTTP error: %s", resp.Status)
|
|
}
|
|
|
|
var response UnrestrictResponse
|
|
err = json.Unmarshal(body, &response)
|
|
if err != nil {
|
|
return UnrestrictResponse{}, 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 UnrestrictResponse{}, 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 UnrestrictResponse{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return UnrestrictResponse{}, err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return UnrestrictResponse{}, fmt.Errorf("HTTP error: %s", resp.Status)
|
|
}
|
|
|
|
var response UnrestrictResponse
|
|
err = json.Unmarshal(body, &response)
|
|
if err != nil {
|
|
return UnrestrictResponse{}, err
|
|
}
|
|
|
|
return response, nil
|
|
}
|
|
|
|
func GetTorrents(accessToken string) ([]Torrent, error) {
|
|
baseURL := "https://api.real-debrid.com/rest/1.0/torrents"
|
|
var allTorrents []Torrent
|
|
page := 1
|
|
limit := 10
|
|
|
|
for {
|
|
params := url.Values{}
|
|
params.Set("page", fmt.Sprintf("%d", page))
|
|
params.Set("limit", fmt.Sprintf("%d", limit))
|
|
|
|
reqURL := baseURL + "?" + params.Encode()
|
|
|
|
req, err := http.NewRequest("GET", reqURL, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+accessToken)
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
|
}
|
|
|
|
var torrents []Torrent
|
|
decoder := json.NewDecoder(resp.Body)
|
|
err = decoder.Decode(&torrents)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
allTorrents = append(allTorrents, torrents...)
|
|
|
|
totalCountHeader := "10" // resp.Header.Get("x-total-count")
|
|
totalCount, err := strconv.Atoi(totalCountHeader)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
if len(torrents) < limit || len(allTorrents) >= totalCount {
|
|
break
|
|
}
|
|
|
|
page++
|
|
}
|
|
|
|
return deduplicateTorrents(allTorrents), nil
|
|
}
|
|
|
|
func deduplicateTorrents(torrents []Torrent) []Torrent {
|
|
mappedTorrents := make(map[string]Torrent)
|
|
|
|
for _, t := range torrents {
|
|
if existing, ok := mappedTorrents[t.Filename]; ok {
|
|
if existing.Hash == t.Hash {
|
|
// If hash is the same, combine the links
|
|
existing.Links = append(existing.Links, t.Links...)
|
|
mappedTorrents[t.Filename] = existing
|
|
} else {
|
|
// If hash is different, delete old entry and create two new entries
|
|
delete(mappedTorrents, t.Filename)
|
|
newKey1 := t.Filename + " - " + t.Hash[:4]
|
|
newKey2 := existing.Filename + " - " + existing.Hash[:4]
|
|
mappedTorrents[newKey1] = t
|
|
mappedTorrents[newKey2] = existing
|
|
}
|
|
} else {
|
|
mappedTorrents[t.Filename] = t
|
|
}
|
|
}
|
|
|
|
// Convert the map back to a slice
|
|
deduplicated := make([]Torrent, 0, len(mappedTorrents))
|
|
for _, value := range mappedTorrents {
|
|
deduplicated = append(deduplicated, value)
|
|
}
|
|
|
|
return deduplicated
|
|
}
|