sábado, agosto 20, 2016

Transmission torrent scripts

Unrar and cleanup script - UPDATE 2013.05.24 - Transmission



transmission-torrent scripts
https://github.com/martintamare/transmission-scripts



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.
___________________________________________________


http://vortexbox.org/forum/vortexbox/help/4727-transmission-move-completed-downloads


Code:
nano /var/lib/transmission/.config/transmission/settings.json
Add/edit these lines:

Code:
"script-torrent-done-enabled": true, 
    "script-torrent-done-filename": "/var/lib/transmission/.config/transmission/transmission-complete.sh",
Save and write via Ctrl+x.

As you can see, the torrent-done script here will be located at /var/lib/transmission/.config/transmission/transmission-complete.sh. You can put it anywhere you want, just make sure the eventual location of the script and the second line agree.

So let's write your torrent-done script; assuming the above, do:

Code:
nano /var/lib/transmission/.config/transmission/transmission-complete.sh
And add all of this to the file:

Code:
#!/usr/bin/env bash
# Transmission script to move files to post-processing
# and/or specified directories.
# AUTHOR: divreg <https://github.com/divreg>

#################################################################################
# These are inherited from Transmission.                                        #
# Do not declare these. Just use as needed.                                     #
#                                                                               #
# TR_APP_VERSION                                                                #
# TR_TIME_LOCALTIME                                                             #
# TR_TORRENT_DIR                                                                #
# TR_TORRENT_HASH                                                               #
# TR_TORRENT_ID                                                                 #
# TR_TORRENT_NAME                                                               #
#                                                                               #
#################################################################################


#################################################################################
#                                    CONSTANTS                                  #
#                         configure directories and filetypes                   #
#################################################################################


# Use recursive hardlinks (cp -al) only if both Transmission's seed dir and 
# the final dir belong to the same filesystem.  Set to false to make a 
# duplicate copy. Note: true allows you to seed and copy without using up 
# twice the storage.
HARDLINKS=true

# The file for logging events from this script
LOGFILE="/var/lib/transmission/.config/transmission/transmission-complete.log"

# Listening directories
MUSIC_DIR="/storage/music/flac"

# Transmission remote login details. Leave user:pass blank if no authentication
TR_HOST="0.0.0.0"

# Music extensions
MUSIC_EXTS[0]="flac"
#MUSIC_EXTS[1]="mp3"

# Path to new content from transmission
TR_DOWNLOADS="$TR_TORRENT_DIR/$TR_TORRENT_NAME"


#################################################################################
#                                 SCRIPT CONTROL                                #
#                               edit with caution                               #
#################################################################################


function edate 
{
  echo "`date '+%Y-%m-%d %H:%M:%S'`    $1" >> "$LOGFILE"
}

function trans_check
{
  for directory in $(find "$TR_DOWNLOADS" -type d)
  do
    cd "$TR_DOWNLOADS" > /dev/null 2>&1
    cd $directory > /dev/null 2>&1
    files=$(ls *.${MUSIC_EXTS[*]} 2> /dev/null | wc -l)
    if [ $files != "0" ] 
    then
      echo "$files"
      continue
    fi
  done
}

edate "Directory is $TR_TORRENT_DIR"
edate "Torrent ID is $TR_TORRENT_ID"
edate "Torrent Hash is $TR_TORRENT_HASH"
edate "Working on the new download $TR_DOWNLOADS"


# Move new music dir and files to the listening location
# Passes through if none of your extension types are found in
# the new music dir
if [ "$(trans_check ${MUSIC_EXTS[*]})" ]
then
  edate "File $TR_TORRENT_NAME contains audio files!"
  if [ $HARDLINKS == true ] 
  then 
    edate "Hardlinking file contents to listening directory. Success!"
    cp -al "$TR_DOWNLOADS" "$MUSIC_DIR" >> "$LOGFILE"
  fi
  if [ $HARDLINKS == false ]
  then 
    edate "Duplicating file contents to listening directory. Success!"
    cp -R "$TR_DOWNLOADS" "$MUSIC_DIR" >> "$LOGFILE" 
  fi
