Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helper script for deluge. It hardlinks completed torrents into a destination folder (or unrars it directly into the destination, if appropriate)
#!/bin/bash
# Helper script for the Deluge torrent client
# Intended to be run at torrent completion, using the 'torrent complete' event in the 'Execute' plugin.
#
# The basic idea is to hardlink the files that deluge has just finished downloading to a second directory.
# This allows you to configure deluge to automatically pause or delete torrents when they reach a given seed ratio,
# while also keeping a copy around for other reasons. For example, SyncThing could be used to propagate new downloads
# to a remote machine to be processed further. When processing has finished, and the file is deleted/moved out of the
# Syncthing folder, the remote Syncthing will propagate a deletion back to the original Synchting (on the machine
# running deluge).
#
# The end result is that the lifetime of files involved both in deluge's seeding process and the 'forward to somewhere
# else' process (e.g. via Syncthing) are decoupled, and can safely execute in parallel without needing to be aware of
# what the other is doing. And yet the net result is that the files will still be cleaned up automagically when both
# have finished their respective tasks.
#
# Note: if you use the Label plugin, note that if you change the location where completed torrents should be moved to,
# remember that each label may also have a location stored in each which will also be need to be updated.
#
# Paul Chambers, Copyright (c) 2019.
#
# Made available under the Creative Commons 'BY' license
# https://creativecommons.org/licenses/by/4.0/
#
set -x
torrentId=$1
torrentName=$2
torrentPath=$3
# echo "$torrentId $torrentName $torrentPath" >> /tmp/torrent-complete.log
srcDir="/home/user/files/seeding"
destDir="/home/user/files/complete"
label="${torrentPath#$srcDir}"
# note that srcPath may be a file, not necessarily a
# directory. Which means the same is true for destPath.
srcPath="${torrentPath}/${torrentName}"
destPath="${destDir}${label}/${torrentName}"
# We may be given a file or a directory. If it's a directory, it may contain one or more
# rar files, in which case we unpack each one directly into the destination hierarchy.
# Non-rar files are always hardlinked to the destination.
if [ -d "${srcPath}" ]
then
# multiple rar files may be found in subdirectories, so handle each one, preserving hierarchy
find "${srcPath}" -name '*.rar' -print0 | while read -d $'\0' rarFile
do
path="$(dirname "${rarFile}")"
subDir="${path#$srcDir}"
mkdir -p "${destDir}/${subDir}"
unrar e -o+ "${rarFile}" "${destDir}/${subDir}"
done
# hardlink everything in the source directory (not rar-related), to the destination directory
find "${srcPath}" -mindepth 1 ! -regex '.*\.r[a0-9][r0-9]' -print0 | while read -d $'\0' nonRarFile
do
path="$(dirname "${nonRarFile}")"
subDir="${path#$srcDir}"
mkdir -p "${destDir}/${subDir}"
ln -v "${nonRarFile}" "${destDir}/${subDir}"
done
else
# we were passed a single file, not a directory
cp -la "${srcPath}" "${destPath}"
fi
# could unpack other archives here too, but it's preferable to decompress
# any already-compressed archives at the remote machine, not here.
@thedirektor

This comment has been minimized.

Copy link

@thedirektor thedirektor commented Jul 23, 2019

I was tired of the plugin solutions and was hoping to find an alternative. This is it. Unrar/Copy/Clean This works fantastic!
Thank you.

@paul-chambers

This comment has been minimized.

Copy link
Owner Author

@paul-chambers paul-chambers commented Jul 24, 2019

Glad you found it useful :)

@Theangelofspace15

This comment has been minimized.

Copy link

@Theangelofspace15 Theangelofspace15 commented Oct 6, 2019

How can all this b integrated on sonarr radarr? I used to use copy completed and unrar plugin to do this but copy completed not working now. Also how can it workd with labels so it copy to /LABEL/*

@paul-chambers

This comment has been minimized.

Copy link
Owner Author

@paul-chambers paul-chambers commented Oct 7, 2019

@Theangelofspace15

This comment has been minimized.

Copy link

@Theangelofspace15 Theangelofspace15 commented Oct 7, 2019

Thats no what I mean. Deluge use label and copy movies tvshows to different folder base on label when they r done. I want to know how to move the different files base on label with your script

@paul-chambers

This comment has been minimized.

Copy link
Owner Author

@paul-chambers paul-chambers commented Oct 7, 2019

It already supports labels. I use labels myself. See lines 35 and 40.

@Theangelofspace15

This comment has been minimized.

Copy link

@Theangelofspace15 Theangelofspace15 commented Oct 7, 2019

I see, but the label in the script is a variable that you created it is not? label="${torrentPath#$srcDir}"
Or would that get the label assigned on the torrent using the label or label plus plugin?

@Koyakami

This comment has been minimized.

Copy link

@Koyakami Koyakami commented Jan 9, 2020

Hi mate, complete beginner - Deluge -> Plugins -> Execute. Select Add, Event=Torrent Completed and what do I put in the command?

Do I need to transfer this .sh file over to my Deluge FTP and then type the file name into that?

OR do I copy and paste everything in the .sh file?

Cheers Paul

@TinTin192

This comment has been minimized.

Copy link

@TinTin192 TinTin192 commented Apr 18, 2020

Haven't been able to get this running for some reason. Perms are correct etc etc.. But just isn't executing...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment