Further automation for torrents (bash/factory)

Hey Folks,

As many of you probably know, there isn’t a lot of API-driven support for torrent clients yet, so I choose to go the more “manual” route.

Essentially, the way I use torrents is simple. I use qBittorrent (-nox, on ubuntu), and at times I’ll go to the private trackers I have access to and snag TV packs for shows my friends or family want me to carry. I also use RSS to get new releases for the times where a show might find its way to a tracker before it finds its way to usenet.

The only difference is that I script the final steps of the process. Since i’m forced to use Torrent-blackhole, I wrote a script to handle post-processing.

This is how it works: When a torrent finishes downloading and winds up in my “seed” directory, I run the below post-processing script against it. The script then determines if it is a regular file (no directory) or a directory (for either packs or because someone insisted it go into a directory).

Then it checks for case. In the case of .rar files (either single or multiple), the script will unrar them to a “staging” directory. In the case of static files that aren’t RARs, they simply get copied to the staging directory.

Once complete, the entire contents of the staging directory gets mv’d to the factory folder, which Sonarr (and in my case, also Couchpotato) picks up and moves to its final destination.

The script is here in case formatting below gets ganked: http://www.weckstrom.com/postproc.txt

Also, if you’re interested in automating subtitle downloads from opensubtitles, I have a script to do that as well. It leverages the command line for Filebot and is fairly simple. I have it running every 12 hours. http://www.weckstrom.com/update-subtitles.txt

#!/bin/bash

#NOTE: STG, VTGT and MTGT should all reside on the same volume as files are staged prior to moving to the factory folder.
#
#TOR - if using qBittorrent, you pass the name of the post-completion command. I chose to put mine in /usr/sbin as the
#      file 'torrent-pp' (pp meaning 'post-processing'). To configure this in qBittorrent, enable the "Run an external 
#      external program on torrent completion" option and add the following command line:
#      /usr/sbin/torrent-pp "%n"  
#	
#	Obviously you could alter this script to do post processing on any torrent client, but I leave that to you.
#
#VTGT - Target directory for video files. Place on same filesystem as STG.
#
#MTGT - Target directory for audio (music) files. Place on same filesystem as STG.
#
#STG - Staging directory. Files are copied or unrar'd to here. Once completed, they are mv'd to the factory directory. 
#      Doing it this way will prevent any issues with Sonarr trying to relocate files across file system boundaries,
#      usually rearing its ugly head as an incomplete file that's only been partially copied/moved. 

TOR=$1
VTGT=/tmp/factory
MTGT=/cage/3/downloads/music-complete
SEED=/cage/3/downloads/torrent-seed
STG=/tmp/staging

# You probably won't need to edit below this line - though you could if you wish to add additional filetypes to
# move to the factory folder. I configure the factory folder to work in both Sonarr and Couchpotato. This will result
# in the generation of some '.unknown.ignore' files since you're essentially mixing both movie and TV content. I just
# nuke *.unknown.ignore files from time to time.  

logger -t bittorrent-pp "Found $SEED/$TOR"
if [ -d "$SEED/$TOR" ]; then
cd "$SEED/$TOR"
 logger -t bittorrent-pp "`find . -name "*.mkv" -exec cp -v "{}" "$STG/" \;`"
 logger -t bittorrent-pp "`find . -name "*.flv" -exec cp -v "{}" "$STG/" \;`" 
 logger -t bittorrent-pp "`find . -name "*.mp4" -exec cp -v "{}" "$STG/" \;`"
 logger -t bittorrent-pp "`find . -name "*.m4v" -exec cp -v "{}" "$STG/" \;`" 
 logger -t bittorrent-pp "`find . -name "*.mpg" -exec cp -v "{}" "$STG/" \;`" 
 logger -t bittorrent-pp "`find . -name "*.mpeg" -exec cp -v "{}" "$STG/" \;`" 
 logger -t bittorrent-pp "`find . -name "*.avi" -exec cp -v "{}" "$STG/" \;`"
 logger -t bittorrent-pp "`find . -name "*.ts" -exec cp -v "{}" "$STG/" \;`"
 logger -t bittorrent-pp "`rm -v $STG/*sample* $STG/*.rar $STG/*.idx $STG/*.sfv`" \;
 logger -t bittorrent-pp "`mv -v $STG/*.mkv $STG/*.flv $STG/*.mp4 $STG/*.m4v $STG/*.mpg $STG/*.mpeg $STG/*.avi $STG/*.ts "$VTGT/"`"
        
 logger -t bittorrent-pp "`find . -name "*.mp3" -exec cp -v "{}" "$STG/" \;`"
 logger -t bittorrent-pp "`find . -name "*.m4a" -exec cp -v "{}" "$STG/" \;`"
 logger -t bittorrent-pp "`find . -name "*.flac" -exec cp -v "{}" "$STG/" \;`"	
 logger -t bittorrent-pp "`find . -name "*.dsr" -exec cp -v "{}" "$STG/" \;`"
 logger -t bittorrent-pp "`find . -name "*.wav" -exec cp -v "{}" "$STG/" \;`"
 logger -t bittorrent-pp "`find . -name "*.ogg" -exec cp -v "{}" "$STG/" \;`"
 logger -t bittorrent-pp "`find . -name "*.ape" -exec cp -v "{}" "$STG/" \;`"
 logger -t bittorrent-pp "`mv -v $STG/*.mp3 $STG/*.m4a $STG/*.flac $STG/*.dsr $STG/*.wav $STG/*.ogg $STG/*.ape "$MTGT/"`"

