Add media info filters

This commit is contained in:
Ben Adrian Sarmiento
2024-06-05 19:19:03 +02:00
parent fa26553933
commit c63ce8da0a
7 changed files with 265 additions and 58 deletions

View File

@@ -13,25 +13,24 @@ import (
"github.com/debridmediamanager/zurg/pkg/utils"
mapset "github.com/deckarep/golang-set/v2"
cmap "github.com/orcaman/concurrent-map/v2"
"gopkg.in/vansante/go-ffprobe.v2"
)
func inProgressStatus(status string) bool {
return status == "downloading" || status == "uploading" || status == "queued" || status == "compressing"
}
func (t *TorrentManager) refreshTorrents() []string {
func (t *TorrentManager) refreshTorrents() {
t.inProgressHashes = mapset.NewSet[string]()
instances, _, err := t.api.GetTorrents(false)
if err != nil {
t.log.Warnf("Cannot get torrents: %v", err)
return nil
return
}
var wg sync.WaitGroup
var mergeChan = make(chan *Torrent, len(instances))
updatedPaths := mapset.NewSet[string]()
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
freshIDs := mapset.NewSet[string]()
@@ -62,15 +61,8 @@ func (t *TorrentManager) refreshTorrents() []string {
mainTorrent, exists := allTorrents.Get(accessKey)
if !exists {
allTorrents.Set(accessKey, torrent)
t.assignDirectory(torrent, func(directory string) {
listing, _ := t.DirectoryMap.Get(directory)
listing.Set(accessKey, torrent)
updatedPaths.Add(fmt.Sprintf("%s/%s", directory, accessKey))
})
t.writeTorrentToFile(torrent)
t.assignDirectory(torrent, true)
} else if !mainTorrent.DownloadedIDs.Contains(tInfo.ID) {
forMerging = torrent
}
@@ -102,20 +94,7 @@ func (t *TorrentManager) refreshTorrents() []string {
mainTorrent := t.mergeTorrents(existing, torrent)
allTorrents.Set(accessKey, mainTorrent)
t.writeTorrentToFile(mainTorrent)
t.DirectoryMap.IterCb(func(directory string, torrents cmap.ConcurrentMap[string, *Torrent]) {
if strings.HasPrefix(directory, "int__") {
return
}
torrents.Remove(accessKey)
})
t.assignDirectory(mainTorrent, func(directory string) {
listing, _ := t.DirectoryMap.Get(directory)
listing.Set(accessKey, mainTorrent)
updatedPaths.Add(fmt.Sprintf("%s/%s", directory, accessKey))
})
t.assignDirectory(mainTorrent, true)
}
// removed torrents
@@ -163,8 +142,6 @@ func (t *TorrentManager) refreshTorrents() []string {
}
})
})
return updatedPaths.ToSlice()
}
// StartRefreshJob periodically refreshes the torrents
@@ -183,10 +160,8 @@ func (t *TorrentManager) StartRefreshJob() {
}
t.setNewLatestState(checksum)
updatedPaths := t.refreshTorrents()
t.refreshTorrents()
t.log.Info("Finished refreshing torrents")
t.TriggerHookOnLibraryUpdate(updatedPaths)
case <-t.RefreshKillSwitch:
t.log.Info("Stopping periodic refresh job")
return
@@ -364,15 +339,28 @@ func (t *TorrentManager) mergeTorrents(existing, toMerge *Torrent) *Torrent {
return mergedTorrent
}
func (t *TorrentManager) assignDirectory(tor *Torrent, cb func(string)) {
func (t *TorrentManager) assignDirectory(tor *Torrent, triggerHook bool) {
accessKey := t.GetKey(tor)
t.DirectoryMap.IterCb(func(directory string, torrents cmap.ConcurrentMap[string, *Torrent]) {
if strings.HasPrefix(directory, "int__") {
return
}
torrents.Remove(accessKey)
})
torrentIDs := tor.DownloadedIDs.ToSlice()
// get filenames needed for directory conditions
var filenames []string
var fileSizes []int64
var mediaInfos []*ffprobe.ProbeData
unplayable := true
tor.SelectedFiles.IterCb(func(key string, file *File) {
filenames = append(filenames, filepath.Base(file.Path))
fileSizes = append(fileSizes, file.Bytes)
if file.MediaInfo != nil {
mediaInfos = append(mediaInfos, file.MediaInfo)
}
if utils.IsPlayable(file.Path) || t.IsPlayable(file.Path) {
unplayable = false
}
@@ -389,8 +377,13 @@ func (t *TorrentManager) assignDirectory(tor *Torrent, cb func(string)) {
configV1 := t.Config.(*config.ZurgConfigV1)
for _, directories := range configV1.GetGroupMap() {
for _, directory := range directories {
if t.Config.MeetsConditions(directory, t.GetKey(tor), tor.ComputeTotalSize(), torrentIDs, filenames, fileSizes) {
cb(directory)
if t.Config.MeetsConditions(directory, t.GetKey(tor), tor.ComputeTotalSize(), torrentIDs, filenames, fileSizes, mediaInfos) {
listing, _ := t.DirectoryMap.Get(directory)
listing.Set(accessKey, tor)
if triggerHook {
t.TriggerHookOnLibraryUpdate([]string{fmt.Sprintf("%s/%s", directory, accessKey)})
}
break
}
}