Last active
February 21, 2022 11:28
-
-
Save werkkrew/587a3703354583832bc1 to your computer and use it in GitHub Desktop.
Torrent Processing Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#### Torrent post-processing script. | |
# This script runs on an interval and checks the status of labels in rTorrent via PyroScope. | |
# If the label matches one which we want to process, extract/copy/move the files to where we collect them from. | |
# After processing, change the label to something else which we will then not process again. | |
# | |
# | |
# Requires: rTorrent or rTorrent-PS | |
# PyroScope tools (https://pyrocore.readthedocs.org/en/latest/) | |
# | |
# Bryan Chain (werkkrew) 2016 | |
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")" | |
SCRIPT_PATH=$(pwd) | |
NOW=$(date +"%m%d%Y") | |
LOGDIR="${HOME}/.logs" | |
LOGFILE="$SCRIPT_NAME-$NOW.log" | |
SAVEIFS=$IFS | |
# Test if script is run interactively, if so check for paramters and do not log | |
if [[ $- != *i* ]]; then | |
exec 1>> "$LOGDIR"/"$LOGFILE" 2>&1 | |
echo | |
echo "----------------------------------------------------------" | |
echo | |
echo "Non-interactive shell, lets log!" | |
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 '*.log' -type f -mtime +7 -exec rm {} \; | |
else | |
if [[ $# -gt 0 ]]; then | |
LABELS=$1 | |
fi | |
fi | |
#################### USER DEFINED SETTINGS | |
########################################################### | |
# The directory we place files, this is where we pull data from using an outside server (our house) | |
DEST_PATH="${HOME}/private/downloads/completed" | |
# Temporary location to extract to, such that files being written do not get downloaded | |
EXTR_PATH="${HOME}/private/extracting" | |
# label we set after processing | |
FINISHED="processed" | |
ERROR="error" | |
# output format string, probably shouldn't edit this | |
# rtcontrol -q custom_1=tv -o "%(custom_1)s,%(name)s,%(realpath)s" | |
LABEL_FIELD="custom_1" | |
FORMAT="${LABEL_FIELD},name,realpath,kind_95,hash,directory" | |
# labels we want to process, comma separated list fed into the rtcontrol query | |
LABELS="movie,tv,anime,software,music,pr0n,ebook,other" | |
################### | |
# clean up empty directories left behind by LFTP | |
find "$DEST_PATH"/* -mindepth 1 -type d -exec rmdir '{}' \; | |
rtcontrol -q $LABEL_FIELD=$LABELS is_complete=yes | |
if [[ $? -ne 0 ]]; then | |
echo "rtcontrol had non-zero return code, this means either no results came from the query or rtorrent is not running." | |
exit 1 | |
fi | |
# Prevent the script from running more than once | |
LOCKFILE="${HOME}/scripts/$SCRIPT_NAME.lock" | |
trap "rm -f $LOCKFILE" SIGINT SIGTERM | |
if [[ -e "$LOCKFILE" ]] | |
then | |
echo "$SCRIPT_NAME is running already." | |
exit | |
else | |
touch "$LOCKFILE" | |
fi | |
( rtcontrol -q $LABEL_FIELD=$LABELS is_complete=yes -o $FORMAT | tr \\t , ) | while IFS=',' read -r -a download | |
do | |
category=${download[0]} | |
name=${download[1]} | |
source=${download[2]} | |
type=${download[3]} | |
hash=${download[4]} | |
path=${download[5]} | |
if [[ -f $source ]]; then | |
echo "Source Torrent is an individual file without a containing folder..." | |
# Leave the source variable alone as it will contain the full path and file name | |
# But adjust the name field to remove the file extension | |
name=${name%.*} | |
else | |
# Add a trailing slash to the source path when it's not a regular file | |
source=${source}/ | |
fi | |
echo "Download category set to $category" | |
destination="$DEST_PATH/$category" | |
mkdir -p "${destination}" | |
echo "Destination set to $destination" | |
extract="$EXTR_PATH/$category/$name" | |
mkdir -p "${extract}" | |
echo "Temporary folder set to $extract" | |
# Fix the IFS to deal with spaces in files | |
IFS=$(echo -en "\n\b") | |
if [[ $type != "rar" ]]; then | |
echo "Download is not an archive, using rsync to copy" | |
# Sync up any non-archive type files that we want | |
rsync -av --ignore-existing --prune-empty-dirs "${source}" "${extract}" \ | |
--exclude '*.rar' \ | |
--exclude '*.r[0-9][0-9]' \ | |
--exclude '*.[0-9][0-9][0-9]' \ | |
--exclude '*.nfo' \ | |
--exclude '*.sfv' | |
if [[ $? -eq 0 ]]; then | |
echo "--> Successfully copied $name." | |
echo "Move extracted files." | |
mv -f "$extract" "$destination"/ | |
rtcontrol -q hash="${hash}" --custom=1=${FINISHED} | |
else | |
echo "--> Unable to rsync $name." | |
rtcontrol -q hash="${hash}" --custom=1=${ERROR} | |
fi | |
else | |
echo "Download is an archive, extracting..." | |
# look for RAR archives | |
archives=0 | |
rc=0 | |
echo "Finding archive files..." | |
for archive in $( find "$source" -maxdepth 2 -regextype egrep -type f -iregex '.*\.(rar)$' | sort ); do | |
archives=$(expr $archives + 1) | |
echo "Extracting $archive TO $extract" | |
cd "${extract}" | |
unrar x -idq -y -o+ -p- "${archive}" | |
if [ $? -eq 0 ]; then | |
echo "--> Successfully extracted $name." | |
else | |
echo "--> Error extracting $name." | |
rc=$(expr $rc + 1) | |
fi | |
done | |
## Check for errors during extraction | |
if [ $rc -eq 0 ]; then | |
echo "--> $archives archives were extracted from $name." | |
echo "--> All extracted successfull!" | |
echo "Move extracted files." | |
mv -f "$extract" "$destination"/ | |
rtcontrol -q hash="${hash}" --custom=1=${FINISHED} | |
elif [ $rc -gt 0 ]; then | |
echo "--> Error: Unable to extract $name." | |
rtcontrol -q hash="${hash}" --custom=1=${ERROR} | |
elif [ $archives -eq 0 ]; then | |
echo "--> Error: No archives found in $name." | |
rtcontrol -q hash="${hash}" --custom=1=${ERROR} | |
else | |
echo "--> Error: Undefined Error on $name." | |
rtcontrol -q hash="${hash}" --custom=1=${ERROR} | |
fi | |
fi | |
done | |
rtcontrol --from-view stopped is_complete=yes --start | |
echo "Done processing!" | |
rm -f "$LOCKFILE" | |
trap - SIGINT SIGTERM | |
IFS=$SAVEIFS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment