[7. June 2013] Youporn available now | [19. April 2013] A-400 Advanced Network Setup | [11 Jan. 2013] Customize your A-400 Home Screen

Firmware: A-400 [13 May 2013] | C-300 [30 Nov. 2012] | A-300 [30 Nov. 2012] | C-200 RC1 [13 June 2013] | A-200/A-210 RC1 [13 June 2013]

Just got your NMT | WIKI has the answers | Search the forum | Forum Rules/Policy | Firmware & Official NMT News | Popcornhour manuals



User(s) browsing this thread: 1 Guest(s)
Post Reply 
[script] C200 Transmission automatic unrar script
03-07-2011, 08:06 PM (This post was last modified: 03-07-2011 08:21 PM by xray.)
Post: #1
[script] C200 Transmission automatic unrar script
I've ported the original Popcorn A110 automatic unrar script for C200.

Prerequisite:

1. Install gawk via i-pkg! (with the awk provided by busybox it will not work!)

Installation:

1. Save the following script to your NMT C200 somewhere into the Transmission directory. For example save as:

/share/Apps/Transmission/unrar.sh

2. Set execute permission on it via telnet or SSH.

Code:
chmod 755  /share/Apps/Transmission/unrar.sh

3. Stop Transmission.

4. Append the following two lines to your Transmission configuration (/share/.transmission/settings.json):

Code:
"script-torrent-done-enabled": true,
"script-torrent-done-filename": "/share/Apps/Transmission/unrar.sh $TR_TORRENT_DIR/$TR_TORRENT_NAME"

5. Start Transmission and enjoy automatic unrar ;-)

Code:
#!/bin/sh
#------------------------------------------------------------------------------
# NAME:
#     unrar.sh
# DESCRIPTION:
#     will unrar finished torrents collected from transmission used on
#     popcornhour
# COMMENTS:
#     The original A110 source code updated and rebuilt for NTM C200
#     tested on Linux PCH-C200 2.6.22.19-27-4 firmware version:
#     02-04-101206-21-POP-408-000
#     and NMT Apps version:
#     02-04-101104-21-POP-408
#
# HISTORY:
#     Rewritten for NTM C200 02/01/2011 by xray (based on the original script by NMT forum member: nickel)
#
#------------------------------------------------------------------------------
# log function to easily change from stdout to tmpfile
log() {
    # added time infomation for later calculations or future script expansion
    echo -e "[`date | awk '{print $4}'`] $@" >>$TMPFILE
}
# setup logfile for script output ($$ is PID of script)
TMPFILE=/tmp/unrar.log

log "[INFO] Script starting.."

# setup unrar and pidof exectuable location variables
UNRAR_EXE=/nmt/apps/bin/unrar
PIDOF_EXE=/bin/pidof

# global variable to delete logfile if the run was succesful
# false keeps logfile, true removes logfile on safe exit
CLEANUP_LOGFILE=false

# function to move log file to torrent directory and
# end script
end_script() {

    log "[INFO] Script ending..."

    # moves log file to torrent directory and exits, however if we encounter an error trying to get inside the
    # the torrent directory, the logfile will move to the root of the .transmission folder for later review
    mv $TMPFILE .

    # parameter 1 is the exit code that enters this compound-command function, lets use it, if defined
    if [[ -z $1 ]] # if we have no exit code then its a safe bet we cleared all errors!
    then
        # if we made it here then we were pretty darn lucky things worked
        # perform cleanup of file
        if [[ $CLEANUP_LOGFILE == "true" ]]
        then
            log "[INFO] removing file.."
            rm -rf $TMPFILE # remove temp log file
        fi
        exit 0 # a safe exit
    else
        exit $1 # now just stop! We need to understand why the process failed
    fi
}

