Skip to content

Controlling Deluge Auto-Management and File Priorities via Python

2011 November 15
by Tedb0t

Today I was working away from my home file server which runs the BitTorrent server Deluge.  My Deluge Web UI stopped working for some reason (and it never worked that well in the first place) and I really needed to start a torrent at home.  Deluge has an “Auto Managed” feature that’s enabled by default, but since I seed a lot of torrents, it actually prevents a new torrent from starting to download automatically, so I have to disable that and the resume the torrent after adding it.  Thankfully Deluge has a Python client interface we can use to write our own scripts.

Here’s an example script that adds a URL from an argument with auto_managed turned off:

#!/usr/bin/env python
# encoding: utf-8

Starts a new Deluge torrent from a URL.

Created by Ted Hayes.


import urllib, os, sys, re
# Import the client module
from deluge.ui.client import client
# Import the reactor module from Twisted - this is for our mainloop
from twisted.internet import reactor

# Set up the logger to print out errors
from deluge.log import setupLogger

# Connect to a daemon running on the localhost
# We get a Deferred object from this method and we use this to know if and when
# the connection succeeded or failed.
d = client.connect()


torrentURL = sys.argv[1]
# btjunkie URLs use 'download.torrent' as the filename, get a better one
if"btjunkie", torrentURL):
    filename = re.findall("torrent\/(.*?)\/", torrentURL)[0]
    filename = os.path.basename(torrentURL)

print "Using filename: %s" % filename

def addTorrent():
    client.core.add_torrent_url(torrentURL, '/home/daleth/Downloads/'+filename, {'auto_managed':False}).addCallback(added)

def added(s):
    print "Added status: ",s

# We create a callback function to be called upon a successful connection
def on_connect_success(result):
    print "Connection was successful!"

def disconnect():
    print "disconnecting"

# We add the callback to the Deferred object we got from connect()

# We create another callback function to be called when an error is encountered
def on_connect_fail(result):
    print "Connection failed!"
    print "result:", result

# We add the callback (in this case it's an errback, for error)

# Run the twisted main loop to make everything go

I still have to figure out how to then automatically resume the torrent.

Here are some helpful links:

UIClient Documentation

UIClient Example 




Related Posts:

One Response
  1. Yonatan Naamad permalink
    May 7, 2012

    The line
    client.core.add_torrent_url(torrentURL, ‘/home/daleth/Downloads/’+filename, {‘auto_managed’:False}).addCallback(added)

    is invalid in the latest version of Deluge (1.3). The save path should be passed passed as the ‘download_location’ parameter in the options dictionary. For instance,

        client.core.add_torrent_url(torrentURL, {‘download_location’: ‘/home/daleth/Downloads/’+filename, ‘auto_managed’:False}).addCallback(added)

Comments are closed.