Instant entertainment

Ever wondered if there’s a way to have all the TV-Shows you would ever want directly delivered to your media centre as soon as it’s available online? Over the last years I’ve experimented with several ways of automatic download, sorting and management of my shows, and I think I have found a setup that works according to my needs.

Here’s what I currently have in my mediacenter setup:
- Asrock NetTop ION330
- Western Digital MyBook World Edition II

Configurations:
Western Digital MyBook World edition II:
This box has been configured in the usual way you would set up up according to the manual, however I disabled samba and ftp support, and enabled NFS for speed. Also, I disabled the built in remote management system, and Twonkymedia for speed. Basically, what you want to do is to disable as many services as possible, and leaving the disk as strictly a remote storage disk. I can highly recommend reading up on this site http://mybookworld.wikidot.com/ They have a lot of useful information regarding this box and how to hack it to do just about everything. I did earlier run this box as a seed-box, but unfortunately it doesn’t handle high speeds (100mbit) connections very well on torrents.

Asrock NetTop ION330:

This box has a lot of configurations, both for my media centre functions, and for my personal projects, but here’s the basics needed to have a fully functional, automatic downloading, sorting and renaming media box.

The system itself: XBMC LiveCD – For me, this is by far the best media center known to man. Works flawlessly on the box and runs GNU/Linux (Ubuntu). Download the ISO image from here, burn to a CD and boot up. Follow the installation instructions, and voila!

Now for the nitty gritty configurations and script working in the background and making everything happen.

First of all, you will have to set up FlexGet . I have written a complete guide on how to do this here.

Then, you will have to install an application to start these downloads with, like rTorrent. This is easiest done by running the following command:

user@XBMCLive:~$ sudo apt-get install rtorrent

Then there’s the configuration:
rtorrent.rc

max_peers = 131

max_uploads = 11

upload_rate = 2000

directory = /your/path/to/downloading

session=/your/path/to/sessions

