From 4b441ce2d85bb48ce9ce3bfc26b2fe10d4b16189 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sat, 6 Jan 2024 21:58:10 +0100 Subject: [PATCH] Group all unplayable torrents in a separate directory --- internal/config/v1.go | 7 +++++-- internal/torrent/delete.go | 4 ++-- internal/torrent/manager.go | 10 +++++++++- internal/torrent/refresh.go | 12 +++++++----- internal/torrent/repair.go | 2 +- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/internal/config/v1.go b/internal/config/v1.go index ed8885d..634fcf0 100644 --- a/internal/config/v1.go +++ b/internal/config/v1.go @@ -13,7 +13,8 @@ import ( ) const ( - ALL_TORRENTS = "__all__" + ALL_TORRENTS = "__all__" + UNPLAYABLE_TORRENTS = "__unplayable__" ) func loadV1Config(content []byte, log *logutil.Logger) (*ZurgConfigV1, error) { @@ -30,13 +31,14 @@ func (z *ZurgConfigV1) GetVersion() string { } func (z *ZurgConfigV1) GetDirectories() []string { - rootDirectories := make([]string, len(z.Directories)+1) + rootDirectories := make([]string, len(z.Directories)+2) i := 0 for directory := range z.Directories { rootDirectories[i] = directory i++ } rootDirectories[i] = ALL_TORRENTS + rootDirectories[i+1] = UNPLAYABLE_TORRENTS return rootDirectories } @@ -74,6 +76,7 @@ func (z *ZurgConfigV1) GetGroupMap() map[string][]string { temp := make([]string, len(v)) copy(temp, v) result[k] = temp + // result[k] = append(result[k], UNPLAYABLE_TORRENTS) } result[ALL_TORRENTS] = []string{ALL_TORRENTS} // Add special group for all torrents diff --git a/internal/torrent/delete.go b/internal/torrent/delete.go index 10c22bc..09d3f34 100644 --- a/internal/torrent/delete.go +++ b/internal/torrent/delete.go @@ -16,7 +16,7 @@ func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool { torrent.DownloadedIDs.Each(func(id string) bool { infoCache.Set(id, torrent) t.writeTorrentToFile(id, torrent) - return true + return false }) } return false @@ -32,7 +32,7 @@ func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) { t.Api.DeleteTorrent(id) infoCache.Remove(id) t.deleteTorrentFile(id) - return true + return false }) } } diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index 70e8095..8ea4a04 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -9,6 +9,7 @@ import ( "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/pkg/logutil" "github.com/debridmediamanager/zurg/pkg/realdebrid" + "github.com/debridmediamanager/zurg/pkg/utils" mapset "github.com/deckarep/golang-set/v2" cmap "github.com/orcaman/concurrent-map/v2" "github.com/panjf2000/ants/v2" @@ -123,9 +124,13 @@ func (t *TorrentManager) assignedDirectoryCb(tor *Torrent, cb func(string)) { // get filenames needed for directory conditions var filenames []string var fileSizes []int64 + unplayable := true tor.SelectedFiles.IterCb(func(key string, file *File) { filenames = append(filenames, filepath.Base(file.Path)) fileSizes = append(fileSizes, file.Bytes) + if unplayable && utils.IsStreamable(file.Path) { + unplayable = false + } }) // Map torrents to directories switch t.Config.GetVersion() { @@ -133,7 +138,10 @@ func (t *TorrentManager) assignedDirectoryCb(tor *Torrent, cb func(string)) { configV1 := t.Config.(*config.ZurgConfigV1) for _, directories := range configV1.GetGroupMap() { for _, directory := range directories { - if t.Config.MeetsConditions(directory, tor.AccessKey, tor.ComputeTotalSize(), torrentIDs, filenames, fileSizes) { + if unplayable { + cb(config.UNPLAYABLE_TORRENTS) + break + } else if t.Config.MeetsConditions(directory, tor.AccessKey, tor.ComputeTotalSize(), torrentIDs, filenames, fileSizes) { cb(directory) break } diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index 7fb73e2..aae0aed 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -7,6 +7,7 @@ import ( "sync" "time" + "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/pkg/realdebrid" mapset "github.com/deckarep/golang-set/v2" cmap "github.com/orcaman/concurrent-map/v2" @@ -59,7 +60,7 @@ func (t *TorrentManager) RefreshTorrents() []string { // assign to directories tor, ok := allTorrents.Get(accessKey) if !ok { - return true + return false } var directories []string t.assignedDirectoryCb(tor, func(directory string) { @@ -69,18 +70,19 @@ func (t *TorrentManager) RefreshTorrents() []string { torrents, _ := t.DirectoryMap.Get(directory) torrents.Set(accessKey, tor) updatedPaths = append(updatedPaths, fmt.Sprintf("%s/%s", directory, accessKey)) - if directory != "__all__" { + // 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 true + return false }) // removed torrents t.allAccessKeys.Difference(freshKeys).Each(func(accessKey string) bool { t.Delete(accessKey, false) - return true + return false }) return updatedPaths @@ -212,7 +214,7 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent { toMerge.DownloadedIDs.Difference(existing.DownloadedIDs).Each(func(id string) bool { mainTorrent.DownloadedIDs.Add(id) mainTorrent.InProgressIDs.Remove(id) - return true + return false }) // the link can have the following values diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index e6990ca..79d97e5 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -40,7 +40,7 @@ func (t *TorrentManager) Repair(torrent *Torrent) { torrent.DownloadedIDs.Each(func(id string) bool { infoCache.Set(id, torrent) t.writeTorrentToFile(id, torrent) - return true + return false }) _ = t.repairWorker.Submit(func() { t.log.Infof("Repairing torrent %s", torrent.AccessKey)