# function to check if unrar is running already
# if running returns true otherwise false
is_unrar_running() {
    # check to make sure unrar is NOT running this allows the NMT to stay cool
    # by not processing many torrents with its limited block buffer, memory,
    # and CPU horsepower --we dont want to kill our entertainment toy!
    UNRAR_PID=$($PIDOF_EXE unrar)

    if [[ -z $UNRAR_PID ]] # if the process is not present, then pidof returns null
    then
        log "[INFO] unrar NOT running, yay!"
        return 1 # false not running
    else
        log "[WARNING] unrar is already running with PID $UNRAR_PID"
        return 0 # true is running
    fi
}

# function to unrar the file that was found from the completed torrent
# if unpacks succeeds returns true otherwise false
unpack_file() {
    # check to make sure we can get a handle on the incoming rar file
    if [[ -f $1 ]] && [[ -r $1 ]]
    then
        log "[INFO] file $1 available for reading and extracting"
    else
        log "[ERROR] couldnt get a lock on rar file $1 for extracing"
        end_script 1 # exit with error
    fi

    # check to see if unrar is present and executable by the caller of the script
    if [[ -f $UNRAR_EXE ]] && [[ -x $UNRAR_EXE ]]
    then
        # unrar is present and exectuable
        log "[INFO] $UNRAR_EXE application found and verified executable"
    else
        log "[ERROR] $UNRAR_EXE application either not found or not executable"
        end_script 1 # exit with error
    fi

    # check to see if pidof is present and executable by the caller of the script
    if [[ -f $PIDOF_EXE  ]] && [[ -x $PIDOF_EXE ]]
    then
        # pidof is present and exectuable
        log "[INFO] $PIDOF_EXE application found and verified executable"
    else
        log "[ERROR] $PIDOF_EXE application either not found or not executable"
        end_script 1 # exit with error
    fi
    # check to make sure UNRAR is NOT running by calling a function to trap the return code
    while true
    do # do while loop here until pid is destroyed
        ISRUN=` is_unrar_running ; echo $?`
        if (( $ISRUN == 0 ))
        then #if unrar is already running
            log "[WARNING] unrar is already running  sleeping 15 seconds..."
            sleep 15 # 15 seconds, is this enough time? .. probably so
        else
            log "[INFO] unrar is NOT running, lets hurry to unrar file"
            break
        fi
        # go around the loop again until we get really dizzy
    done

    # if we made it here its time to start the unrar process using some legacy code already present
    # however we change the extraction directory to the name of the archive this is safer just in
    # case we hit multiple rar files with similar inside packings
    echo $UNRAR_EXE x -y -p- -o+ "$1" ${ARCHIVE_FILE%\.rar}/
    $UNRAR_EXE x -y -p- -o+ "$1" ${ARCHIVE_FILE%\.rar}/ >> $TMPFILE 2>&1
    if [[ "${?}" -ne "0" ]] # check return code to ensure the app didnt fail us
    then # return code exited something other than zero, why?
        log "[ERROR] $UNRAR_EXE application failed with $? errorcode"
        return 1
    else
        log "[INFO] $UNRAR_EXE application succeeded"
        return 0 # return true
    fi
}

