Update hook passing paths now

This commit is contained in:
Ben Sarmiento
2023-11-23 03:03:52 +01:00
parent addac3cc9f
commit d82265a0ad
4 changed files with 37 additions and 19 deletions

View File

@@ -12,7 +12,10 @@ info_cache_time_hours: 12
enable_repair: true # BEWARE! THERE CAN ONLY BE 1 INSTANCE OF ZURG THAT SHOULD REPAIR YOUR TORRENTS enable_repair: true # BEWARE! THERE CAN ONLY BE 1 INSTANCE OF ZURG THAT SHOULD REPAIR YOUR TORRENTS
retain_folder_name_extension: false # if true, zurg won't modify the filenames from real-debrid retain_folder_name_extension: false # if true, zurg won't modify the filenames from real-debrid
on_library_update: | on_library_update: |
echo "hook" for arg in "$@"
do
echo "detected update on: $arg"
done
network_buffer_size: 1048576 # 1 MiB network_buffer_size: 1048576 # 1 MiB
preferred_hosts: # Run the script here https://github.com/debridmediamanager/real-debrid-network-test preferred_hosts: # Run the script here https://github.com/debridmediamanager/real-debrid-network-test

View File

@@ -19,6 +19,8 @@ func (se *ScriptExecutor) Execute() (string, error) {
return "", nil return "", nil
} }
cmd := exec.Command("/bin/sh", "-c", se.Script) cmd := exec.Command("/bin/sh", "-c", se.Script)
cmd.Args = append(cmd.Args, "zurg")
cmd.Args = append(cmd.Args, se.Args...)
var out bytes.Buffer var out bytes.Buffer
var stderr bytes.Buffer var stderr bytes.Buffer
cmd.Stdout = &out cmd.Stdout = &out

View File

