Fix logic of fetching torrents

This commit is contained in:
Ben Sarmiento
2024-05-06 10:48:01 +02:00
parent b6b59b22e6
commit ae94252156
11 changed files with 205 additions and 49 deletions

View File

@@ -9,6 +9,7 @@ import (
"time"
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/internal/fs"
"github.com/debridmediamanager/zurg/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
mapset "github.com/deckarep/golang-set/v2"
@@ -22,24 +23,31 @@ const (
)
type TorrentManager struct {
Config config.ConfigInterface
Api *realdebrid.RealDebrid
DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent
DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download]
fixers cmap.ConcurrentMap[string, string] // trigger -> [command, id]
allAccessKeys mapset.Set[string]
allIDs mapset.Set[string]
latestState *LibraryState
requiredVersion string
workerPool *ants.Pool
requiredVersion string
Config config.ConfigInterface
api *realdebrid.RealDebrid
workerPool *ants.Pool
log *logutil.Logger
DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent
DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download]
RootNode *fs.FileNode
RefreshKillSwitch chan struct{}
RepairKillSwitch chan struct{}
RemountTrigger chan struct{}
repairTrigger chan *Torrent
repairSet mapset.Set[*Torrent]
repairRunning bool
repairRunningMu sync.Mutex
log *logutil.Logger
latestState *LibraryState
allAccessKeys mapset.Set[string]
allIDs mapset.Set[string]
fixers cmap.ConcurrentMap[string, string] // trigger -> [command, id]
repairTrigger chan *Torrent
repairSet mapset.Set[*Torrent]
repairRunning bool
repairRunningMu sync.Mutex
}
// NewTorrentManager creates a new torrent manager
@@ -47,18 +55,24 @@ type TorrentManager struct {
// and store them in-memory and cached in files
func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, workerPool *ants.Pool, log *logutil.Logger) *TorrentManager {
t := &TorrentManager{
Config: cfg,
Api: api,
DirectoryMap: cmap.New[cmap.ConcurrentMap[string, *Torrent]](),
requiredVersion: "0.9.3-hotfix.10",
Config: cfg,
api: api,
workerPool: workerPool,
log: log,
DirectoryMap: cmap.New[cmap.ConcurrentMap[string, *Torrent]](),
RootNode: fs.NewFileNode("root", true),
RefreshKillSwitch: make(chan struct{}, 1),
RepairKillSwitch: make(chan struct{}, 1),
RemountTrigger: make(chan struct{}, 1),
allAccessKeys: mapset.NewSet[string](),
allIDs: mapset.NewSet[string](),
latestState: &LibraryState{},
requiredVersion: "0.9.3-hotfix.10",
workerPool: workerPool,
log: log,
latestState: &LibraryState{},
allAccessKeys: mapset.NewSet[string](),
allIDs: mapset.NewSet[string](),
}
t.fixers = t.readFixersFromFile()
@@ -78,9 +92,9 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
return t
}
// proxy
// proxy function
func (t *TorrentManager) UnrestrictLinkUntilOk(link string) *realdebrid.Download {
ret, err := t.Api.UnrestrictLink(link, t.Config.ShouldServeFromRclone())
ret, err := t.api.UnrestrictLink(link, t.Config.ShouldServeFromRclone())
if err != nil {
t.log.Warnf("Cannot unrestrict link %s: %v", link, err)
return nil
@@ -194,7 +208,7 @@ func (t *TorrentManager) mountDownloads() {
page := 1
offset := 0
for {
downloads, totalDownloads, err := t.Api.GetDownloads(page, offset)
downloads, totalDownloads, err := t.api.GetDownloads(page, offset)
if err != nil {
// if we get an error, we just stop
t.log.Warnf("Cannot get downloads on page %d: %v", page, err)
@@ -236,6 +250,7 @@ func (t *TorrentManager) initializeDirectories() {
// create directory maps
for _, directory := range t.Config.GetDirectories() {
t.DirectoryMap.Set(directory, cmap.New[*Torrent]())
// t.RootNode.AddChild(fs.NewFileNode(directory, true))
}
}