# sanity check to ensure the script is passed at least 1 argurment to begin processing
if [[ $# -lt 1 ]] # parameters needs to be greater than 1
then
    # exit script with error since parameters did not equal greater than 1
    log "[ERROR] Try passing the directory name where the rar file is!"
    end_script 1 # exit with error
else
    # we only care about the 1st parameter where the torrent data resides
    # Param2 is left here for historical reasons and maybe future expansion

    # Recall that...
    # Param $1 -> Path to file
    # Param $2 -> Filename of first file in torrent

    # lets do a sanity check on the Param1 to ensure
    # its a directory, is readable and is writeable by the caller of the script
    if [[ -d $1 ]] && [[ -r $1 ]] && [[ -w $1 ]]
    then
        log "[INFO] $1 passed all sanity tests"
        # param1 passes all sanity checks lets set the variable and continue
        PATH_TO_FILE="$1" # quote it up to preserve spaces, etc
    else
        # param1 failed 1 or more sanity tests show error and exit
        log "[ERROR] $1 failed 1 or more sanity tests"
        end_script 1 # exit with error
    fi
fi

# change directories into the PATH_TO_FILE location
cd "$PATH_TO_FILE"

# get the file count for all .rar file extensions in the given directory PATH_TO_FILE
# if no rar files are found surpress the No such file or directory error to the bit bucket
# store in a count variable
#RAR_COUNT=$(ls *.rar 2>/dev/null | awk 'END{print NR}')
RAR_COUNT=$(find . -name '*.rar' 2>/dev/null | wc -l 2>/dev/null)
log "[INFO] found $RAR_COUNT rar files."
# case select for the various counts that can come from the total rar count command above
# start searching for the rar files,  for each rar file found populate
# the ARCHIVE_FILE variable and begin the unrar process on that file
# ** sorry zips are not supported at this time **
case $RAR_COUNT in
    0)
        log "[INFO] No rar files detected, aborting!"
        end_script
        ;;
    1)
        # prior to RAR version 3.0, segments of RAR file sets had names ending with .rar, .r00, .r01, .r02, etc.
        log "[INFO] version 2.0 rar archive detected."

        # grab and populate the single rar filename for unpacking
        ##ARCHIVE_FILE=$(ls *.rar)
        ARCHIVE_FILE=$(find . -name '*.rar' 2>/dev/null)
        log "[INFO] found archive file: $ARCHIVE_FILE"
        # unpack the file!
        if unpack_file "$ARCHIVE_FILE"
        then
            chmod -R a+rw .
            log "[INFO] everything went a-ok for file $ARCHIVE_FILE"
        else
            log "[ERROR] it did not go so well, we hit an error on $ARCHIVE_FILE.."
            end_script 1 # exit with error
        fi
        ;;
    *)
        # beginning with RAR version 3.0, a new naming convention was introduced that adds two extensions to the file name
        # the first indicates the part number, and the second is always ".rar". A typical file posting will have segments with
        # names such as file.part01.rar, file.part02.rar, file.part03.rar, etc.
        log "[INFO] possible version 3.0 rar achive detected.."

        # lets do some sanity on this to ensure we really have 1 single archive split across many parts and not
        # many small files rar files that need unpacking, if we do no worries this script will extract everything!

        # first we need to output all rar files the system knows in this directory to a text file, this
        # will save an excessive I/O calls to the ls function for each part and helps us to resort the list later
        #for ARCHIVE_FILE in $(ls -X *.rar) #sort alphabetically by entry extension
        for ARCHIVE_FILE in $(find . -name '*.rar' 2>/dev/null)
        do
            log "[INFO] found archive file: $ARCHIVE_FILE"

            # grab the rar volume number by issung a unrar list on the archive file and then chomping the heck out of the response
            # so we can populate this variable, however since we will be running the unrar against multiple files we need to make sure
            # we dont collide with another ending torrent that will try to compete to use unrar as this script does, so lets call
            # the handy function from before
            while true # check to make sure UNRAR is NOT running by calling a function to trap the return code
            do # do while loop here until pid is destroyed
                is_unrar_running
                ISRUN=`echo $?`
                if (( $ISRUN == 0 ))
                then #if unrar is already running
                    log "[WARNING] unrar is already running sleeping 15 seconds..."
                    sleep 15 # 15 seconds, is this enough time? .. probably so
                else
                    log "[INFO] unrar is NOT running, firing info request.."
                    RAR_VOLUME=$("$UNRAR_EXE" l "$ARCHIVE_FILE" | awk '{y=x "\n" $0; x=$0};END{print y}' | grep -v ^$ | awk '{print $6}')
                    if [[ "${?}" -ne "0" ]] # check return code to ensure  our command didnt fail us
                    then # return code exited something other than zero, why?
                        log "[ERROR] $UNRAR_EXE application failed with $? errorcode"
                        end_script 1
                    else
                        log "[INFO] $UNRAR_EXE application succeeded"
                        log "[INFO] $ARCHIVE_FILE volume number is: $RAR_VOLUME"

                        # the meat and potatos of the script
                        if [[ "$RAR_VOLUME" -eq "1" ]]
                        then # process the RAR volume 1
                            log "[INFO] RAR volume $RAR_VOLUME found in $ARCHIVE_FILE located in path $PATH_TO_FILE"
                            if unpack_file "$ARCHIVE_FILE"
                            then
                                chmod -R a+rw .
                                log "[INFO] everything went a-ok for file $ARCHIVE_FILE"
                            else
                                log "[ERROR] it did not go so well, we hit an error on $ARCHIVE_FILE.."
                                end_script 1 # exit with error
                            fi
                        else
                            log "[WARNING] Found RAR file with undefined volume: $ARCHIVE_FILE"
                        fi
                    fi
                    break #exit the inner loop
                fi
                # go around these loops make me really dizzy
            done
        done
        ;;
