Add support for configs

This commit is contained in:
Ben Sarmiento
2023-10-18 21:09:25 +02:00
parent f9b5b1efac
commit 4650213218
19 changed files with 359 additions and 48 deletions

View File

@@ -7,6 +7,7 @@ import (
"strings"
"sync"
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
)
@@ -14,15 +15,17 @@ type TorrentManager struct {
token string
torrents []Torrent
workerPool chan bool
config config.ConfigInterface
}
// NewTorrentManager creates a new torrent manager
// it will fetch all torrents and their info in the background
// and store them in-memory
func NewTorrentManager(token string) *TorrentManager {
func NewTorrentManager(token string, config config.ConfigInterface) *TorrentManager {
handler := &TorrentManager{
token: token,
workerPool: make(chan bool, 10),
config: config,
}
handler.torrents = handler.getAll()
@@ -40,18 +43,36 @@ func NewTorrentManager(token string) *TorrentManager {
}
func (t *TorrentManager) getAll() []Torrent {
log.Println("Getting all torrents")
torrents, err := realdebrid.GetTorrents(t.token)
if err != nil {
log.Printf("Cannot get torrents: %v\n", err.Error())
return nil
}
var torrentsV2 []Torrent
for _, torrent := range torrents {
torrentsV2 = append(torrentsV2, Torrent{
Torrent: torrent,
SelectedFiles: nil,
})
version := t.config.GetVersion()
if version == "v1" {
configV1 := t.config.(*config.ZurgConfigV1)
groupMap := configV1.GetGroupMap()
for _, directories := range groupMap {
for _, torrent := range torrents {
// process grouping
torrentV2 := Torrent{
Torrent: torrent,
SelectedFiles: nil,
}
for _, directory := range directories {
if configV1.MeetsConditions(directory, torrent.ID, torrent.Name) {
torrentV2.Directories = append(torrentV2.Directories, directory)
break
}
}
torrentsV2 = append(torrentsV2, torrentV2)
}
}
}
log.Printf("Fetched %d torrents", len(torrentsV2))
return torrentsV2
}
@@ -59,6 +80,18 @@ func (t *TorrentManager) GetAll() []Torrent {
return t.torrents
}
func (t *TorrentManager) GetByDirectory(directory string) []Torrent {
var torrents []Torrent
for _, torrent := range t.torrents {
for _, dir := range torrent.Directories {
if dir == directory {
torrents = append(torrents, torrent)
}
}
}
return torrents
}
func (t *TorrentManager) getInfo(torrentID string) *Torrent {
torrentFromFile := t.readFromFile(torrentID)
if torrentFromFile != nil {