Skip to content

Instantly share code, notes, and snippets.

@werkkrew
Last active August 29, 2017 13:10
Show Gist options
  • Save werkkrew/62bf78db67b80f2a3fe0 to your computer and use it in GitHub Desktop.
Save werkkrew/62bf78db67b80f2a3fe0 to your computer and use it in GitHub Desktop.
Script to get called by Deluge on Torrent completion
#!/bin/bash
#### Torrent post-processing script.
# This script gets triggered by deluge and moves/extracts a file to another place for further processing (filebot)
# The reason this script does not handle the filebot processing itself (which it could) is for flexbility of using multiple methods of downloading (e.g. usenet)
# The idea is that a filebot script monitors a folder for new files and deals with them, regardless of the source.
# This script puts the files in a central location for processing and allows them to continue seeding.
# Uncomment the following line to enable debugging
#set -x
if [ $# -lt 3 ]
then
echo "Not enough arguments supplied"
echo "Parameters for interactive use are <torrent_id> <torrent_name> <torrent_path>"
echo "torrent_id is not used currently so it can be an arbitrary value such as 0"
exit 1
fi
command -v unrar >/dev/null 2>&1 || { echo >&2 "I require unrar but it's not installed or in my $PATH. Aborting."; exit 1; }
# Set up some logging
SCRIPT_NAME="$(basename "$0" .sh)"
LOGDIR="${HOME}/.logs"
LOGFILE="$SCRIPT_NAME-$(date +"%m%d%Y").log"
exec > >(tee -i $LOGDIR/$LOGFILE)
exec 2>&1
echo "Logging to: ${LOGDIR}/${LOGFILE}"
# Delete old log files, comment this out if you just want to use logrotate or something
echo "Deleting log files older than 7 days..."
find $LOGDIR/ -name '$SCRIPT_NAME*.log' -type f -mtime +7 -delete
TORRENT_ID=$1
TORRENT_NAME=$2
TORRENT_PATH=$3
TORRENT="${TORRENT_PATH}/${TORRENT_NAME}"
echo "PROCESSING ----> $TORRENT"
# The base directory where torrents we want are seeded from (not autodl torrents etc.)
WANT_PATH="${HOME}/downloads/data/wanted"
# The directory we download from
DEST_PATH="${HOME}/downloads/completed"
# Temporary location to extract to, such that files being written do not get processed
EXTR_PATH="${HOME}/extracting"
echo "BASE Path for wanted files: $WANT_PATH"
echo "BASE Path for completed files: $DEST_PATH"
echo "BASE Path for temporary extracted files: $EXTR_PATH"
echo "Category will be added to the end of each base path..."
echo ""
# If the torrent is not in our "wanted" directory, bail out
if [[ $TORRENT != *$WANT_PATH* ]]; then
echo "Torrent is not in our wanted path, exiting."
exit 0
else
# assign a category to the torrent based on the folder it is in, if it just in the base of our "wanted" folder default to "other"
CATEGORY=`basename $TORRENT_PATH`
if [ $CATEGORY == "wanted" ]; then
CATEGORY = "other"
fi
echo "Download category set to $CATEGORY"
DEST_PATH+="/$CATEGORY"
mkdir -p "${DEST_PATH}"
EXTR_PATH+="/$CATEGORY"
mkdir -p "${EXTR_PATH}"
echo "Destination set to $DEST_PATH"
fi
# rar filter list, looks in same directory as script for a file called "unrar_files.lst", if not present defaults to *
UNRAR_MASK="@"
UNRAR_LIST="$SCRIPT_PATH/unrar_files.lst"
if [ ! -f $UNRAR_LIST ]; then
UNRAR_MASK=""
UNRAR_LIST="*"
fi
FILE_FILTER=$UNRAR_MASK$UNRAR_LIST
echo "Using $FILE_FILTER for unrar filter"
# Fix the IFS to deal with spaces in files
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
echo "Look for and sync files which are not in RAR archives and sync."
echo "Source folder: $TORRENT -----> Destination folder: $DEST_PATH/$TORRENT_NAME"
# Sync up any non-archive type files that we want
rsync -av --ignore-existing --prune-empty-dirs $TORRENT/ $DEST_PATH/$TORRENT_NAME \
--exclude '*.rar' \
--exclude '*.r[0-9][0-9]' \
--exclude '*.[0-9][0-9][0-9]' \
--exclude '*.nfo' \
--exclude '*.sfv'
echo "Done Syncing non-archive media."
# look for RAR archives
echo "Looking for archives (RAR) for extraction..."
for archive in $( find $TORRENT -maxdepth 1 -regextype egrep -type f -iregex '.*\.(rar)$' | sort ); do
echo "Extracting $archive TO $EXTR_PATH"
cd "${EXTR_PATH}"
unrar x -ad -idq -y -o+ -p- "${archive}" "${FILE_FILTER}"
if [ $? -eq 0 ]; then
echo "--> Successfully extracted."
echo "Move extracted files."
mv -f $EXTR_PATH/* $DEST_PATH/
else
echo "--> Error: Unable to extract."
exit 100
fi
done
echo "Done extracting all archives."
echo "Done processing!"
IFS=$SAVEIFS
@gallo-s-chingon
Copy link

is this script still in use by you? for some reason I it either isn't triggering in deluge on torrent complete, or something I edited to match my system is off.

@werkkrew
Copy link
Author

Sorry for the delay, I don't really pay attention to this.

No, I am using the process-pyroscope script with rtorrent now.

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