esac
# good-bye
Find all posts by this user
Add Thank You Quote this message in a reply
[+] 2 users say Thank You to xray for this post
03-08-2011, 10:15 AM (This post was last modified: 03-08-2011 10:21 AM by Roman_V_M.)
Post: #2
RE: [script] C200 Transmission automatic unrar script
I've investigated this some time ago: http://networkedmediatank.com/showthread.php?tid=44564
Actually, there's no need for such elaborate script to unrar downloaded files. The string
Code:
/nmt/apps/bin/unrar x -y -p- -o+ "$TR_TORRENT_DIR/$TR_TORRENT_NAME/*.rar" "$TR_TORRENT_DIR/$TR_TORRENT_NAME/"
works perfectly.
For 100 series you should use:
Code:
/mnt/syb8634/bin/unrar x -y -p- -o+ "$TR_TORRENT_DIR/$TR_TORRENT_NAME/*.rar" "$TR_TORRENT_DIR/$TR_TORRENT_NAME/"

Also it's necessary to note that this will work with Transmission 2.XX. Previous versions don't support custom scripts.
Find all posts by this user
Add Thank You Quote this message in a reply
12-10-2011, 04:09 AM (This post was last modified: 12-10-2011 04:13 AM by ericalism.)
Post: #3
RE: [script] C200 Transmission automatic unrar script
But the string can't detect if there is "unrar" running at that moment,which is an important part of this script, I think
Find all posts by this user
Add Thank You Quote this message in a reply
12-10-2011, 10:54 AM
Post: #4
RE: [script] C200 Transmission automatic unrar script
Mine is better, LOL
http://networkedmediatank.com/showthread.php?tid=56777
Find all posts by this user
Add Thank You Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
Lightbulb Torrentexpander: automatic renamer | build .nfo | download poster & fanart | NMJ Jannyy 193 41,074 Yesterday 08:25 PM
Last Post: supraru
  Transmission 2.21 needed!! dreamland 0 85 06-17-2013 09:57 PM
Last Post: dreamland
  (A400) Transmission 2.42 Wishmaster 2 179 06-14-2013 09:29 PM
Last Post: Wishmaster
  Transmission encryption rocsemail 2 194 06-12-2013 04:40 PM
Last Post: rocsemail
  Automatic RSS Torrent downloader Sulkin 16 806 06-08-2013 10:36 PM
Last Post: jhmiller
  [ Transmission CSI ] v2.71-dath1 (21 October 2012) DathRarhek 432 108,229 06-08-2013 07:57 PM
Last Post: chai73
  Bit torrent Sync on A200 / C200 rvdet 0 306 05-21-2013 08:12 PM
Last Post: rvdet
  How to change default transmission UI ????????? fareign 2 531 05-11-2013 12:03 AM
Last Post: fareign
  Transmission. How to change download paths to other HDD connected to A300 ? djhifi 0 500 05-07-2013 01:05 AM
Last Post: djhifi
  Torrent expander/ unrar vus 9 1,133 04-26-2013 09:57 PM
Last Post: NaaN

Forum Jump: