Wrote this script for a communal Transmission instance.
Our process is anyone can add a torrent, but they do not remove it upon completion. Instead they have to copy it another location (either their PC or another shared location) and leave it there for anyone else to get a copy. After the download is 10 days old, it is deleted.
Improvements needed:
I'm not sure why, but I wasn't able to generalise the call to transmission-remote with authentication, so if you don't have authentication you'll need to get rid of any instance of "-n $USER:$PASS" (lines 23, 29 and 54)
It works best in conjunction with cron!
Requirements:
- Transmission 2.03 (should work on others?)
Shell with GNU Extensions (for + regex operator)
Code: Select all
#!/bin/sh
# Automatically remove a torrent and delete its data after a specified period of
# time (in seconds).
TARGET=This is where you put your completed torrents.
USER=Username
PASS=Password
BIN="/usr/bin/transmission-remote"
# The default is 10 days (in seconds).
CUTOFF=`expr 86400 \* 10`
##############################################
### You shouldn't need to edit below here. ###
##############################################
# Tokenise over newlines instead of spaces.
OLDIFS=$IFS
IFS="
"
for ENTRY in `$BIN -n $USER:$PASS -l | grep 100%.*Done.*Finished`; do
# Pull the ID out of the listing.
ID=`echo $ENTRY | sed "s/^ *//g" | sed "s/ *100%.*//g"`
# Determine the name of the downloaded file/folder.
NAME=`$BIN -n $USER:$PASS -t $ID -f | head -1 |\
sed "s/ ([0-9]\+ files)://g"`
# If it's a folder, find the last modified file and its modification time.
if [ -d "$TARGET/$NAME" ]; then
LASTMODIFIED=0
for FILE in `find $TARGET/$NAME`; do
AGE=`stat "$FILE" -c%Y`
if [ $AGE -gt $LASTMODIFIED ]; then
LASTMODIFIED=$AGE
fi
done
# Otherwise, just get the modified time.
else
LASTMODIFIED=`stat "$TARGET/$NAME" -c%Y`
fi
TIME=`date +%s`
DIFF=`expr $TIME - $LASTMODIFIED`
# Remove the torrent if its older than the CUTOFF.
if [ $DIFF -gt $CUTOFF ]; then
date
echo "Removing $NAME with ID:$ID"
$BIN -n $USER:$PASS -t $ID --remove-and-delete
fi
done
IFS=$OLDIFS