Auto-remove after X time script

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

Auto-remove after X time script

Post by 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

Post by 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

Post by 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

Post by 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

Post by 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

Post by 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

Post by 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

Post by 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

Pacho
Posts: 2
Joined: Sat Sep 29, 2018 2:01 pm

Re: Auto-remove after X time script

Post by Pacho » Sat Dec 08, 2018 2:00 pm

Hi guys

Sorry for invading on the thread, is there anyway to do this on a Mac osx running transmission as well??

I have both the gui and the terminal version!

I just really want a script that can remove torrents in the program after a set amount of days...

damdim
Posts: 2
Joined: Sat Jun 01, 2019 5:13 am

Re: Auto-remove after X time script

Post by damdim » Sat Jun 01, 2019 5:43 am

Hi all. I tried the previous script in a QNAP NAS, and I couldn't make it work. Mainly I think, because I use multiple folders for completed torrents, and I only wanted it to take effect on a specific one. So I fiddle around and I modified it, to suit my situation. It uses the "Seeding Time" of the torrent info, and doesn't check the timestamps of the files. If you find a bug or have a suggestion, please post it. The script is checked with transmission 2.94 only.

Code: Select all

#!/bin/sh

# Automatically remove a torrent and delete its data after a specified period of
# time (in seconds).

# The target folder of completed torrents
TARGET="/share/Torrents/Downloaded"

# The executable with the passing arguments (I use a custom port)
BIN="/opt/bin/transmission-remote 127.0.0.1:49091 -n user:pass"

# The maximum seeding time in seconds
CUTOFF=2592000

# Get the list of torrents IDs
TORRENTLIST=`$BIN -l | sed -e '1d' -e '$d' | awk '{print $1}' | sed -e 's/[^0-9]*//g'`

# Loop for every torrent
	for ID in $TORRENTLIST
	do
    	
    	# Get Torrent name
    	TORRENTNAME=`$BIN -t $ID -i | grep "Name:" | cut -c 9-`
    	
    	# Determine the location  of the torrent.
    	DOWNPATH=`$BIN -t $ID -i | grep "Location:" | cut -c 13-`
    	
    	# Determine the seeding time  of the torrent.
    	SEEDTIME=`$BIN -t $ID -i | grep "Seeding Time" | cut -d "(" -f 2 | cut -c -5`
    	
    	# Check if Location=Target and Seeding Time is bigger than cutoff and remove torrent
   		if [ "$DOWNPATH" == "$TARGET" -a $SEEDTIME -gt $CUTOFF ]; then
    			echo "Removing $TORRENTNAME"
    			$BIN -t $ID --remove-and-delete
		else
    			echo "Not Removing $TORRENTNAME"
		fi
	done

damdim
Posts: 2
Joined: Sat Jun 01, 2019 5:13 am

Re: Auto-remove after X time script

Post by damdim » Sat Oct 12, 2019 12:00 pm

I played a little with sed and made some changes to the script

Code: Select all

#!/bin/sh

# Automatically remove a torrent and delete its data after a specified period of
# time (in seconds).

TARGET="/share/Torrents/Downloaded"
BIN="/opt/bin/transmission-remote 127.0.0.1:49091 -n user:pass"

#(in seconds).
CUTOFF=5184000

TORRENTLIST=`$BIN -l | sed -e '1d' -e '$d' | awk '{print $1}' | sed -e 's/[^0-9]*//g'`

for ID in $TORRENTLIST
do
    # Get Torrent name
    TORRENTNAME=`$BIN -t $ID -i | grep "Name:" | sed 's/.*Name: \(.*\)/\1/'`
    # Determine the location  of the torrent.
    DOWNPATH=`$BIN -t $ID -i | grep "Location:" | sed 's/.*Location: \(.*\)/\1/'`
    # Determine the seeding time  of the torrent.
    SEEDTIME=`$BIN -t $ID -i | grep "Seeding Time" | sed 's/.*(\(.*\) seconds)/\1/'`
    # Check if Location=Target and Seeding Time is bigger than cutoff and remove torrent
  # if [ "$DOWNPATH" == "$TARGET" -a $SEEDTIME -gt $CUTOFF ]; then
        if [ "$DOWNPATH" == "$TARGET" ]; then
          if [ $SEEDTIME -gt $CUTOFF ]; then
            echo -e "\033[0;31mRemoving $TORRENTNAME because Location is $DOWNPATH and Seeding Time is $SEEDTIME seconds\033[0m"
            $BIN -t $ID --remove-and-delete
          else
            echo -e "\033[0;32mNOT Removing $TORRENTNAME because Seeding Time is $SEEDTIME seconds\033[0m"
          fi
        else
        echo -e "\033[0;33mNOT Removing $TORRENTNAME because Location is $DOWNPATH\033[0m"
        fi
done

luizbird
Posts: 1
Joined: Mon Jul 06, 2020 5:18 pm

Re: Auto-remove after X time script

Post by luizbird » Mon Jul 06, 2020 5:19 pm

Hello.

I'm trying to implement this script but I keep getting errors.

What should be the "BIN" folder when the transmission is running on Docker?

Also, for the TARGET folder, I'm getting "Permission denied"..

Murph1y
Posts: 1
Joined: Sat Aug 22, 2020 9:07 am

Re: Auto-remove after X time script

Post by Murph1y » Sat Aug 22, 2020 9:19 am

I am trying to add for the custom extension file conditions but it is not working. Is there any solution?

ghdsports
Posts: 1
Joined: Sat Sep 05, 2020 2:17 pm

Re: Auto-remove after X time script

Post by ghdsports » Sat Sep 05, 2020 2:20 pm

1

After trying to use these examples, I hit a couple of issues:

The comparison operator should be greater than, not less than
filemtime returns the modified time. filectime is the time when a file's inode data is changed; that is, when the permissions, owner, group, or other metadata from the inode is updated, which may lead to unexpected results
I changed the example to use filemtime and fixed the comparison as follows:

<?php
$path = dirname(__FILE__).'/files';
if ($handle = opendir($path)) {

while (false !== ($file = readdir($handle))) {
if ((time()-filemtime($path.'/'.$file)) > 86400) { // 86400 = 60*60*24
if (preg_match('/\.txt$/i', $file)) {
unlink($path.'/'.$file);
}
}
}
}
?>

Post Reply