#
# I put in a sleep loop to wait for existing unrar jobs to complete before continuing. I do this so there's less disk
# contention and fighting over CPU cycles, which concurrent unrar jobs will obviously cause - particularly when crossing
# filesystem borders. 
#
 while [ ! -z "$(ps aux | grep unrar | grep -v defunct | grep -v grep | awk '{print $2}')" ]
  do
    echo "Active unrar job running - waiting 2 minutes."
    logger -t bittorrent-pp "Active unrar job running - waiting 2 minutes."
    sleep 120
 done

 logger -t bittorrent-pp "Processing rar archives..."
 logger -t bittorrent-pp "`find . -name "*.rar" -exec echo "Will unrar RAR file: {}" \;`"
 logger -t bittorrent-pp "`find . -name "*.rar" -exec unrar e -y -p- -o- -inul -x'*.rar' -x'*.idx' -x'*.nfo' -x'*sample*' "{}" "$STG/" \;`"
 logger -t bittorrent-pp "Completed unrar process"

 cd "$STG/"
 logger -t bittorrent-pp "`find . -name "*.mkv" -exec mv -v "{}" "$VTGT/" \;`"
 logger -t bittorrent-pp "`find . -name "*.flv" -exec mv -v "{}" "$VTGT/" \;`"
 logger -t bittorrent-pp "`find . -name "*.mp4" -exec mv -v "{}" "$VTGT/" \;`"
 logger -t bittorrent-pp "`find . -name "*.m4v" -exec mv -v "{}" "$VTGT/" \;`"	
 logger -t bittorrent-pp "`find . -name "*.mpg" -exec mv -v "{}" "$VTGT/" \;`"
 logger -t bittorrent-pp "`find . -name "*.avi" -exec mv -v "{}" "$VTGT/" \;`"
 logger -t bittorrent-pp "`find . -name "*.mpeg" -exec mv -v "{}" "$VTGT/" \;`"	
 logger -t bittorrent-pp "`find . -name "*.ts" -exec mv -v "{}" "$VTGT/" \;`"
       	
 logger -t bittorrent-pp "`find . -name "*.mp3" -exec mv -v "{}" "$MTGT/" \;`"
 logger -t bittorrent-pp "`find . -name "*.m4a" -exec mv -v "{}" "$MTGT/" \;`"
 logger -t bittorrent-pp "`find . -name "*.flac" -exec mv -v "{}" "$MTGT/" \;`"
 logger -t bittorrent-pp "`find . -name "*.dsr" -exec mv -v "{}" "$MTGT/" \;`"  
 logger -t bittorrent-pp "`find . -name "*.wav" -exec mv -v "{}" "$MTGT/" \;`"
 logger -t bittorrent-pp "`find . -name "*.ogg" -exec mv -v "{}" "$MTGT/" \;`"
 logger -t bittorrent-pp "`find . -name "*.ape" -exec mv -v "{}" "$MTGT/" \;`"
fi

if [ -f "$SEED/$TOR" ]; then
 cd "$SEED"
 logger -t bittorrent-pp $TOR appears to be a regular file
 case "$TOR" in
 *.mkv|*.m4v|*.mpg|*.mpeg|*.flv|*.avi|*.mp4|*.ts)
 logger -t bittorrent-pp "`cp -v "$TOR" "$VTGT/"`"
 ;;
 *.mp3|*.m4a|*.flac|*.dsr|*.wav|*.ogg|*.ape)
 logger -t bittorrent-pp "`cp -v "$TOR" "$MTGT/"`"
 ;;
 *.rar)
 logger -t bittorrent-pp "`unrar e -y -p- -o- -inul -x'*.rar' -x'*.idx' -x'*.nfo' -x'*sample*' "$TOR" "$STG/"`"
 logger -t bittorrent-pp "`mv -v *.mp3 *.m4a *.flac *dsr *.wav *.ogg *.ape "$MTGT/"`"
 logger -t bittorrent-pp "`mv -v *.mkv *.flv *.mp4 *.m4v *.mpg *.avi *.mpeg *.ts "$VTGT/"`"
 ;;
 esac
fi