@@ -29,6 +29,7 @@ const (
type TorrentManager struct { type TorrentManager struct {
DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent
checksum string checksum string
latestAdded string
requiredVersion string requiredVersion string
cfg config.ConfigInterface cfg config.ConfigInterface
api *realdebrid.RealDebrid api *realdebrid.RealDebrid
@@ -133,6 +134,8 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p
} }
go t.startRefreshJob() go t.startRefreshJob()
t.latestAdded = newTorrents[0].Added // set the latest added to the first torrent's added
return t return t
} }
@@ -268,23 +271,26 @@ func (t *TorrentManager) startRefreshJob() {
t.log.Infof("Fetched info for %d torrents", len(newTorrents)) t.log.Infof("Fetched info for %d torrents", len(newTorrents))
noInfoCount := 0 noInfoCount := 0
allTorrents, _ := t.DirectoryMap.Get(ALL_TORRENTS) oldTorrents, _ := t.DirectoryMap.Get(ALL_TORRENTS)
retain := make(map[string]bool) newSet := cmap.New[*Torrent]()
for info := range torrentsChan { for info := range torrentsChan {
if info == nil { if info == nil {
noInfoCount++ noInfoCount++
continue continue
} }
retain[info.AccessKey] = true if torrent, exists := oldTorrents.Get(info.AccessKey); exists {
if torrent, exists := allTorrents.Get(info.AccessKey); exists {
mainTorrent := t.mergeToMain(torrent, info) mainTorrent := t.mergeToMain(torrent, info)
allTorrents.Set(info.AccessKey, mainTorrent) oldTorrents.Set(info.AccessKey, mainTorrent)
newSet.Set(info.AccessKey, mainTorrent)
} else { } else {
allTorrents.Set(info.AccessKey, info) oldTorrents.Set(info.AccessKey, info)
newSet.Set(info.AccessKey, info)
} }
} }
allTorrents.IterCb(func(accessKey string, torrent *Torrent) { var updatedPaths []string
newSet.IterCb(func(_ string, torrent *Torrent) {
// get IDs // get IDs
var torrentIDs []string var torrentIDs []string
for _, instance := range torrent.Instances { for _, instance := range torrent.Instances {
@@ -301,7 +307,10 @@ func (t *TorrentManager) startRefreshJob() {
for _, directory := range directories { for _, directory := range directories {
if t.cfg.MeetsConditions(directory, torrent.AccessKey, torrentIDs, filenames) { if t.cfg.MeetsConditions(directory, torrent.AccessKey, torrentIDs, filenames) {
torrents, _ := t.DirectoryMap.Get(directory) torrents, _ := t.DirectoryMap.Get(directory)
torrents.Set(accessKey, torrent) torrents.Set(torrent.AccessKey, torrent)
if torrent.LatestAdded > t.latestAdded {
updatedPaths = append(updatedPaths, fmt.Sprintf("%s/%s", directory, torrent.AccessKey))
}
break break
} }
} }
@@ -310,9 +319,9 @@ func (t *TorrentManager) startRefreshJob() {
}) })
// delete torrents that no longer exist // delete torrents that no longer exist
accessKeys := allTorrents.Keys() oldAccessKeys := oldTorrents.Keys()
for _, oldAccessKey := range accessKeys { for _, oldAccessKey := range oldAccessKeys {
if _, ok := retain[oldAccessKey]; !ok { if _, ok := newSet.Get(oldAccessKey); !ok {
t.DirectoryMap.IterCb(func(_ string, torrents cmap.ConcurrentMap[string, *Torrent]) { t.DirectoryMap.IterCb(func(_ string, torrents cmap.ConcurrentMap[string, *Torrent]) {
torrents.Remove(oldAccessKey) torrents.Remove(oldAccessKey)
}) })
@@ -320,7 +329,7 @@ func (t *TorrentManager) startRefreshJob() {
} }
} }
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", oldTorrents.Count(), noInfoCount)
t.SetChecksum(t.getChecksum()) t.SetChecksum(t.getChecksum())
@@ -329,8 +338,9 @@ func (t *TorrentManager) startRefreshJob() {
t.repairAll() t.repairAll()
t.log.Info("Finished checking for torrents to repair") t.log.Info("Finished checking for torrents to repair")
} }
// TODO: pass the changed directories to the hook go OnLibraryUpdateHook(updatedPaths, t.cfg, t.log)
go OnLibraryUpdateHook([]string{}, t.cfg, t.log)
t.latestAdded = newTorrents[0].Added
} }
} }
@@ -610,11 +620,9 @@ func (t *TorrentManager) Repair(accessKey string) {
} }
fileCopy.Link = "" // empty the links = chaos! fileCopy.Link = "" // empty the links = chaos!
}) })
selectedFiles, isChaotic = t.organizeChaos(links, selectedFiles)
// chaotic file means RD will not output the desired file selection // 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 // e.g. even if we select just a single mkv, it will output a rar
// var isChaotic bool selectedFiles, isChaotic = t.organizeChaos(links, selectedFiles)
// selectedFiles, isChaotic = t.organizeChaos(info.Links, selectedFiles)
if isChaotic { 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.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]) { t.DirectoryMap.IterCb(func(_ string, torrents cmap.ConcurrentMap[string, *Torrent]) {

View File

@@ -25,12 +25,14 @@ type Torrent struct {
Name string `json:"filename"` Name string `json:"filename"`
Progress int `json:"-"` Progress int `json:"-"`
Links []string `json:"links"` Links []string `json:"links"`
Added string `json:"-"`
} }
func (i *Torrent) UnmarshalJSON(data []byte) error { func (i *Torrent) UnmarshalJSON(data []byte) error {
type Alias Torrent type Alias Torrent
aux := &struct { aux := &struct {
Progress float64 `json:"progress"` Progress float64 `json:"progress"`
Added string `json:"added"`
*Alias *Alias
}{ }{
Alias: (*Alias)(i), Alias: (*Alias)(i),
@@ -41,6 +43,9 @@ func (i *Torrent) UnmarshalJSON(data []byte) error {
} }
i.Progress = int(math.Round(aux.Progress)) i.Progress = int(math.Round(aux.Progress))
i.Added = strings.Replace(aux.Added, "Z", "+01:00", 1)
return nil return nil
} }