From 10980f4f3eaaddf77f2813dcd079857647ecd26a Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sun, 19 Nov 2023 13:58:36 +0100 Subject: [PATCH] Separated the eager repair work done on getting info --- internal/torrent/manager.go | 49 ++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index 6e0f44a..f7dcb85 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -359,12 +359,8 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { // if it is empty, it means the file is no longer available // Files+Links together are the same as SelectedFiles var selectedFiles []*File - streamableCount := 0 // if some Links are empty, we need to repair it for _, file := range info.Files { - if isStreamable(file.Path) { - streamableCount++ - } if file.Selected == 0 { continue } @@ -376,19 +372,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { }) } if len(selectedFiles) > len(info.Links) && info.Progress == 100 { - // chaotic file means RD will not output the desired file selection - // e.g. even if we select just a single mkv, it will output a rar - var isChaotic bool - selectedFiles, isChaotic = t.organizeChaos(info.Links, selectedFiles) - if isChaotic { - t.log.Warnf("Torrent id=%s %s is unplayable; it is always returning a rar file (it will no longer show up in your directories, zurg suggests you delete it)", info.ID, info.Name) - // t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash) - return nil - } else if streamableCount == 1 { - t.log.Warnf("Torrent id=%s %s is unplayable; the lone streamable link has expired (it will no longer show up in your directories, zurg suggests you delete it)", info.ID, info.Name) - // t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash) - return nil - } + t.log.Warnf("Torrent id=%s is partly expired, it has %d selected files but only %d links", info.ID, len(selectedFiles), len(info.Links)) } else if len(selectedFiles) == len(info.Links) { // all links are still intact! good! for i, file := range selectedFiles { @@ -482,14 +466,14 @@ func (t *TorrentManager) organizeChaos(links []string, selectedFiles []*File) ([ for _, link := range links { wg.Add(1) link := link // redeclare to avoid closure on loop variable - // Use the existing workerPool to submit tasks + // Use the existing worker pool to submit tasks err := t.antsPool.Submit(func() { defer wg.Done() resp := t.api.UnrestrictUntilOk(link) resultsChan <- Result{Response: resp} }) if err != nil { - t.log.Errorf("Error submitting task to workerPool: %v", err) + t.log.Errorf("Error submitting task to worker pool: %v", err) } } @@ -581,8 +565,13 @@ func (t *TorrentManager) Repair(accessKey string) { t.log.Infof("Evaluating whole torrent to find the correct files for torrent: %s", torrent.AccessKey) var selectedFiles []*File + var isChaotic bool var links []string + streamableCount := 0 torrent.SelectedFiles.IterCb(func(_ string, file *File) { + if isStreamable(file.Path) { + streamableCount++ + } fileCopy := &File{ File: file.File, Added: file.Added, @@ -595,7 +584,27 @@ func (t *TorrentManager) Repair(accessKey string) { } fileCopy.Link = "" // empty the links = chaos! }) - selectedFiles, _ = t.organizeChaos(links, selectedFiles) + selectedFiles, isChaotic = t.organizeChaos(links, selectedFiles) + // chaotic file means RD will not output the desired file selection + // // e.g. even if we select just a single mkv, it will output a rar + // var isChaotic bool + // selectedFiles, isChaotic = t.organizeChaos(info.Links, selectedFiles) + if isChaotic { + t.log.Warnf("Torrent %s is always returning an unplayable rar file (it will no longer show up in your directories, zurg suggests you delete it)", torrent.AccessKey) + t.DirectoryMap.IterCb(func(_ string, torrents cmap.ConcurrentMap[string, *Torrent]) { + torrents.Remove(torrent.AccessKey) + }) + // t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash) + return + } else if streamableCount == 1 { + t.log.Warnf("Torrent %s only file has expired (it will no longer show up in your directories, zurg suggests you delete it)", torrent.AccessKey) + t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", torrent.Instances[0].Hash) + t.DirectoryMap.IterCb(func(_ string, torrents cmap.ConcurrentMap[string, *Torrent]) { + torrents.Remove(torrent.AccessKey) + }) + return + } + // t.log.Debugf("Identified the expired files of torrent id=%s", info.ID) for _, newFile := range selectedFiles { if file, exists := torrent.SelectedFiles.Get(filepath.Base(newFile.Path)); exists { file.Link = newFile.Link