Auto-remove after X time script

Discussion of Transmission that doesn't fit in the other categories
teambritta
Posts: 2
Joined: Sat May 19, 2012 8:17 am

Auto-remove after X time script

Postby teambritta » Sat May 19, 2012 8:31 am

Hi all,

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

ZaPHoN
Posts: 9
Joined: Sun Aug 19, 2012 5:54 pm

Re: Auto-remove after X time script

Postby ZaPHoN » Tue Sep 18, 2012 8:23 am

I keep getting a invalid syntax in the Target path.

In Terminal:

Code: Select all

python /home/mario/Downloads/scripttest/timenuke.sh


Result:

Code: Select all

File "/home/mario/Downloads/scripttest/timenuke.sh", line 6
    TARGET=/home/mario/Downloads/complete
           ^
SyntaxError: invalid syntax

teambritta
Posts: 2
Joined: Sat May 19, 2012 8:17 am

Re: Auto-remove after X time script

Postby teambritta » Tue Sep 18, 2012 8:34 am

Hi ZePHoN,

You're having this issue because this is a shell scritpt, not python.

Here are two ways to run it:

Code: Select all

sh /path/to/script/timenuke.sh

OR

Code: Select all

chmod +x /path/to/script/timenuke.sh
/path/to/script/timenuke.sh

Cheers,

Team Britta

ZaPHoN
Posts: 9
Joined: Sun Aug 19, 2012 5:54 pm

Re: Auto-remove after X time script

Postby ZaPHoN » Tue Sep 18, 2012 7:22 pm

Ahhh Thank you so much. I'll try.

Now I get this:

Code: Select all

sh /home/mario/Downloads/scripttest/timenuke.sh
/home/mario/Downloads/scripttest/timenuke.sh: 57: /usr/bin/transmission-remote: not found


It seems that with the defalut install of the Transmission GTK there is no transmission-remote located in:

Code: Select all

BIN="/usr/bin/transmission-remote"


Can I simply install transmission remote through synaptic or get rid of the GTK which I have no use for anyway since I'll be accessing this machine remotely through web anyway?

blacke4dawn
Posts: 552
Joined: Sun Dec 13, 2009 10:44 pm

Re: Auto-remove after X time script

Postby blacke4dawn » Fri Sep 21, 2012 5:54 pm

I believe that transmission-remote is packaged together with the daemon version among the deb-packages.

berkz
Posts: 1
Joined: Mon Nov 05, 2012 10:27 pm

Re: Auto-remove after X time script

Postby berkz » Mon Nov 05, 2012 10:44 pm

Hey man this script is excactly what i've been looking for. Any chance there is a mac os x friendly version? The Gnu extensions won't work in a standar mac os x shell.

asensio
Posts: 2
Joined: Thu May 30, 2013 5:02 am

Re: Auto-remove after X time script

Postby asensio » Thu May 30, 2013 5:19 am

Hi,
sorry to re-open a year old topic, but...

I only want to delete the torrent from the queue after a period of time, not its downloaded data. Is it possible to do it just by editing the line 54?

from

Code: Select all

$BIN -n $USER:$PASS -t $ID --remove-and-delete

to

Code: Select all

$BIN -n $USER:$PASS -t $ID --remove


and...
I have my series organized like this (i'm using flexget):
/path/to/serie1
/path/to/serie2
/path/to/serie3
...

the script "looks" inside each folder or just the TARGET?

Thank you,
Cheers

Remage
Posts: 1
Joined: Sun Dec 03, 2017 2:00 am

Re: Auto-remove after X time script

Postby Remage » Sun Dec 03, 2017 2:13 am

So I found this had issues if I used things like Sonarr and used a subdirectory etc... I made a update to this script that works properly with Sonarr/Radarr & setting a category. I didn't set a user name etc... because I run this as a cron job daily, but it can be altered to use login like the original script.

I'm sure there are other scripts that work better but this worked well enough for me.

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.
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 -l | grep 100%`; 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 -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 $NAME`; do
             AGE=`stat "$FILE" -c%Y`
             if [ $AGE -gt $LASTMODIFIED ]; then
                 LASTMODIFIED=$AGE
             fi
        done

    # Otherwise, just get the modified time.
    else
   FILE1=`find $TARGET -name $NAME`
        LASTMODIFIED=`stat "$FILE1" -c%Y`
    fi

    TIME=`date +%s`
    DIFF=`expr $TIME - $LASTMODIFIED`
    # Remove the torrent if its older than the CUTOFF.
    if [ $DIFF -gt $CUTOFF ]; then
        echo "Removing $NAME with ID:$ID"
        $BIN -t $ID --remove-and-delete
    fi

done

IFS=$OLDIFS


Return to “General”