Fix detection issues

This commit is contained in:
Ben Sarmiento
2024-02-18 04:15:41 +01:00
parent 02d900971d
commit 36298dd979
6 changed files with 63 additions and 67 deletions

View File

@@ -64,7 +64,7 @@ func MainApp(configPath string) {
downloadClient := http.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), config.GetDownloadTimeoutSecs(), true, config, log.Named("download_client")) downloadClient := http.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), config.GetDownloadTimeoutSecs(), true, config, log.Named("download_client"))
api := realdebrid.NewRealDebrid(apiClient, unrestrictClient, downloadClient, log.Named("realdebrid")) api := realdebrid.NewRealDebrid(apiClient, unrestrictClient, downloadClient, config, log.Named("realdebrid"))
premium.MonitorPremiumStatus(api, zurglog) premium.MonitorPremiumStatus(api, zurglog)

View File

@@ -30,6 +30,7 @@ type ConfigInterface interface {
ShouldDeleteRarFiles() bool ShouldDeleteRarFiles() bool
GetDownloadsEveryMins() int GetDownloadsEveryMins() int
GetPlayableExtensions() []string GetPlayableExtensions() []string
GetTorrentsCount() int
} }
type ZurgConfig struct { type ZurgConfig struct {
@@ -60,6 +61,7 @@ type ZurgConfig struct {
RetriesUntilFailed int `yaml:"retries_until_failed" json:"retries_until_failed"` RetriesUntilFailed int `yaml:"retries_until_failed" json:"retries_until_failed"`
ServeFromRclone bool `yaml:"serve_from_rclone" json:"serve_from_rclone"` ServeFromRclone bool `yaml:"serve_from_rclone" json:"serve_from_rclone"`
Username string `yaml:"username" json:"username"` Username string `yaml:"username" json:"username"`
TorrentsCount int `yaml:"get_torrents_count" json:"get_torrents_count"`
} }
func (z *ZurgConfig) GetConfig() ZurgConfig { func (z *ZurgConfig) GetConfig() ZurgConfig {
@@ -201,3 +203,10 @@ func (z *ZurgConfig) GetPlayableExtensions() []string {
} }
return z.PlayableExtensions return z.PlayableExtensions
} }
func (z *ZurgConfig) GetTorrentsCount() int {
if z.TorrentsCount == 0 {
return 100
}
return z.TorrentsCount
}

View File

