Add more metadata
This commit is contained in:
@@ -8,15 +8,16 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func UnrestrictCheck(accessToken, link string) (UnrestrictResponse, error) {
|
||||
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
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
@@ -25,35 +26,35 @@ func UnrestrictCheck(accessToken, link string) (UnrestrictResponse, error) {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return UnrestrictResponse{}, err
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return UnrestrictResponse{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return UnrestrictResponse{}, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
var response UnrestrictResponse
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
return UnrestrictResponse{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return response, nil
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func UnrestrictLink(accessToken, link string) (UnrestrictResponse, error) {
|
||||
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
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
@@ -62,33 +63,33 @@ func UnrestrictLink(accessToken, link string) (UnrestrictResponse, error) {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return UnrestrictResponse{}, err
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return UnrestrictResponse{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return UnrestrictResponse{}, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
|
||||
}
|
||||
|
||||
var response UnrestrictResponse
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
return UnrestrictResponse{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return response, nil
|
||||
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 := 2500
|
||||
limit := 100
|
||||
|
||||
for {
|
||||
params := url.Values{}
|
||||
@@ -124,7 +125,7 @@ func GetTorrents(accessToken string) ([]Torrent, error) {
|
||||
|
||||
allTorrents = append(allTorrents, torrents...)
|
||||
|
||||
totalCountHeader := resp.Header.Get("x-total-count")
|
||||
totalCountHeader := "100" // resp.Header.Get("x-total-count")
|
||||
totalCount, err := strconv.Atoi(totalCountHeader)
|
||||
if err != nil {
|
||||
break
|
||||
@@ -140,25 +141,67 @@ func GetTorrents(accessToken string) ([]Torrent, error) {
|
||||
return deduplicateTorrents(allTorrents), nil
|
||||
}
|
||||
|
||||
func GetTorrentInfo(accessToken, 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 {
|
||||
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()
|
||||
|
||||
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 Torrent
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func deduplicateTorrents(torrents []Torrent) []Torrent {
|
||||
mappedTorrents := make(map[string]Torrent)
|
||||
|
||||
for _, t := range torrents {
|
||||
if existing, ok := mappedTorrents[t.Filename]; ok {
|
||||
torrentName := t.Filename
|
||||
if existing, ok := mappedTorrents[torrentName]; 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
|
||||
existing.ID += "," + t.ID
|
||||
// existing.Links = append(existing.Links, t.Links...)
|
||||
for _, link := range t.Links {
|
||||
existing.Links = appendIfNotExists(existing.Links, link)
|
||||
}
|
||||
existing.Bytes += t.Bytes
|
||||
existing.Added = moreRecent(existing.Added, t.Added)
|
||||
mappedTorrents[torrentName] = existing
|
||||
} else {
|
||||
// If hash is different, delete old entry and create two new entries
|
||||
delete(mappedTorrents, t.Filename)
|
||||
newKey1 := fmt.Sprintf("%s - %s", t.Filename, t.Hash[:4])
|
||||
delete(mappedTorrents, torrentName)
|
||||
newKey1 := fmt.Sprintf("%s - %s", torrentName, t.Hash[:4])
|
||||
mappedTorrents[newKey1] = t
|
||||
newKey2 := fmt.Sprintf("%s - %s", existing.Filename, existing.Hash[:4])
|
||||
mappedTorrents[newKey2] = existing
|
||||
}
|
||||
} else {
|
||||
mappedTorrents[t.Filename] = t
|
||||
mappedTorrents[torrentName] = t
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,3 +213,31 @@ func deduplicateTorrents(torrents []Torrent) []Torrent {
|
||||
|
||||
return deduplicated
|
||||
}
|
||||
|
||||
func contains(slice []string, str string) bool {
|
||||
for _, v := range slice {
|
||||
if v == str {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func appendIfNotExists(slice []string, str string) []string {
|
||||
if !contains(slice, str) {
|
||||
slice = append(slice, str)
|
||||
}
|
||||
return slice
|
||||
}
|
||||
|
||||
func moreRecent(time1, time2 string) string {
|
||||
tTime1, err1 := time.Parse(time.RFC3339, time1)
|
||||
tTime2, err2 := time.Parse(time.RFC3339, time2)
|
||||
if err1 != nil || err2 != nil {
|
||||
return time1
|
||||
}
|
||||
if tTime2.After(tTime1) {
|
||||
time1 = time2
|
||||
}
|
||||
return time1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user