niamu

Brendon Walsh
Automating Home Entertainment

Automating Home Entertainment

2010-01-10

I won't pretend for a moment that my TV entertainment doesn't come from places which many would describe as nefarious. Of course, were I an American resident I could legally watch all of my TV shows on Hulu or other network sites for free. Unfortunately, I live in the distant, incredibly deprived and foreign nation of Canada.

Naturally, I do what many do in my situation; I download the desired TV show via BitTorrent), extract a series of RAR archives that are frustratingly tedious, then meticulously rename the file with my own preferred naming structures and file it away in my archives until the Blu-Ray becomes available.

Tedious to say the least. And up until recently I dutifully took to the task every night one of my beloved programs aired. A few weeks ago however, I got the idea that I could script the entire process to automate it. These are the scripts I've both written and found to mix together a perfect cocktail of BASH, PERL and PYTHON to automate my entertainment system.

Transmission

Transmission is a fantastic, minimalist, cross-platform BitTorrent client with low memory usage. It is also highly scriptable which makes it particularly of use to me as I have added support for both watch directories and a download completion notifier.

Flexget

FlexGet is a multipurpose automation tool for content like torrents, nzbs, podcasts, comics, etc. FlexGet is able to handle different kinds of sources like RSS-feeds, html pages and even csv files, just to name a few.

Having a torrent watch directory is nice, but it's also kind of useless without a way to automatically add torrents to the watch directory. Flexget allows me to do that with a great deal of ease. My own configuration file is available below to show just how easy it is to work with. Flexget makes use of YAML for easy to read and understand syntax.

feeds:
  tv:
    rss: REDACTED
    series:
  • the big bang theory download: ~/.config/transmission-daemon/watch/

tvrenamer.pl

A spectacular little script written by Robert Meerman that analyzes TV show filenames and will rename them according to your own preferences. My preferred options for the tvrenamer PERL script are below.

perl tvrenamer.pl --include_series --pad=2 --gap=" - " --scheme=SXXEYY --postproc="s/(\A)([a-z])/\u\1\l\2/g" --unattended

This string of options will automatically check a folder named according to a TV series such as "Dexter" and will proceed to parse every file within that folder and rename them as such: "Dexter - S04E09 - Hungry Man.mkv"

This is such an incredible time saver for those as meticulous as myself. So now that all the pieces of the puzzle are there, it's just a matter of putting it together.

BASH scripts

So all of the above is great, but it's still not as simple as firing up Boxee and hitting play when the download completes. I still have to grab the file from my remote server, then run the tvrenamer.pl script and file it away in the library folders for Boxee to pick it up.

Git Repository

The first script grabs the files from the remote server upon completion where they are stored in a single file.

IFS='
'

cd ~/Downloads/Torrents/
touch ~/Downloads/.donotcopy

for file in ~/Downloads/Torrents/*
do
    if [[ $file == *.mkv ]]
    then
      grep "$file" ~/Downloads/.donotcopy
        if [ $? = 1 ]
            then
            cp "$file" ~/Downloads/
            echo "$file" >> ~/Downloads/.donotcopy
        fi
    fi
done
sh ~/Dropbox/Personal/Scripts/tvsort.sh

As referenced by the above script, tvsort.sh is shown below and takes care of calling tvrenamer.pl and sorting away the results.

IFS='
'

SOURCE="~/Downloads"
DEST="/Volumes/Destination_Volume"

cd $SOURCE/

regex[1]=$(ls -1 | grep -e "^[Tt]he[.|[:space:]][Bb]ig[.|[:space:]][Bb]ang[.|[:space:]][Tt]heory") #The Big Bang Theory

series[1]="The Big Bang Theory"

for (( c=1; c<=${#series[*]}; c++ ))
do
  cd $SOURCE/

  series_name=$(echo ${series[$c]})

  for i in ${regex[$c]}
  do
    mkdir -p $SOURCE/TV/"$series_name"/
    mv ${i} $SOURCE/TV/"$series_name"/

    s=$(echo ${i} | grep -o "[sS][0-9][0-9][eE]")
    s=$(echo ${s:1:2})
    s=$(echo ${s#0})

    cd $SOURCE/TV/"$series_name"/
    mkdir -p $DEST/TV/"$series_name"/Season\ $s/

    perl ~/Dropbox/Personal/Scripts/tvrenamer.pl --include_series --pad=2 --gap=" - " --scheme=SXXEYY --postproc="s/(\A)([a-z])/\u\1\l\2/g" --season=$s --dubious --unattended
    convertedfiles=$(ls -1 | grep .mkv)
    mv *.mkv $DEST/TV/"$series_name"/Season\ $s/
  done
done

Prowl

Last, but not least, is Prowl. Prowl was initially designed as a Growl plugin for the iPhone push notification system. A way of forwarding event notifiers from a Mac to your iPhone. However, an API now exists for Prowl which makes it far more compelling to write your own notifications for Prowl outside of Growl.

You may have noticed the PERL script prowl.pl within some of my above code snippets. These pieces are the bits of flair that really make me giddy. When all of these scripts are triggered by CRON and begin to due my bidding, Prowl indicates the progress level along the way and finally let's me know when it is ready to watch.

Taking it Further

Pretty cunning, dontcha think? And while this does make my life considerably easier, I'm certainly not done yet. As I stated earlier, I do buy all the Blu-Rays for my TV shows and there is still a considerable amount of automation left to uncover in the ripping of those discs.

I've become obsessed with the idea of automation and you can expect at least one follow up post on the subject. Finally, if any BASH gurus would like to offer further suggestions on optimizing my rather rudimentary scripts, please do advise in the comments.