@@ -40,7 +40,6 @@ func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) {
}) })
} }
} }
t.allAccessKeys.Remove(accessKey)
t.log.Infof("Removing torrent %s from zurg database (not real-debrid)", accessKey) t.log.Infof("Removing torrent %s from zurg database (not real-debrid)", accessKey)
t.DirectoryMap.IterCb(func(directory string, torrents cmap.ConcurrentMap[string, *Torrent]) { t.DirectoryMap.IterCb(func(directory string, torrents cmap.ConcurrentMap[string, *Torrent]) {
torrents.Remove(accessKey) torrents.Remove(accessKey)

View File

@@ -29,6 +29,7 @@ type TorrentManager struct {
DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download] DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download]
fixers cmap.ConcurrentMap[string, string] // trigger -> [command, id] fixers cmap.ConcurrentMap[string, string] // trigger -> [command, id]
allAccessKeys mapset.Set[string] allAccessKeys mapset.Set[string]
allIDs mapset.Set[string]
latestState *LibraryState latestState *LibraryState
requiredVersion string requiredVersion string
workerPool *ants.Pool workerPool *ants.Pool
@@ -55,6 +56,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
RepairKillSwitch: make(chan struct{}, 1), RepairKillSwitch: make(chan struct{}, 1),
RemountTrigger: make(chan struct{}, 1), RemountTrigger: make(chan struct{}, 1),
allAccessKeys: mapset.NewSet[string](), allAccessKeys: mapset.NewSet[string](),
allIDs: mapset.NewSet[string](),
latestState: &LibraryState{}, latestState: &LibraryState{},
requiredVersion: "0.9.3-hotfix.10", requiredVersion: "0.9.3-hotfix.10",
workerPool: workerPool, workerPool: workerPool,

View File

@@ -36,60 +36,60 @@ func (t *TorrentManager) refreshTorrents(isInitialRun bool) []string {
close(infoChan) close(infoChan)
t.log.Infof("Fetched info for %d torrents", len(instances)) t.log.Infof("Fetched info for %d torrents", len(instances))
newlyFetchedKeys := mapset.NewSet[string]() var updatedPaths []string
noInfoCount := 0 noInfoCount := 0
allTorrents, _ := t.DirectoryMap.Get(INT_ALL) allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
freshAccessKeys := mapset.NewSet[string]()
deletedIDs := t.allIDs.Clone()
for info := range infoChan { for info := range infoChan {
if info == nil { if info == nil {
noInfoCount++ noInfoCount++
continue continue
} }
accessKey := t.GetKey(info)
if !info.AnyInProgress() { // since it's single instance info, Any == All
newlyFetchedKeys.Add(accessKey)
}
torrent, exists := allTorrents.Get(accessKey) infoID, _ := info.DownloadedIDs.Clone().Pop()
deletedIDs.Remove(infoID)
accessKey := t.GetKey(info)
freshAccessKeys.Add(accessKey)
// update allTorrents
mainTorrent, exists := allTorrents.Get(accessKey)
if !exists { if !exists {
allTorrents.Set(accessKey, info) allTorrents.Set(accessKey, info)
continue } else {
if !mainTorrent.DownloadedIDs.Contains(infoID) {
merged := t.mergeToMain(mainTorrent, info)
allTorrents.Set(accessKey, merged)
}
} }
newIDs := info.DownloadedIDs.Union(info.InProgressIDs)
oldIDs := torrent.DownloadedIDs.Union(torrent.InProgressIDs) // check for newly finished torrents for assigning to directories
if !newIDs.Difference(oldIDs).IsEmpty() { isDone := info.DownloadedIDs.Cardinality() > 0 && info.InProgressIDs.IsEmpty()
mainTorrent := t.mergeToMain(torrent, info) if isDone && !t.allIDs.Contains(infoID) {
allTorrents.Set(accessKey, &mainTorrent) var directories []string
mainTor, _ := allTorrents.Get(accessKey)
t.assignedDirectoryCb(mainTor, func(directory string) {
listing, _ := t.DirectoryMap.Get(directory)
listing.Set(accessKey, mainTor)
updatedPaths = append(updatedPaths, fmt.Sprintf("%s/%s", directory, accessKey))
// this is just for the logs
if directory != config.ALL_TORRENTS {
directories = append(directories, directory)
}
})
t.allIDs.Add(infoID)
} }
} }
t.allIDs.RemoveAll(deletedIDs.ToSlice()...)
t.log.Infof("Compiled into %d torrents, %d were missing info", allTorrents.Count(), noInfoCount) t.log.Infof("Compiled into %d torrents, %d were missing info", allTorrents.Count(), noInfoCount)
var updatedPaths []string
// torrents yet to be assigned in a directory
newlyFetchedKeys.Difference(t.allAccessKeys).Each(func(accessKey string) bool {
// assign to directories
tor, ok := allTorrents.Get(accessKey)
if !ok {
return false
}
var directories []string
t.assignedDirectoryCb(tor, func(directory string) {
torrents, _ := t.DirectoryMap.Get(directory)
torrents.Set(accessKey, tor)
updatedPaths = append(updatedPaths, fmt.Sprintf("%s/%s", directory, accessKey))
// this is just for the logs
if directory != config.ALL_TORRENTS {
directories = append(directories, directory)
}
})
// t.log.Debugf("Added %s to %v", accessKey, directories)
t.allAccessKeys.Add(accessKey)
return false
})
// removed torrents // removed torrents
t.allAccessKeys.Difference(newlyFetchedKeys).Each(func(accessKey string) bool { t.allAccessKeys.Difference(freshAccessKeys).Each(func(accessKey string) bool {
t.Delete(accessKey, false) t.Delete(accessKey, false)
t.allAccessKeys.Remove(accessKey)
return false return false
}) })
t.allAccessKeys.Append(freshAccessKeys.ToSlice()...)
if t.Config.EnableRepair() { if t.Config.EnableRepair() {
if isInitialRun { if isInitialRun {
@@ -138,33 +138,17 @@ func (t *TorrentManager) StartRefreshJob() {
func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
if torrentFromCache, exists := infoCache.Get(rdTorrent.ID); exists && if cachedTor, exists := infoCache.Get(rdTorrent.ID); exists &&
!torrentFromCache.AnyInProgress() && cachedTor.SelectedFiles.Count() == len(rdTorrent.Links) {
torrentFromCache.SelectedFiles.Count() == len(rdTorrent.Links) {
return torrentFromCache return cachedTor
} else if !exists { } else if diskTor := t.readTorrentFromFile(rdTorrent.ID); diskTor != nil &&
diskTor.SelectedFiles.Count() == len(rdTorrent.Links) {
torrentFromFile := t.readTorrentFromFile(rdTorrent.ID) infoCache.Set(rdTorrent.ID, diskTor)
t.ResetSelectedFiles(diskTor)
if torrentFromFile != nil && return diskTor
torrentFromFile.SelectedFiles.Count() == len(rdTorrent.Links) {
hasBrokenFiles := false
torrentFromFile.SelectedFiles.IterCb(func(filepath string, file *File) {
if file.IsBroken && !file.IsDeleted {
hasBrokenFiles = true
}
})
if !hasBrokenFiles {
infoCache.Set(rdTorrent.ID, torrentFromFile)
t.ResetSelectedFiles(torrentFromFile)
return torrentFromFile
}
}
} }
info, err := t.Api.GetTorrentInfo(rdTorrent.ID) info, err := t.Api.GetTorrentInfo(rdTorrent.ID)
@@ -184,7 +168,6 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
// if it is empty, it means the file is no longer available // if it is empty, it means the file is no longer available
// Files+Links together are the same as SelectedFiles // Files+Links together are the same as SelectedFiles
var selectedFiles []*File var selectedFiles []*File
// if some Links are empty, we need to repair it
for _, file := range info.Files { for _, file := range info.Files {
if file.Selected == 0 { if file.Selected == 0 {
continue continue
@@ -192,7 +175,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
selectedFiles = append(selectedFiles, &File{ selectedFiles = append(selectedFiles, &File{
File: file, File: file,
Ended: info.Ended, Ended: info.Ended,
Link: "", // no link yet Link: "", // no link yet, consider it broken
IsBroken: true, IsBroken: true,
}) })
} }
@@ -253,7 +236,7 @@ func (t *TorrentManager) ResetSelectedFiles(torrent *Torrent) {
torrent.SelectedFiles = newSelectedFiles torrent.SelectedFiles = newSelectedFiles
} }
func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent { func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) *Torrent {
var newer, older *Torrent var newer, older *Torrent
if existing.Added < toMerge.Added { if existing.Added < toMerge.Added {
newer = toMerge newer = toMerge
@@ -322,7 +305,7 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent {
t.CheckDeletedStatus(&mainTorrent) t.CheckDeletedStatus(&mainTorrent)
} }
return mainTorrent return &mainTorrent
} }
func (t *TorrentManager) assignedDirectoryCb(tor *Torrent, cb func(string)) { func (t *TorrentManager) assignedDirectoryCb(tor *Torrent, cb func(string)) {

View File

@@ -8,6 +8,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/debridmediamanager/zurg/internal/config"
zurghttp "github.com/debridmediamanager/zurg/pkg/http" zurghttp "github.com/debridmediamanager/zurg/pkg/http"
"github.com/debridmediamanager/zurg/pkg/logutil" "github.com/debridmediamanager/zurg/pkg/logutil"
) )
@@ -16,14 +17,16 @@ type RealDebrid struct {
client *zurghttp.HTTPClient client *zurghttp.HTTPClient
unrestrictClient *zurghttp.HTTPClient unrestrictClient *zurghttp.HTTPClient
downloadClient *zurghttp.HTTPClient downloadClient *zurghttp.HTTPClient
cfg config.ConfigInterface
log *logutil.Logger log *logutil.Logger
} }
func NewRealDebrid(client, unrestrictClient, downloadClient *zurghttp.HTTPClient, log *logutil.Logger) *RealDebrid { func NewRealDebrid(client, unrestrictClient, downloadClient *zurghttp.HTTPClient, cfg config.ConfigInterface, log *logutil.Logger) *RealDebrid {
return &RealDebrid{ return &RealDebrid{
client: client, client: client,
unrestrictClient: unrestrictClient, unrestrictClient: unrestrictClient,
downloadClient: downloadClient, downloadClient: downloadClient,
cfg: cfg,
log: log, log: log,
} }
} }
@@ -119,7 +122,7 @@ func (rd *RealDebrid) GetTorrents(customLimit int, active bool) ([]Torrent, int,
page := 1 page := 1
limit := customLimit limit := customLimit
if limit == 0 { if limit == 0 {
limit = 100 limit = rd.cfg.GetTorrentsCount()
} }
totalCount := 0 totalCount := 0