A lot of rewrite here

This commit is contained in:
Ben Sarmiento
2023-10-18 00:17:07 +02:00
parent 44216343e2
commit 0886c93250
14 changed files with 399 additions and 482 deletions

View File

@@ -8,7 +8,6 @@ import (
"net/http"
"net/url"
"strconv"
"time"
)
func UnrestrictCheck(accessToken, link string) (*UnrestrictResponse, error) {
@@ -89,7 +88,7 @@ func GetTorrents(accessToken string) ([]Torrent, error) {
baseURL := "https://api.real-debrid.com/rest/1.0/torrents"
var allTorrents []Torrent
page := 1
limit := 100
limit := 2500
for {
params := url.Values{}
@@ -125,7 +124,7 @@ func GetTorrents(accessToken string) ([]Torrent, error) {
allTorrents = append(allTorrents, torrents...)
totalCountHeader := "100" // resp.Header.Get("x-total-count")
totalCountHeader := resp.Header.Get("x-total-count")
totalCount, err := strconv.Atoi(totalCountHeader)
if err != nil {
break
@@ -138,7 +137,7 @@ func GetTorrents(accessToken string) ([]Torrent, error) {
page++
}
return deduplicateTorrents(allTorrents), nil
return allTorrents, nil
}
func GetTorrentInfo(accessToken, id string) (*Torrent, error) {
@@ -175,69 +174,3 @@ func GetTorrentInfo(accessToken, id string) (*Torrent, error) {
return &response, nil
}
func deduplicateTorrents(torrents []Torrent) []Torrent {
mappedTorrents := make(map[string]Torrent)
for _, t := range torrents {
torrentName := t.Filename
if existing, ok := mappedTorrents[torrentName]; ok {
if existing.Hash == t.Hash {
// If hash is the same, combine the links
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, 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[torrentName] = t
}
}
// Convert the map back to a slice
deduplicated := make([]Torrent, 0, len(mappedTorrents))
for _, value := range mappedTorrents {
deduplicated = append(deduplicated, value)
}
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
}

View File

@@ -27,6 +27,6 @@ type Torrent struct {
type File struct {
ID int `json:"id"`
Path string `json:"path"`
Bytes int `json:"bytes"`
Bytes int64 `json:"bytes"`
Selected int `json:"selected"`
}