Unrar and cleanup script - UPDATE 2013.05.24

Discussion of Transmission that doesn't fit in the other categories
Post Reply
dexusno
Posts: 3
Joined: Tue Jan 31, 2012 10:28 pm

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by dexusno »

Hi!

I'm relatively new to linux scripting, and was wondering if it is possible to get the script to only unrar files IF the file is in a given folder?

I have set up some automation on tv-series download with a watch folder for new .torrent files.. I would like the script to unrar these tv series and movies that end up in the default download dir, but not if I specify another destination dir.

E.g. Everytime my script put a .torrent file in the watch-folder, Transmission downloads the files from that torrent, and puts them in the "/transmission/data" folder. I would like the script to unrar these to another folder, and leave the originals to be seeded.

However.. If I decide to manually add a torrent to transmission with a different destination folder, e.g "/transmission/data2" I would like the script to do nothing..

Is this possible?

Thanks for all help,

dexusno
killemov
Posts: 535
Joined: Sat Jul 31, 2010 5:04 pm

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by killemov »

Slightly modified answer from Unrar script for JUST the default download dir?. Put this before the actual processing comment line.

Code: Select all

if [ "$TR_TORRENT_DIR" != "/transmission/data" ]; then
    TR_TORRENT_PARAMETER="EXIT"
fi
You can control some behavior when you put a file the named "keep" or "exit" in the torrent's folder.
JRRandall
Posts: 2
Joined: Thu Mar 08, 2012 1:31 am

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by JRRandall »

I have a simple bash script to extract .rar and .zip files that I thought I would share. Basically it will create a subfolder named "extracted" inside the torrent directory, and places the extracted files there. It also prints status messages to a log file if you'd like. Other archive formats besides zip and rar can be added easily to the formants and comands arrays. Enjoy! Disclaimer: some of the original code I obtained from another torrent client (Deluge I think).

Code: Select all

#!/bin/bash
formats=(zip rar)
commands=([zip]="unzip -u" [rar]="unrar -o- e")
extraction_subdir='extracted'

torrentid=$TR_TORRENT_ID
torrentname=$TR_TORRENT_NAME
torrentpath=$TR_TORRENT_DIR

log()
{
    logger -t transmission-extractarchives "$@"
}

log "Torrent complete: $@"
cd "${torrentpath}"
for format in "${formats[@]}"; do
    while read file; do 
        log "Extracting \"$file\""
        cd "$(dirname "$file")"
        file=$(basename "$file")
        # if extraction_subdir is not empty, extract to subdirectory
        if [[ ! -z "$extraction_subdir" ]] ; then
            mkdir "$extraction_subdir"
            cd "$extraction_subdir"
            file="../$file"
        fi
        ${commands[$format]} "$file"
    done < <(find "$torrentpath/$torrentname" -iname "*.${format}" )
done
killemov
Posts: 535
Joined: Sat Jul 31, 2010 5:04 pm

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by killemov »

Why put that in this thread? It adds unzip but removes cleanup. Oh, and have you tested it with spaces in the file names?
JRRandall
Posts: 2
Joined: Thu Mar 08, 2012 1:31 am

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by JRRandall »

