Skip to content

Instantly share code, notes, and snippets.

@no-jochs
Last active January 14, 2021 07:23
Show Gist options
  • Save no-jochs/4b023b3e99303c10c7ff9e8399ce4941 to your computer and use it in GitHub Desktop.
Save no-jochs/4b023b3e99303c10c7ff9e8399ce4941 to your computer and use it in GitHub Desktop.
A script for moving completed torrents as they finish.
#!/bin/bash
# This is a script which is executed each time a torrent finishes downloading. It should move the torrent's data out of the download directory and into the appropriate folder in Media.
# The positional arguments below represent:
# (1) The directory the torrent was downloaded to.
# (2) The name of the torrent file downloaded (does not include any segments of the download path).
# (3) The "torrent ID" is the numeric integer given in the ID column produced with the command `transmission-remote -l`. It represents a single file of data.
TR_TORRENT_DIR=${TR_TORRENT_DIR:-$1}
TR_TORRENT_NAME=${TR_TORRENT_NAME:-$2}
TR_TORRENT_ID=${TR_TORRENT_ID:-$3}
CATEGORY=
case $TR_TORRENT_DIR in
/data/completed/Adult)
CATEGORY=Adult
;;
/data/completed/Movies)
CATEGORY=Movies
;;
/data/completed/TV)
CATEGORY=TV
;;
*)
CATEGORY=Other
;;
esac
TR_FULL_PATH="$TR_TORRENT_DIR/$TR_TORRENT_NAME"
DESTINATION_DIR="/data/completed/${CATEGORY}
if [ -f $TR_FULL_PATH ]
then
EXTENSION=
if [[ $FULL_PATH =~ \.([a-zA-Z0-9]+)$ ]]
then
EXTENSION=${BASH_REMATCH[1]}
fi
case $EXTENSION in
zip)
7z e $TR_FULL_PATH -y -o $DESTINATION_DIR
;;
gz | tar)
tar -xzf $TR_FULL_PATH -C $DESTINATION_DIR
;;
*)
transmission-remote localhost:9091 -n jho:jhopass -t "${TR_TORRENT_ID}" --move "${DESTINATION_DIR}"
;;
esac
elif [ -d $TR_FULL_PATH ]
then
RAR_FILE=$(ls -1 $TR_FULL_PATH | grep -iE '.*.rar$')
if [[ -n $RAR_FILE ]]
then
unrar e "${TR_FULL_PATH}/${RAR_FILE}" "${DESTINATION_DIR}"
else
transmission-remote localhost:9091 -n jho:jhopass -t "${TR_TORRENT_ID}" --move "${DESTINATION_DIR}"
fi
else
echo "Unable to discern filetype for: ${TR_FULL_PATH} - copy/move operations are incomplete."
fi
chown -R plex:users /data/completed /data/media
chmod -R 755 /data/completed /data/media
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment