Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helper script for deluge. hardlinks completed torrents into another folder (and unrars it, 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.
#
# 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/downloads/seeding"
destDir="/home/user/files/downloads/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}"
# echo "hardlink ${srcPath} to ${destPath}" >> /tmp/torrent-complete.log
if [ -d "${srcPath}" ]
then
# srcPath is a directory, so make sure destPath exists and is a directory,
# then recursively link the *contents* of the srcPath directory into destPath
mkdir -p "${destPath}"
cp -la "${srcPath}/"* "${destPath}"
else
# srcPath is a file, so just link it
cp -la "${srcPath}" "${destPath}"
fi
#
# if there are .rar files, unpack them
# usually there is only one, but nothing prevents more than one..
#
for rarFile in `find "${destPath}" -name "*.rar"`
do
# echo "unrar ${rarFile}" >> /tmp/torrent-complete.log
# if the unrar completes without error, unlink the .rar file and its companions.
# the originals will live on in srcDir and continue to be seeded.
cd "$(dirname ${rarFile})" \
&& unrar e -inul -o+ "${rarFile}" \
&& find . -type f \( -regex '.*\.r[0-9][0-9]' -o -name '*.rar' \) -delete
done
# could unpack other archives here too, but it's preferable to decompress
# any already-compressed archives at the remote machine, not here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.