Page 1 of 10

Unrar and cleanup script - UPDATE 2013.05.24

Posted: Sat Jul 31, 2010 5:21 pm
by killemov
Hello,

I have implemented this script for Debian to run from transmission-daemon after a download has finished.

regards,

K.

Dependencies:
  • Debian (bash compatibility, system configuration)
  • unrar
  • sleep
  • transmission-remote
To make the script work you have to add/change these lines in a configuration file named "settings.json" (usually /etc/transmission-daemon/settings.json) while transmission-daemon is not running.

Code: Select all

    "script-torrent-done-enabled": true, 
    "script-torrent-done-filename": "/var/lib/scripts/posttorrent.sh", 
The posttorrent.sh script:

Code: Select all

#! /bin/bash
# posttorrent.sh by Killemov
{
  # Log file, file where we tell what events have been processed.
  LOG_FILE=/var/log/posttorrent.log
  # Username for transmission remote.
  TR_USERNAME="username"
  # Password for transmission remote.
  TR_PASSWORD="password"
  # Get current time.
  NOW=$(date +%Y-%m-%d\ %H:%M:%S)
  # Source directory, should not be changed.
  SRC_DIR="${TR_TORRENT_DIR}/${TR_TORRENT_NAME}"
  # Directory to store the un-compressed files in..
  DEST_DIR="${TR_TORRENT_DIR}/${TR_TORRENT_NAME}/"
  # This parameter string could be passed from Transmission in the future.
  TR_TORRENT_PARAMETER="EXTRACT SLEEP1h"

  if [ -e "$SRC_DIR/keep" ]; then
    TR_TORRENT_PARAMETER="$TR_TORRENT_PARAMETER KEEP"
  fi

  if [ -e "$SRC_DIR/exit" ]; then
    TR_TORRENT_PARAMETER="EXIT"
  fi

  # Actual processing starts here.
  if [[ "$TR_TORRENT_PARAMETER" =~ "EXIT" ]]; then
    echo $NOW "Exiting $TR_TORRENT_NAME" >> $LOG_FILE
    exit 0
  fi

  if [[ "$TR_TORRENT_PARAMETER" =~ "EXTRACT" ]]; then
    cd $TR_TORRENT_DIR
    if [ -d "$SRC_DIR" ]; then
      IFS=$'\n'
      unset RAR_FILES i
      for RAR_FILE in $( find "$SRC_DIR" -type f -iname "*.rar" ); do
        if [[ $RAR_FILE =~ .*part.*.rar ]]; then
          if [[ $RAR_FILE =~ .*part0*1.rar ]]; then
            RAR_FILES[i++]=$RAR_FILE
          fi
        else
          RAR_FILES[i++]=$RAR_FILE
        fi
      done
      unset IFS

      if [ ${#RAR_FILES} -gt 0 ]; then
        for RAR_FILE in "${RAR_FILES[@]}"; do
          unrar x -inul "$RAR_FILE" "$DEST_DIR"
          if [ $? -gt 0 ]; then
            echo $NOW "Error unrarring $TR_TORRENT_NAME" >> $LOG_FILE
            transmission-remote -n $TR_USERNAME:$TR_PASSWORD -t $TR_TORRENT_HASH --verify --start
            exit 0
          fi
        done
        if [[ ! "$TR_TORRENT_PARAMETER" =~ "KEEP" ]]; then
          SLEEP=$(expr match "$TR_TORRENT_PARAMETER" '.*SLEEP\([0-9a-zA-Z]*\)')
          if [ ${#SLEEP} -gt 0 ]; then
            sleep $SLEEP
          fi
          transmission-remote -n $TR_USERNAME:$TR_PASSWORD -t $TR_TORRENT_HASH --remove-and-delete
        fi
        echo $NOW "Unrarred $TR_TORRENT_NAME" >> $LOG_FILE
      fi
    fi
  fi
} &
Control files:
  • "exit" - If a file named exit is present in the root of the torrents' folder then no processing is done at all.
  • "keep" - If a file named keep is present in the root of the torrents' folder then no files will be removed.

Re: Implemented unrar and cleanup script

Posted: Thu Sep 02, 2010 9:36 am
by golddk
I am trying to use this script but had no luck. Anyone made it work ?

I am using Transmission 2.04 on a Synology 110j NAS.

Re: Implemented unrar and cleanup script

Posted: Wed Sep 15, 2010 8:07 pm
by killemov
golddk wrote:I am trying to use this script but had no luck. Anyone made it work ?

I am using Transmission 2.04 on a Synology 110j NAS.
Do you have the stock firmware on it? Then it probably isn't equipped to run even the most basic commands in the script. Try running unrar from the shell for instance.

Re: Implemented unrar and cleanup script

Posted: Fri Oct 08, 2010 10:51 am
by tigertailz
Cool, is it possible to choose wich dir it will unrar to ? and if I don't wan't it to keep the torrent seeded I just comment the following line right?

transmission-remote -n name:password -t$TR_TORRENT_ID --remove-and-delete &

Re: Implemented unrar and cleanup script

Posted: Fri Oct 08, 2010 11:15 am
by killemov
tigertailz wrote:Cool, is it possible to choose wich dir it will unrar to ? and if I don't wan't it to keep the torrent seeded I just comment the following line right?

transmission-remote -n name:password -t$TR_TORRENT_ID --remove-and-delete &
Yes and Yes.

See the options for unrar on how to unrar to a FIXED directory. All your content from rarred torrents will end up in a single directory.

Re: Implemented unrar and cleanup script

Posted: Fri Oct 08, 2010 11:24 am
by tigertailz
# posttorrent.sh by Killemov
cd $TR_TORRENT_DIR
if [ -d "$TR_TORRENT_NAME" ]
then
if ls "$TR_TORRENT_NAME"/*.rar > /dev/null 2>&1
then
find "$TR_TORRENT_NAME" -iname "*.rar" | while read file
do
unrar x -inul /media/wd1000/unrared "$file"
done
echo "Unrarred" $TR_TORRENT_NAME >> /var/log/posttorrent.log
fi
fi


Like this aight?

Re: Implemented unrar and cleanup script

Posted: Fri Oct 08, 2010 12:06 pm
by killemov
No!

unrar x -inul "$file" /media/wd1000/unrared

Re: Implemented unrar and cleanup script

Posted: Fri Oct 08, 2010 12:22 pm
by tigertailz
Alright, thank you. =)

Re: Implemented unrar and cleanup script

Posted: Tue Oct 19, 2010 2:04 pm
by Lampy
killemov wrote:
golddk wrote:I am trying to use this script but had no luck. Anyone made it work ?

I am using Transmission 2.04 on a Synology 110j NAS.
Do you have the stock firmware on it? Then it probably isn't equipped to run even the most basic commands in the script. Try running unrar from the shell for instance.
Well I have the same problem. In telnet I can use the unrar command but de script gave some errors. Is there somebody who can tell how to get this script working on a synology disk station? I'm kinda linux n00b....

Re: Implemented unrar and cleanup script

Posted: Sun Oct 24, 2010 9:21 pm
by taz
hi i have been trying to use you script but i cant get it to work i am running ubuntu 10.10 any ide what i can do?

Re: Implemented unrar and cleanup script

Posted: Sat Oct 30, 2010 6:41 pm
by blacke4dawn
taz wrote:hi i have been trying to use you script but i cant get it to work i am running ubuntu 10.10 any ide what i can do?
If it gives you any error messages then please post them since otherwise we don't know where to start helping you.

Re: Implemented unrar and cleanup script

Posted: Sun Oct 31, 2010 12:09 am
by taz
sorry i did not give any info but i am a bit new to linux, but i got it to work :D and it is Great, what would i have to add to get the script to copy all files if ther is no rar files?

Re: Implemented unrar and cleanup script

Posted: Sun Oct 31, 2010 11:31 am
by blacke4dawn
Personally I would use links instead of copying it, saves lots of space. Although if you are transferring it to another computer then of course copy is better.

Maybe something like this would do. NOTE: Only done minimal testing to get any syntax errors out.

First you'll have to change the DEST_DIR-variable to your preferred dir. You can add "$DEST_DIR" to the unrar line if you also want your unrars to end up there.
Second, choosing if copying or linking. Remove the # in front of the cp-line at the bottom if copying or the linker-line if linking.

Code: Select all

#!/bin/sh
# posttorrent.sh by Killemov

DEST_DIR="/some/other/dir"

function linker() {
  if [ ! -d "$DEST_DIR/$1" ]; then mkdir "$DEST_DIR/$1"; fi
  cd "$1"
  for F in *; do
    if [ -d "$F" ]; then linker "$1/$F"; fi
    ln -s "$TR_TORRENT_DIR/$1/$F" "$DEST_DIR/$1"
  done
}

cd $TR_TORRENT_DIR
if [ -d "$TR_TORRENT_NAME" ]
then
  if ls "$TR_TORRENT_NAME"/*.rar > /dev/null 2>&1
  then
    find "$TR_TORRENT_NAME" -iname "*.rar" | while read file
    do
      unrar x -inul "$file"
    done
    transmission-remote -n name:password -t$TR_TORRENT_ID --remove-and-delete &
    echo "Unrarred $TR_TORRENT_NAME" >> /var/log/posttorrent.log
  else
    # cp -r "$TR_TORRENT_NAME" "$DEST_DIR"
    # linker "$TR_TORRENT_NAME"
  fi
fi
If you want it to also work on single-file torrents then add this between the two fi-files:

Code: Select all

else
  # cp "$TR_TORRENT_NAME" "$DEST_DIR"
  # ln -s "$TR_TORRENT_NAME" "$DEST_DIR"
And same here, cp-line for copy or ln-line for linking.


If linking then I recommend getting the symlinks program which will make cleaning up broken links (after removing the original) much easier.

Re: Implemented unrar and cleanup script

Posted: Thu Jan 13, 2011 11:15 pm
by mcmanuf
Is it possible to extend this script to support *zip file format as well?

Re: Implemented unrar and cleanup script

Posted: Fri Jan 14, 2011 11:53 am
by blacke4dawn
Of course it is:

Code: Select all

#!/bin/sh
# posttorrent.sh by Killemov

DEST_DIR="/some/other/dir"

function linker() {
  if [ ! -d "$DEST_DIR/$1" ]; then mkdir "$DEST_DIR/$1"; fi
  cd "$1"
  for F in *; do
    if [ -d "$F" ]; then linker "$1/$F"; fi
    ln -s "$TR_TORRENT_DIR/$1/$F" "$DEST_DIR/$1"
  done
}

cd $TR_TORRENT_DIR
if [ -d "$TR_TORRENT_NAME" ]
then
  if ls "$TR_TORRENT_NAME"/*.rar > /dev/null 2>&1
  then
    find "$TR_TORRENT_NAME" -iname "*.rar" | while read file
    do
      unrar x -inul "$file"
    done
    transmission-remote -n name:password -t$TR_TORRENT_ID --remove-and-delete &
    echo "Unrarred $TR_TORRENT_NAME" >> /var/log/posttorrent.log
  elif ls "$TR_TORRENT_NAME"/*.zip > /dev/null 2>&1
  then
    find "$TR_TORRENT_NAME" -iname "*.zip" | while read file
    do
      unzip "$file"
    done
    transmission-remote -n name:password -t$TR_TORRENT_ID --remove-and-delete &
    echo "Unzipped $TR_TORRENT_NAME" >> /var/log/posttorrent.log
  else

    # cp -r "$TR_TORRENT_NAME" "$DEST_DIR"
    # linker "$TR_TORRENT_NAME"
  fi
fi