Accommodate for empty RD accounts

This commit is contained in:
Ben Sarmiento
2024-01-08 13:01:21 +01:00
parent b1116dfcca
commit 122eea2374
4 changed files with 37 additions and 31 deletions

View File

@@ -1,38 +1,33 @@
package torrent package torrent
import ( import (
"time"
"github.com/debridmediamanager/zurg/pkg/realdebrid" "github.com/debridmediamanager/zurg/pkg/realdebrid"
) )
type LibraryState struct { type LibraryState struct {
TotalCount int TotalCount int
ActiveCount int ActiveCount int
FirstTorrent realdebrid.Torrent FirstTorrent *realdebrid.Torrent
FirstActiveTorrent realdebrid.Torrent FirstActiveTorrent *realdebrid.Torrent
} }
func (ls LibraryState) equal(a LibraryState) bool { func (ls LibraryState) equal(a LibraryState) bool {
return a.TotalCount == ls.TotalCount && if a.TotalCount != ls.TotalCount || a.ActiveCount != ls.ActiveCount {
a.ActiveCount == ls.ActiveCount && return false
a.FirstTorrent.ID == ls.FirstTorrent.ID &&
a.FirstTorrent.Status == ls.FirstTorrent.Status &&
a.FirstActiveTorrent.ID == ls.FirstActiveTorrent.ID &&
a.FirstActiveTorrent.Status == ls.FirstActiveTorrent.Status
}
func EmptyState() LibraryState {
empty := realdebrid.Torrent{
ID: "",
Added: (time.Time{}).Format(time.RFC3339),
} }
return LibraryState{ if (ls.FirstTorrent == nil) != (a.FirstTorrent == nil) {
TotalCount: 0, return false
ActiveCount: 0,
FirstTorrent: empty,
FirstActiveTorrent: empty,
} }
if ls.FirstTorrent != nil && (ls.FirstTorrent.ID != a.FirstTorrent.ID || ls.FirstTorrent.Status != a.FirstTorrent.Status) {
return false
}
if (ls.FirstActiveTorrent == nil) != (a.FirstActiveTorrent == nil) {
return false
}
if ls.FirstActiveTorrent != nil && (ls.FirstActiveTorrent.ID != a.FirstActiveTorrent.ID || ls.FirstActiveTorrent.Status != a.FirstActiveTorrent.Status) {
return false
}
return true
} }
func (t *TorrentManager) SetNewLatestState(checksum LibraryState) { func (t *TorrentManager) SetNewLatestState(checksum LibraryState) {
@@ -52,7 +47,7 @@ func (t *TorrentManager) getCurrentState() LibraryState {
torrentChan := make(chan torrentsResp, 1) torrentChan := make(chan torrentsResp, 1)
activeChan := make(chan torrentsResp, 1) activeChan := make(chan torrentsResp, 1)
countChan := make(chan int, 1) countChan := make(chan int, 1)
errChan := make(chan error, 3) // accommodate errors from both goroutines errChan := make(chan error, 3) // accommodate errors from all goroutines
defer close(torrentChan) defer close(torrentChan)
defer close(activeChan) defer close(activeChan)
defer close(countChan) defer close(countChan)
@@ -85,22 +80,24 @@ func (t *TorrentManager) getCurrentState() LibraryState {
countChan <- count.DownloadingCount countChan <- count.DownloadingCount
}) })
// Existing goroutines for GetTorrents and GetActiveTorrentCount var first, active *realdebrid.Torrent
var first realdebrid.Torrent
var active realdebrid.Torrent
var totalCount, count int var totalCount, count int
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
select { select {
case firstResp := <-torrentChan: case firstResp := <-torrentChan:
first = firstResp.torrents[0] if len(firstResp.torrents) > 0 {
first = &firstResp.torrents[0]
}
totalCount = firstResp.totalCount totalCount = firstResp.totalCount
case activeResp := <-activeChan: case activeResp := <-activeChan:
active = activeResp.torrents[0] if len(activeResp.torrents) > 0 {
active = &activeResp.torrents[0]
}
case count = <-countChan: case count = <-countChan:
case err := <-errChan: case err := <-errChan:
t.log.Warnf("Checksum API Error: %v", err) t.log.Warnf("Checksum API Error: %v", err)
return EmptyState() return LibraryState{}
} }
} }

View File

@@ -38,13 +38,11 @@ type TorrentManager struct {
// it will fetch all torrents and their info in the background // it will fetch all torrents and their info in the background
// and store them in-memory and cached in files // and store them in-memory and cached in files
func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p *ants.Pool, log *logutil.Logger) *TorrentManager { func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p *ants.Pool, log *logutil.Logger) *TorrentManager {
initialSate := EmptyState()
t := &TorrentManager{ t := &TorrentManager{
Config: cfg, Config: cfg,
Api: api, Api: api,
allAccessKeys: mapset.NewSet[string](), allAccessKeys: mapset.NewSet[string](),
latestState: &initialSate, latestState: &LibraryState{},
requiredVersion: "07.01.2024", requiredVersion: "07.01.2024",
workerPool: p, workerPool: p,
log: log, log: log,

View File

@@ -49,6 +49,9 @@ func (t *TorrentManager) repairAll() {
var availabilityChecks = make(map[string]bool) var availabilityChecks = make(map[string]bool)
uncachedCount := 0 uncachedCount := 0
for i := range hashGroups { for i := range hashGroups {
if hashGroups[i].Cardinality() == 0 {
break
}
resp, err := t.Api.AvailabilityCheck(hashGroups[i].ToSlice()) resp, err := t.Api.AvailabilityCheck(hashGroups[i].ToSlice())
if err != nil { if err != nil {
t.log.Warnf("Cannot check availability: %v", err) t.log.Warnf("Cannot check availability: %v", err)

View File

@@ -95,6 +95,10 @@ func (rd *RealDebrid) GetTorrents(customLimit int, active bool) ([]Torrent, int,
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode == http.StatusNoContent {
return allTorrents, 0, nil
}
// if status code is not 2xx, return erro // if status code is not 2xx, return erro
var torrents []Torrent var torrents []Torrent
@@ -340,6 +344,10 @@ func (rd *RealDebrid) GetDownloads(page, offset int) ([]Download, int, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode == http.StatusNoContent {
return allDownloads, 0, nil
}
// if status code is not 2xx, return erro // if status code is not 2xx, return erro
var downloads []Download var downloads []Download