Sorting torrent files on the fly with rTorrent - December 28, 2009

In rTorrent, you can watch certain directories for torrents and load and start them automatically.

schedule = watch_directory,10,10,load_start_verbose=torrents/*.torrent

Now, this is pretty simple to do with pretty much any client, but we can take this even further by performing certain actions on a torrent depending on where its from. rTorrent 0.8.5 added several helpful commands including execute_capture which returns the output (stdout) of the specified command.

I wanted to sort my torrents based on the tracker announce url, so I wrote a ruby script which, when called with the name of a torrent file, returns the host of the url with the TLD stripped.

#!/usr/bin/ruby

require 'rubygems'

require 'bcodec'
require 'uri'

tracker_uri = URI.parse(BCodec.decode(open(ARGV[0], "r"))["announce"])
print tracker_uri.host.split(".")[-2]

One thing to be careful about is to make sure the command you run does not output a newline character, because execute_command will capture this as well.

I wanted rTorrent to set the download directory to the output of this script, so I saved the script to torrent-tracker.rb and put it in my $PATH, then used execute_capture.

schedule = watch_directory,10,10,"load_start_verbose=torrents/*.torrent,
    \"d.set_directory=
        \\\"$execute_capture={torrent-tracker.rb,$d.get_tied_to_file=}\\\"\""

After looking through the raw list of rTorrent commands, I thought I'd see what other neat things I could do. Until now, I hadn't experimented with rTorrent views. These allow you to display different groups of torrents, organized by whatever you want. This seemed ideal for showing torrents from a specific tracker so I thought I'd give it a try.

First, you need to tell rTorrent which views you want to use.

group.insert_persistent_view = tracker1
group.insert_persistent_view = tracker2
group.insert_persistent_view = tracker3

You can now automatically set torrents visible on certain views by adding an extra command to your directory watch schedule.

schedule = watch_directory,10,10,"load_start_verbose=torrents/*.torrent,
    \"d.set_directory=
        \\\"$execute_capture={torrent-tracker.rb,$d.get_tied_to_file=}\\\"\",
    \"d.set_custom=
        tracker,
        \\\"$execute_capture={torrent-tracker.rb,$d.get_tied_to_file=}\\\"\""

You can show certain views in rTorrent by running the command (Ctrl-X in rTorrent) ui.current_view.set. For example, if you wanted to only show torrents from tracker1, run ui.current_view.set=tracker1.

I also decided it was worth having a custom variable associated with each torrent whose value was the tracker the torrent was using. This too can be accomplished using new commands from rTorrent 0.8.5, d.set_custom and d.get_custom.

This can also be set automatically in the schedule command.

schedule = watch_directory,10,10,"load_start_verbose=torrents/*.torrent,
    \"d.set_directory=
        \\\"$execute_capture={torrent-tracker.rb,$d.get_tied_to_file=}\\\"\",
    \"d.set_custom=
        tracker,
        \\\"$execute_capture={torrent-tracker.rb,$d.get_tied_to_file=}\\\"\",
    \"view.set_visible=
        \\\"$execute_capture={torrent-tracker.rb,$d.get_tied_to_file=}\\\"\""

Now, the tracker field can be extracted from any torrent by running the command print=$d.get_custom=tracker. This custom variable can probably simplify things a bit in the schedule command, but I am fine with things the way they are now.

This opens the door to many other interesting ideas including sorting torrents by identifying the content through the file name (movie, software, tv, etc). I might experiment with this later on.


comments

Valid XHTML 1.0 StrictValid CSS!
Generated on Thu, 12 Dec 2013 20:28:11 UTC