HOWTO: Have rtorrent download files automatically and tweet you once a download finishes using TTYtter

Requirements:
rtorrent—available through opkg
leech—.ipk available through bitbucket
TTYtter—available from floodgap

Once all three have been installed, you're ready.

First step: enable the rtorrent watchfolder

The rtorrent watchfolder is an option in the .rtorrent.rc file

schedule = watch_directory,5,5,load_start=/path/to/your/watchfolder/*.torrent

Uncomment that line and enter the location you want it to look for torrents. For ease of general use, this should be a folder you have access to so you can manually save torrents there to trigger downloads, in addition to the automatic ones. It's a very bad idea to keep it on the router flash, as it will fill up very quickly.

Second step: use leech to populate the watchfolder

Now, following the leech notes (see bitbucket link above), you run it like so:

CONFIG_DIR="/etc/leech" DOWNLOADS_DIR="/path/to/your/watchfolder" /usr/sbin/leech

/etc/leech is where the configuration files reside, and DOWNLOADS_DIR is obviously your watchfolder, where .torrent files will be downloaded.

It's very straightforward from here.

/etc/leech/foods is the plaintext file that holds the urls to your rss feeds. It will check all of them every time the script is run.

/etc/leech/wild-downloads is the plaintext file where you put your wildcards, one to each line.

legal tv show 1080p x264

The above will match torrents that have ALL wildcards, in that order; in other words, legal blondes tv and cable show 1080p x264 is a match.

Various other settings are available in the plaintext file located at /etc/leech/default

Step three: automation

For automation, chuck the following line into your crontab using vi, by writing crontab -e:

*/10 * * * * CONFIG_DIR=/etc/leech DOWNLOADS_DIR=/path/to/your/watchfolder /usr/sbin/leech

This will run leech every 10 minutes. You don't want it to check every minute, as it might be considered hammering the server of the torrent tracker.

Congratulations, you now have automatic torrent downloads.

Step Four: setting up TTYtter

This is trickier. Out of the box, TTYtter, simple as it is, doesn't work. It will never allow you to connect to the twitter API. The reason is that curl is unable to authenticate the SSL certificate the API—which is now HTTPS-only—is offering.

To get around this, we will need to do two things:

1. Download a certificate bundle (the 250kb .pem from curl.haxx.se is what I use). I converted it to a .crt, but at this stage I can't recall what that entailed. You can figure it out—in any case, you might not even have to!
2. Edit the source code of TTYtter to add a custom option to the curl authentication call.

Copy the certificate bundle file to your router.

Using a better text editor than vim, and preferably on your own computer, open the TTYtter file. It contains around 8,000 lines of abstruse perl. Don't fret!

Line 883-886 look like the following:

    $simple_agent = "$baseagent -s -m 20";

    @wend = ('-s', '-m', '20', '-A', "TTYtter/$TTYtter_VERSION",
            '-H', 'Expect:');

Change it to the following:

    $simple_agent = "$baseagent -s -m 20 --cacert /path/to/your/ca.bundle";

    @wend = ('-s', '-m', '20', '--cacert', '/path/to/your/ca.bundle', '-A', "TTYtter/$TTYtter_VERSION",
            '-H', 'Expect:');

Copy the TTYtter to where you want it on the router, and do a chmod +x filename to make it executable.

With that change, curl will finally play nice with TTYtter, and we are good to go. Run TTYtter (with the -seven option, as utf-8  seems unsupported) and follow the instructions.

You have to be logged into twitter in whatever browser you use to authenticate the token TTYtter gives you to connect it to that account.

Step five: Tweet on completed downloads

After step four, this will seem like child's play.

First of all, you will need to create a bash script that will call TTYtter:

In whatever folder you are working in (say /etc/automation) create a file called tweet.sh by typing touch tweet.sh. Fill the file with the following:

#!/bin/sh
/etc/automation/ttytter -silent -ssl -curl -seven -status="@yourtwittername Download Completed: $*"

And do a chmod +x tweet.sh to make it executable.

In .rtorrent.rc, add the following line (on one line):

system.method.set_key = event.download.finished,mycustomcommand,"execute=/etc/automation/tweet.sh,$d.get_name="

In this line, mycustomcommand can be whatever you want it to be. $* appends everything rtorrent sends to tweet.sh.

And there you have it.

(Last edited by flippant on 6 Feb 2014, 07:54)