SABNZBD -> Sickbeard -> Deluge Torrent Upload script.

Come up with a useful post-processing script? Share it here!
Post Reply
witwolf
Newbie
Newbie
Posts: 1
Joined: February 12th, 2011, 7:41 am

SABNZBD -> Sickbeard -> Deluge Torrent Upload script.

Post by witwolf »

Making torrent files and upload them could be a painful task, so I decided to make my own upload script.

I had a few problems before even attempting to write the script. Since I use Ubuntu Server for my downloads and storage I had to find a different pieces of software that would be able to do the job.
This is how my setup works. To download stuff from usenet I use SABNZB-Plus. Download are added from Search Providers and Series automatically added with Sickbeard.

Sickbeard is a wonderfully piece of software which does all the work for you. You add shows, it will then scrape information and artwork from www.thetvdb.com, download episodes as they are released, rename them to a universal naming convention, and put them in their right folders. After you install sickbeard you would never have to sort or rename files ever again.

Sickbeard integrates into SABNZBD by submitting NZB’s to download via the API key you have to put in its configuration. You configure the series category to run a post script on download completion which notifies sickbeard of a download that is completed. Sickbeard will then process the files accordingly. It also has the feature to run 3rd party scripts on completion of its tasks. This gives me the possibility to create an auto torrent upload script.
I had to find a torrent daemon that could run on a headless server, as my server does not have a screen connect or X11 installed. Deluge seems to have been the best choice as it has a separate daemon, client, and webUI which do not have to run on the same computer.

Now that I have the three main pieces of software to do the biggest jobs I needed to find a way of making all of them talk with each other.

To create torrent files I found the utility mktorrent which is a simple but very useful command line program to create torrent files. To upload the torrent file I will use CURL to submit a web form.
The trickiest part of the script was to add torrents to deluge so that they could start seeding the torrent files. Since I need to upload to a TorrentTrader tracker, I needed to be able to add a passkey to the announce url in the torrent so that it would allow to announce the torrent to the tracker. This was not the only problem as I had to find a way to add torrent files easily without much effort to deluge. This was harder than I initially thought.
The easiest way turned out to use flexget, another RSS download manager (like sickbeard), to add the torrent files, remove the announce url and add a new one with my passkey.
Finally I had a combination that worked!

To do the initial install I followed this guide. (http://www.ainer.org/sick-beard-install ... lucid-lynx)

After you have SABNZBD and Sickbeard working it’s time to install Deluge and  Flexget. You can follow this guide. (http://ubuntuforums.org/showthread.php?t=1433783)

Next you need to install curl (apt-get install curl), and then mktorrent (apt-get install mktorrent).

The only way for flexget to read the torrent files is through http. I have tried everything but it refuses to read torrent files from files on the server. So what I had to do is to install apache to host the torrent files so that flexget can download them from http://127.0.0.1/torrents/*. Really silly, but this is the only way I got this to work. I am not going to cover that here, there are many other apache guides on the internet.
Next we had to configure flexget so we can add torrent files via the command line. Here is my config file;

Code: Select all

feeds:
    MyTracker:
    set:
      path: $path
    remove_trackers:
      - .
    add_trackers:
      - http://sometracker.com/announce.php?passkey=714c990d9916a73e964c0a7ba7b0aa3d
    accept_all: yes
    deluge:
      user: deluge
      pass: 'deluge'
Next here is my script to make the torrent, upload it and pass it to deluge;

Code: Select all

#!/bin/bash

#Set the following variables to configure
#Create log file first, otherwise it wont work


#Get cookie file with curl -c sometracker.com -d "username=witwolf&password=witwolf" http://sometracker.com/account-login.php -v


AnnounceURL="http://sometracker.com/announce.php"
UploadURL="http://sometracker.com/torrents-upload.php"
CreatedTorrentsFolder="/home/witwolf/torrents/"
HTTPDownloadLocation="http://127.0.0.1/torrents/"
CookiesFile="/home/witwolf/cookie.txt"
HDTV="41"
SDTV="6"

#Do not change anything below

filename=${1##*/}
filenameWithoutSpaces=`echo "$filename" | sed 's/ //g'`
directory=${1%/*}
echo "Filename : $filename"
echo "Filename Without Spaces: $filenameWithoutSpaces"
FILESIZE=$(stat -c%s "$1")

if [ $FILESIZE -gt 838860800 ] ; then
        CATAGORY=$HDTV
else
        CATAGORY=$SDTV
fi

#Create Torrent

TorrentExtension=".torrent"
Torrent=$CreatedTorrentsFolder$filenameWithoutSpaces$TorrentExtension
mktorrent -a $AnnounceURL -p -o "$Torrent" "$1"
echo "Created torrent"

#Upload Torrent File

curl $UploadURL -b $CookiesFile -m 120 -F takeupload="yes" -F torrent=@"$Torrent" -F nfo="" -F name="$filename" -F image0="" -F image1="" -F type="$CATAGORY" -F lang="0" -F descr="Uploaded by Witwolf BOT" -F press="Upload Torrent" > /dev/null

#Add to Deluge

SSHCommand="flexget --feed=MyTracker --inject \"$filename\" \"$HTTPDownloadLocation$filenameWithoutSpaces$TorrentExtension\" --cli-config \"path=$directory\""

ssh deluge@localhost $SSHCommand
Change all the variables to your needs. As you can see curl requires a cookie file for authentication to upload the torrent. You can save a cookie with this command.

Code: Select all

curl -c sometracker.com -d "username=witwolf&password=witwolf" http://sometracker.com/account-login.php -v
HDTV & SDTV is the category id of different categories in the upload web form.

Next what we need to do is to setup public key authentication for SSH, so that the script can run a command as user deluge without needing a password.

You can read more about it here: http://www.debian-administration.org/articles/530

Basically you create keys for the user the script will be run as. You need to put the public key then in the home directory of deluge (/var/lib/deluge). Create the directory “.ssh” inside the home directory, and then paste the public key of the user inside a file called “authorized_keys”. You should now be able to SSH into the computer as deluge without needing to enter a password.

Test it by “ssh deluge@localhost”. If it does not ask a password then you succeeded.

OK, so to quickly run through the script to explain what it does.

You can pass any file to the script, let say the script name is test.sh and we have a file test.iso. we can then tell the script to process test.iso by this command “/home/witwolf/test.sh /home/witwolf/test.iso”.
First it will determine the filename without the directory, and then the filename without any spaces. mktorrent does not like torrent file names with spaces. The it will determine the directory where the file is stored.
So here is the values of the variables:

$filename=”test.iso”
$filenameWithoutSpaces=”test.iso”
$directory=”/home/witwolf/

I have made the script so it can determine if something is SD or HD by looking at the filesize. If a file is bigger than 800 MB it is considered to be HD and the category ID is set accordingly.
It will now create the torrentfile.

After the torrent file is created it will attempt to upload it with curl to a web form. For now it can’t determine if it failed or succeeded.

Then using SSH it will run flexget as user deluge (if it’s not run as deluge it can’t access the home directory files). Inspecting the command and flexget configuration it will add the file with “inject” under the feed “MyTracker”, and we need to specify under what URL it will be able to download the torrent file from, as well as where to download the torrent data to (so it can seed the file).

We then use the plug-in “remove_trackers” to remove the plain tracker from the torrent file, and then to re-add the tracker with “add_trackers” to add the tracker url with the passkey.

This is quite complicated, but at least it work. If anyone have an easier way of doing it please let me know.
Post Reply