Because it adds unzip and you can easily add more archive formats if you wish (though I don't recall seeing much more than .rar and .zip in torrents... maybe .7z?) And yes it supports spaces in filenames! It doesn't do cleanup but it could be added. I just delete the original once I'm done seeding so cleanup wasn't a big deal to have for me. YMMV just thought I'd share a nice concise script that does the job wonderfully each time. :D
boondoklife
Posts: 12
Joined: Tue Oct 25, 2011 4:30 pm

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by boondoklife »

@ JRRandall Good work on making your own script, but you may wanna look into checking for .part[0-9]+.rar files (I wrote that in regular expression format as it is easy to represent that way). If you don't account for this then the script will try to unrar each part of the rared file (ex. foo.part01.rar, foo.part02.rar, foo.partn.rar)

You can check the original script for some info on that. I too wrote my own script a while back that handles zip and rars, it really is due for an update, just have to get the time to do so.
dinjo_jo
Posts: 7
Joined: Sat Aug 06, 2011 2:44 am

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by dinjo_jo »

Ok Seems like when there are multiple folders inside folders and if i download only some folders of it the extract script does not work

I tried to simulate the script but seems like the iname is not able find the rar archive , also tried the file to recognize the type and its recognizing the type of file but find can't

Code: Select all

file wind-sparta-pt2.r00
wind-sparta-pt2.r00: RAR archive data, v14, flags: Archive volume, os: Win32

Code: Select all

[root@DINJO /media/Data/data/torrents]$  find "/media/Data/data/torrents/Spartacus.Gods.of.the.Arena.S01" -iname "*.rar"
/media/Data/data/torrents/Spartacus.Gods.of.the.Arena.S01/Spartacus.Gods.of.the.Arena.Pt.III./wind-sparta-pt3.rar
/media/Data/data/torrents/Spartacus.Gods.of.the.Arena.S01/Spartacus.Gods.of.the.Arena.Pt.IV./wind-sparta-pt4.rar
/media/Data/data/torrents/Spartacus.Gods.of.the.Arena.S01/Spartacus.Gods.of.the.Arena.Pt.VI./wind-sparta-pt6.rar
/media/Data/data/torrents/Spartacus.Gods.of.the.Arena.S01/Spartacus.Gods.of.the.Arena.Pt.II./wind-sparta-pt2.rar
/media/Data/data/torrents/Spartacus.Gods.of.the.Arena.S01/Spartacus.Gods.of.the.Arena.Pt.V./wind-sparta-pt5.rar
killemov
Posts: 535
Joined: Sat Jul 31, 2010 5:04 pm

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by killemov »

This is a known issue. I encountered only one similar case before and found it to be too rare to handle it. A workaround might be to place a file named "keep" within the torrents' folder.
livings124
Transmission Developer
Posts: 3142
Joined: Fri Jan 13, 2006 8:08 pm

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by livings124 »

dingo_jo: Transmission is not to be used for piracy. User banned.
globalrebel
Posts: 7
Joined: Wed May 16, 2012 2:07 pm

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by globalrebel »

I feel horrible for asking this . . Can anyone show me (outside of this thread) how to create this script in Transmission and how to execute it.

I just purchased the DS212J and setup the Transmission program and LOVE IT! The ONLY thing that I need this to do is unzip the files after it has finished downloading. I would like it to unzip it to a folder with the same name as the file (within the same folder that holds all the rar's).

I have read the entire thread, and I kind of understand the idea behind this. But, I'm a n00b to this and only need this one thing, before my NAS does everything perfect for me.

(I know everyone here is much better at this and it would be GREATLY appreciated!! I would probably even do a how-to after the fact for anyone else who may come upon this! PLEASE help.)
megatrond
Posts: 1
Joined: Thu May 24, 2012 10:11 am

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by megatrond »

Will this work on OSX, calling it from the regular Transmission GUI upon torrent completion?
killemov
Posts: 535
Joined: Sat Jul 31, 2010 5:04 pm

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by killemov »

megatrond wrote:Will this work on OSX, calling it from the regular Transmission GUI upon torrent completion?
Please read the start post again, especially the dependencies section. It will not work on OSX as-is.
Khabel
Posts: 5
Joined: Wed Jul 04, 2012 10:00 pm

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by Khabel »

Can you confirm a couple things for me please

Will there be any issues running this on unraid?
Can I move the keep file to the root dir so all torrents are kept?
Is it possible to use a sleep parameter so torrents are seeded for a certain period before removing?


Thanks and great script
killemov
Posts: 535
Joined: Sat Jul 31, 2010 5:04 pm

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by killemov »

Khabel wrote:Can you confirm a couple things for me please

Will there be any issues running this on unraid?
This is just a script. If all the commands are there then there will be no problems.
Khabel wrote:Can I move the keep file to the root dir so all torrents are kept?
Why not just comment out the torrent remove command instead?
Khabel wrote:Is it possible to use a sleep parameter so torrents are seeded for a certain period before removing?
The default is 1 hour. If you want to make it configurable, you will have to use a file per torrent as there is no way to pass custom parameters yet.
Khabel wrote:Thanks and great script
You're welcome and spread the word or well .. script.
globalrebel
Posts: 7
Joined: Wed May 16, 2012 2:07 pm

Re: Unrar and cleanup script - UPDATE 2011.10.24

Post by globalrebel »

Don't know if this thread is still watched, but I was wondering . . .

The script looks amazing, and I'm looking to implement it myself here if it can do one thing.

Currently I use FlexGet and have it send my torrents to Transmission with a set directory depending on the type of torrent I have.

If TV : /volume1/tranmission/download/Series/{{Series_Name}}/{{Series_Season}}/{{File_name}}

ex. : /volume1/transmission/download/Series/Mamma and Papa/Season 1/Mama.And.Papa.S1.E01

I would like to have the script take the current directory structure and just move it to a different root directory . .

ex. : /volume1/Video/TV/Series/Mamma and Papa/Season 1/Mama.And.Papa.S1.E01

I was thinking (or better yet told) that I might be able to do something like this. . . (See 5'th group down. Add mkdir and cd lines)

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
    mkdir /volume1/$TR_TORRENT_DIR
    cd $/volume1/$TR_TORRENT_DIR
    if [ -d "$SRC_DIR" ]; then
      IFS=$'\n'
      unset RAR_FILES i
      for RAR_FILE in $( find "$SRC_DIR" -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_ID --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_ID --remove-and-delete
        fi
        echo $NOW "Unrarred $TR_TORRENT_NAME" >> $LOG_FILE
      fi
    fi
  fi
} &
While I know this will end up making a very long directory under '/Volume1/Video/' it should allow the program to do what I am hoping for . . . correct?

The output will now be . . .
For TV Shows
/Volume1/Video/transmission/download/TV/{{series_name}}/Series_season/File_name

Sorry, I'm not very good at this, but trying my best.
Post Reply