Automatically Transfer Torrents from Deluge to rTorrent

I’ve been working on a script (due to personal interest and requests from various private tracker threads) that auto-moves torrents from Deluge (better at downloading & uploading simultaneously) to rTorrent (better for long-term seeding). Now that it’s completely “out of beta” so-to-speak, I’m going to share it! I hope someone finds this useful.

Use your favourite text editor to save the script below somewhere. Make sure you chmod +x delugeExport.sh (or whatever you end up calling it).

The script has several dependencies. You need to install/configure them.

Download this other script and put it somewhere. I put it in the same directory as delugeExport.sh. In order for this Perl script to function, it requires the Bencode module: sudo perl -MCPAN -e 'install Convert::Bencode'. Finally, don’t forget to chmod +x rtorrent_fast_resume.pl.

Configure the configurable things in delugeExport.sh. They’re all important so don’t miss one.

Ensure rTorrent is setup as you would like. You basically just need the watch folder setup (schedule = watch_directory,5,5,load_start=/your/directory/*.torrent). You also need to set rTorrent’s download directory to the directory where Deluge sends completed torrents. Any other stuff like ruTorrent can wait if you want.

Now you need to run the script from the command line. Every single *.torrent file that you have loaded in Deluge will be processed by rtorrent_fast_resume.pl and will have fastresume data added to it. This will only happen for 100% complete downloads. Partial, or still-downloading torrents will not be moved across. All your Deluge torrents are now in rTorrent’s watch folder and have been added if rTorrent was running!

Go to Deluge and remove (without data) all your completed torrents.

Go to Preferences –> Plugins. Turn on Execute.

Turning on Execute in Deluge Preferences

• Restart deluged (and deluged-web).

• Go to Preferences –> Execute. Do your version of this (i.e. enter the path to the script!):

Configuring Execute plugin

Click Apply/Okay/etc.

You may optionally configure a cronjob to clear out the *.torrent files out of rTorrent’s watch directory every 24 hours. Simply execute crontab -e and enter the following line: 0 0 * * * rm $HOME/path/to/rtorrent/watch_folder/*.torrent > /dev/null 2>&1

Unfortunately, due to a bug in the Execute plugin (version 1.3.5 and earlier only) concerning Unicode handling, before you start testing out your new configuration you must follow this post of mine here. If not, you’ll find that random torrents stay resting in Deluge, even after they’re completed.

Finally, you’re done! Add a torrent to Deluge and test it!

Also, you can further minimise your time dealing with Deluge by using this UserScript (use it with NinjaKit for Safari or Chrome).

#!/bin/bash
####################################################################
###             Please fill in the variables below!              ###
####################################################################

# Default is "deluge", but you should have changed it...
password=deluge

# Your domain/IP here. For example, google.com or localhost or 127.0.0.1
domain=localhost

# The port you use to access the WebUI (default 8112)
port=8112

# Do you use http or https to access the WebUI (default http)?
scheme=http

# Which folder are you keeping rtorrent_fast_resume.pl in?
# Please include trailing slash.
dir=$HOME/torrents/

# Where do your torrents eventually download to? If they auto-move
# please specify that directory. Please include trailing slash.
download=$HOME/torrents/complete/

# rTorrent's watch folder. Please include trailing slash.
watch=$HOME/torrents/watchr/

# Do you want to backup whatever's in Deluge's state directory before
# you rm it all? (Maybe useful the first time for the mass-transfer
# just to be sure but probably not afterward).
backup=no

# Do not change this unless you have knowingly changed Deluge's
# state folder.
files=$HOME/.config/deluge/state/

###							         ###
# This script is designed to be used alongside the Execute plugin! #
###								 ###

####################################################################
###   Please remember to create the watch folder for rTorrent!   ###
###   Please remember to have rTorrent fully setup and config'd! ###
###   Remember to chmod  x this script and the perl script!	 ###
####################################################################

# Backup if you told it to.
date=`date  "%m-%d-%Y-%H.%M.%S"`
if [ "$backup" = "yes" ]; then tar -cf $HOME/deluge_state.$date.tar $files; fi

# Every torrent, "f", will have fastresume data added and then moved
# to rTorrent's watch directory, $watch, for addition.
# $1,$2 received from Execute plugin. Need specifics when not en masse.
# If block ensures these lines don't run except via Execute plugin.
# Authenticating to deluge-web.
# Remove torrent using $1 var passed from Execute.
if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
	torrentid=$1
        torrentname=$2
        torrentpath=$3
	perl $dir/rtorrent_fast_resume.pl "$torrentpath" < "$files/$torrentid.torrent" > "$watch/$torrentname.$RANDOM.torrent"
	cookie=`curl -vskm 1 -H "Content-Type: application/json" -X POST -d "{\"id\": 1\,\"method\": \"auth.login\",\"params\": [\"$password\"]}" $scheme://$domain:$port/json 2>&1 | grep -i "Set-Cookie:" | cut -d '=' -f2 | cut -d ';' -f1`
	curl -vskm 1 -b _session_id=$cookie -H "Content-Type: application/json" -X POST -d "{\"method\":\"core.remove_torrent\",\"params\":[\"$1\",false],\"id\":2}" $scheme://$domain:$port/json
else
	for f in $files/*.torrent
        do
          name=`basename $f`
          perl $dir/rtorrent_fast_resume.pl $download < $f > $watch/$name
        done
fi

Please leave a comment if you find a bug!

And, yes, if you so desire, you can use this script as a simple way to move from Deluge to rTorrent completely. Simply execute the script once from the command line and everything will be moved over, imported into rTorrent via the watch directory.

3 thoughts on “Automatically Transfer Torrents from Deluge to rTorrent

  1. Looks very nice.

    if i run the script i only get:
    [email protected]:~/scripts$ bash delugeExport.sh
    : command not foundne 5:
    : command not foundne 8:
    : command not foundne 11:
    : command not foundne 14:
    : command not foundne 17:
    : command not foundne 21:
    : command not foundne 25:
    : command not foundne 28:
    : command not foundne 33:
    : command not foundne 37:
    : command not foundne 41:
    : command not foundne 47:
    : command not foundne 51:
    ‘elugeExport.sh: line 67: syntax error near unexpected token `do
    ‘elugeExport.sh: line 67: ` do

    Script is not modified… Only the settings i need…

    cheers and nice work!

    Reply
  2. Hi, thanks for the script. Will this also remove the torrent and data from Deluge when run?

    I ran the script successfully, the data and torrents were moved to rtorrent/data and rtorrent/watch, but Deluge appears unchanged.

    I had “backup=yes” set when I ran the script the first time. Maybe that’s why? Thanks!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>