schedule = watch_directory,5,5,load_start=/your/watchfolder/*.torrent
schedule = tied_directory,5,5,start_tied=/your/watchfolder/*.torrent
schedule = untied_directory,5,5,stop_untied=/your/watchfolder/*.torrent

schedule = low_diskspace,5,60,close_low_diskspace=100M

execute_log = /your/logfolder/rtorrent.log

port_range = 55556-55560

port_random = no

check_hash = no

use_udp_trackers = yes

encryption = allow_incoming,enable_retry,prefer_plaintext

dht = auto

dht_port = 6881

peer_exchange = yes

on_finished = seed,"execute=mv,-u,$d.get_base_path=,/your/path/to/seeding/"

schedule = ratio,60,60,"close_on_ratio=200"

on_close = move_fin,"execute=mv,-u,$d.get_base_path=,/your/path/to/completed//"

on_close = remove_fin,"execute=rm,$d.get_tied_to_file="

Here you need to change the following values to suit your needs:

  • upload_rate – Max upload rate for all torrents combined.
  • directory – path to where your downloads will be when the download is running.
  • watch/tied/untied_directory – where you will be placing your .torrent files you want to start.
  • execute_log – Path to the logfile for all executed actions.
  • port_range – Ports you want the torrents to be connectable to. Remember to open these ports on your router/firewall!
  • on_finished – Path to where you want files to be stored when they’re seeding.
  • ratio – At what share ratio you want your torrent to be stopped. Default is 200%.
  • on_close – The directory you want the files to be stored once finished downloading.

Once you have this up and running in a screen, the next is to configure two scripts that run as a cron job every day and rename + sort the downloads once they arrive in the completed folder.

Sorting
We want to sort our downloads into neat folders so our XBMC system understand what kind of shows we have. The most important part for this script to work is the directory structure. It must be set up like this:

/your/path/Show Name/Season X/

And here’s the script that is going to do the job of sorting:
mvfiles.sh

#!/bin/bash

# specify the base directory to move to
fs="/your/path/to/TV"

# specify the base directory to search
base="/your/path/to/completed/"
for file in `find "$base" | replace " " "~" | egrep 'mkv|avi'`; do

        file=`echo $file | replace "~" " "`

        show=`basename "$file" | grep -io "[^\/]*S[0-9]\{1,2\}" | grep -io ".*[\._ -]" | replace "." " " "_" " " "-" " " `
        while [ -n "`echo "$show" | grep \" \{2\}\"`" ]; do
                show=`echo "$show" | replace "  " " "`
        done

        show=`echo "\`expr "$show" : '[ ]*\(.*\)[ *]$'\`"`
        #echo "\"$show\""
        snum=`basename "$file" | grep -io "S[0-9]\{1,2\}" | grep -io "[1-9]\?[1-9]"`
        season="Season $snum"
        #echo $season

        show=`ls "$fs" | grep -io "$show"`
        #echo "DIR: \"$show\""

        dest=$fs
        if [ -d "$fs/$show/$season/" ]; then
                dest="$fs/$show/$season/"
        else
                if [ -d "$fs/$show/Season$snum" ]; then
                        dest="$fs/$show/Season$snum/"
                else
                        if [ -d "$fs/$show/" ]; then
                                dest=""
                        fi
                fi
        fi

        if [ "$dest" != "" ]; then
                #echo "mv -v \"$file\" \"$dest\""
                mv -v "$file" "$dest"
                wait `pidof mv`
        fi

done

# Delete all .nfo, .srt, .txt files
find "$base" -name *.nfo -exec rm {} \;
# find "$base" -name *.srt -exec rm {} \;
find "$base" -name *.txt -exec rm {} \;

# Delete all empty folders (recursively)
find "$base" -depth -mindepth 1 -type d -empty -exec rmdir {} \;

exit 0;

As you can see, it works with simple regexp and Linux commands, and after a couple of weeks testing, it does the job very well. If it doesn’t recognize the name, it leaves the file alone.

Just to note, I did not write this script, unfortunately I do not know who did, but if you’re out there, thanks! I spent several months trying to find a way to do this, and this script saved me from having to study bash and write it from scratch.

Renaming
In order to rename the files to names more fitting the naming conventions of XBMC, I made a very simple bash script that uses a python script called tvnamer. This script checks with TheTVdb.com and renames the file according to what is specified there. Usually this will be “Show name [01x01] Episode name.ext” and makes it very easy for XBMC to recognize the show.
renameTV.sh

#!/bin/bash

/usr/local/bin/tvnamer -b -r /your/path/to/TV > /dev/null 2>&1

Be aware though, that this script goes through the entire TV folder and renames any episode there without interaction. If you don’t want this, you might want to figure out another way to set this up.

And last but not least, how to run these scripts automatically. I simply set up a cron job for each one like this, also, I did add a third line that updates the XBMC library automatically once everything is sorted and renamed, so we really have everything ready when we’re back home.

0 15 * * * sh /your/path/to/scripts/mvfiles.sh >> /var/log/mvfiles.log
0 16 * * * sh /your/path/to/scripts/renameTV.sh
0 17 * * * curl --get "http://localhost:8080/xbmcCmds/xbmcHttp?command=ExecBuiltIn&parameter=XBMC.updatelibrary(video) >> /var/log/xbmc-update.log
"

This runs the first script that moves files at 15:00, and the renaming script at 16:00. Which means when I get home from work at 18:00, everything will be sorted and ready for me. Now be aware, this works great for me, because I live in Scandinavia, which means most shows I’m watching come out in the middle of the night here, and gets downloaded during the early hours of the day. This means that by lunch, the files are already to sort.
This might however be different for you if you live in America.

Hope this guide was useful for you! And please leave feedback if you have any questions.

Updated: Added cron job for updating the XBMC library automatically once everything is sorted and renamed.


Share with others:
  • Facebook
  • Twitter
  • Add to favorites
  • StumbleUpon
  • Google Bookmarks
  • LinkedIn

One Comment

  1. Posted 10/04/2010 at 01:49 | Permalink

    One of my friends already told me about this place and I do not regret that I found this article.

Post a Comment

Your email is never shared.