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
"""
DelugeEasyAdd

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
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()

PRIO_NORMAL = 1
PRIO_MAX = 5

torrentURL = sys.argv[1]
# btjunkie URLs use 'download.torrent' as the filename, get a better one
if re.search("btjunkie", torrentURL):
    filename = re.findall("torrent\/(.*?)\/", torrentURL)[0]
else:
    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
    #client.core.resume_torrent()
    disconnect()

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

def disconnect():
    print "disconnecting"
    client.disconnect()
    reactor.stop()

# We add the callback to the Deferred object we got from connect()
d.addCallback(on_connect_success)

# 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)
d.addErrback(on_connect_fail)

# Run the twisted main loop to make everything go
reactor.run()

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 leave one →
  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)

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS