Recently I’ve been throwing together some Perl code to help manage and run query’s on my rtorrent installation.
I’ll try to document the steps here in a series of blog posts. This post is for my own use, and may be lacking in clarity in some places.
NOTE: These scripts and code snippets are not a “complete project”. They are probably not what your looking for. If you do happen to find pieces here and there useful. That’s awesome! Have fun!
To get my Perl scripts talking to rTorrent/ruTorrent XML-RPC communication is needed. To accomplished this the Perl module XML::RPC is used. It provides simple “Pure Perl” methods for XML::RPC communication.
Assuming Perl and cpan are setup.
This will install the
Data::Dumper perl modules into your environment. The
Data::Dumper module is used for testing.
A bit of information is needed to connect to your rtorrent installation.
- The User you login to ruTorrent with. :
- The Password you login to ruTorrent with:
- The Host url your installation is running on:
- The Port, if your site is running
httpsand it probably is. This port will be
443as https communicates over that port.
- The Endpoint:
RPC2in my case. This may change depending on how rtorrent and ruTorrent are setup. – To setup your endpoint you can look at RPC-Setup-XMLRPC from the makers of rtorrent.
The script is provided with these bits of information at runtime, to avoid storing anything hardcoded into the scripts. The command to run the script looks like this.
Assuming the script is called
Inside the script these command line switches are “caught” with the variable
$ARGV. Keep in mind it’s zero indexed. ex.
$ARGV will be
The script will look like this.
From here each variable can be used in a script. (Technically each $ARGV variable can be used as well. I’ve written it out here for clarity.) I have included an alternative to clean up and minimize code clutter.
The string that is used to generate a useable “URL” is:
- Alternative url, after tweaking and calling
To call the
XML::RPC library make sure it’s listed at the top of the script.
use XML::RPC; and call it with.
- Alternative complete call is:
Now that we can communicate with rtorrent. We need to know what say, and what to ask it.
Here we build on the wonderful work done by the github user
mdevaev on the project
emonoda. - Which they picked up from gi-torrent
They have done a wonderful job documenting and compiling it into a nice format for me to easily read.
I find it here: rtorrent-xmlrpc-reference
The call that I am interested in at the moment is.
Passed to the
XML::RPC library the call looks like this.
From here the script will connect to the server and get the hashes of all the torrents.
Now I have chosen to setup a for each loop to loop over all the results returned from the call and get the name of the torrent associated with it.
That code looks like this.
The call used is
d.get_name from: rtorrent-xmlrpc-reference
This loops over each torrent hash and returns the name of it.
From here. It takes quite awhile to run this program, there is a lot of communication going on. The next step is to find a way to store that information locally. So it can be used in further scripting - filtering. And other such routines, etc.
The data structure that I will be using is an Associative Array.
The final snip of code is:
The next big step is finding a way to store that data locally, this is done through the use of a local database.
There is a very nice little article over at the Linux Journal which talks about Creating and Using a Database with Perl.
The snip of code used to open and create a database is as simple as (mud?) here it is:
Make sure to close the database and write everything at the end.
The interesting point to note here is the
%hash_name variable in the foreach loop is writing these results to a memory location.
Instead one can write them directly to file, using
%hash_name_db instead of
The full snip of code is:
- There’s a few print statements thrown in just for clarity on what’s going on.
All that’s left is to call the database, sort it and print it out.
That can be done with this snip of code:
Don’t forget to close the file afterwards with:
The full functional script with no frills.:
Now comes the hardest part. Deciding on a name for this Perl module to include it in cpan.
The name that I settled on is:
Rtmgr::Gen::Db as to if that’s a “good name” I’m not sure.
The reasons why I chose it were.
- Its namespace that’s not already used.
rtstands for rT(orrent), and
mgrstands for Manager. As this is a type of manager for rTorrent.
Genpart of the namespace looks generating Database files.
All these pieces might change up as I work on the functionality of the module. This is a starting point.
With my namespace in hand I need to generate a skeleton for my Perl module.
Should build up a skeleton module nicely!
Also its a good idea to put the source code under version control I will start tracking the module in git with these commands:
lib/Rtmgr/Gen/ folder of the project. There will be a Db.pm file created.
This is where I will be placing my code.
- The latest source code of this module can be viewed on GitHub
It should be fairly self documenting.
This amazing article has information on building and publishing a module on cpan. You can find it here.
When its time to publish to cpan.
- The source docs for git commands and tags is here
- The source docs for uploading to cpan is found here
- You will need CPAN::Uploader so run
sudo cpan install CPAN::Uploader
As of March 20, 2020 : There is a version of this up on CPAN.
cpan install Rtmgr
- The repository is tracked and maintained here: Rtmgr it has all the latest changes.
- Release tags on GitHub correspond to releases pushed up to CPAN.
- As I use and develop the module, I will continue to update the GitHub and CPAN Versions.
- The module might undergo a complete rewrite again. I continue to find and fix things - and add new feature.