fi
Write it Ctrl+x.

This will log the events in /var/lib/transmission/.config/transmission/transmission-complete.log. Check it if things aren't working.

You can also choose multiple formats, like flac, mp3, m4a, etc. By whitelisting extensions, we avoid copying movies/TV into your music directory.

Finally, start Transmission:

Code:
systemctl start transmission-daemon.service

_______________________________________
https://community.wd.com/t/guide-auto-removal-of-downloads-from-transmission-2-82/93156
This script will allow the auto removal of downloaded/completed torrents from transmission bittorrent daemon on my cloud. Note, I did not originally write the script, but I've debugged it and fixed a couple of things to make it work in my installation.
 Script Name:  transmission-purge-completed.sh


#this one RBF uses and works as cron job









#!/bin/sh
# the folder to move completed downloads to
# port, username, password
SERVER="9091 --auth transmission:transmission"
# use transmission-remote to get torrent list from transmission-remote list
# use sed to delete first / last line of output, and remove leading spaces
# use cut to get first field from each line
TORRENTLIST=`transmission-remote $SERVER --list | sed -e '1d;$d;s/^ *//' | cut --only-delimited --delimiter=" " --fields=1`
HOME_DIR="/opt/Torrents/downloads"
DEST_DIR="/mnt/seagate/Downloads"
#transmission-remote $SERVER --list 
echo $TORRENTLIST
# for each torrent in the list
if [ ! -d "$DEST_DIR" ]; then
   #mount nas
 SEAGATE_ERROR_CODECODE=`/bin/sh mount-seagate-0.sh`
fi
for TORRENTID in $TORRENTLIST
do
    #echo Processing : $TORRENTID 
    # check if torrent download is completed
    DL_COMPLETED=`transmission-remote $SERVER --torrent $TORRENTID --info | grep "Percent Done: 100%"`
#    echo $DL_COMPLETED
    # check torrents current state is
    STATE_STOPPED=`transmission-remote $SERVER --torrent $TORRENTID --info | grep "State: Seeding\|Stopped\|Finished\|Idle"`
    _NAME=`transmission-remote $SERVER --torrent $TORRENTID --info | grep "Name:" |cut --delimiter=":" --fields=2`
    NAME="$(echo -e "${_NAME}" | sed -e 's/^[[:space:]]*//')"
#    echo $STATE_STOPPED
#    echo $NAME
#transmission-remote $SERVER --torrent $TORRENTID --info
    # if the torrent is "Stopped", "Finished", or "Idle after downloading 100%"
    if [ -n "$DL_COMPLETED" ] && [ -n "$STATE_STOPPED" ]; then
        # move the files and remove the torrent from Transmission
        mv -v $HOME_DIR/"$NAME" $DEST_DIR
        echo "Torrent #$TORRENTID|$NAME is completed"
        #echo "Removing torrent from list"
        #rbf
        transmission-remote $SERVER --torrent $TORRENTID --remove
    else
        temp="Torrent #$TORRENTID is not completed. Ignoring."
    fi
done
of note in the above script is this : 
SERVER="9091 --auth transmission:transmission"
This sets the username password and port your transmission daemon is running on. Change this to match your server.
* login via ssh into you my cloud
* stop transmission daemon 
/etc/init.d/transmission-daemon stop
Once changed are made,
place the script in location on your my cloud.
e.g   "/DataVolume/shares/scripts/transmission-purge-completed.sh"
it could even in   /root if you wanted 
make sure the script has the executable permission
chmod +x /DataVolume/shares/scripts/transmission-purge-completed.sh
then edit the transmission json settings file
nano /var/lib/transmission-daemon/info/settings.json
change these 2 lines 
"script-torrent-done-enabled": true,
"script-torrent-done-filename": "/DataVolume/shares/scripts/transmission-purge-completed.sh",
where  "script-torrent-done-filename" matchs the path to the above script
* start transmission daemon 
/etc/init.d/transmission-daemon start
and test via downloading a (small) torrent file.

Nenhum comentário: