Installing

Deluge is a torrent client written in python. It however has an RPC API so you can interact with it remotely. Using this API you can add, delete and gather status reports on torrents, you can therefore produce a nice looking frontend for it. It is worth pointing out that there is a web interface for it already called deluge-web.

The package on Ubuntu is simply deluge:

sudo apt update
sudo apt install -y deluge

There is a python client to make it easier to interact with the deluge RPC client which we will use.

This can be installed through pip:

pip3 install deluge-client

Basic Code

The following is the minimum code you need to connect to the RPC server:

from deluge_client import DelugeRPCClient

delugeClient = DelugeRPCClient('127.0.0.1', 58846, 'username', 'password')
delugeClient.connect()
if not delugeClient.connected:
    raise Exception("Error connecting to Deluge Daemon")

Running a function

The following doc shows the RPC call: https://deluge.readthedocs.io/en/develop/reference/rpc.html

The following snippet from that doc shows the RPC function call format:

[[request_id, method, [args], {kwargs}], …]

request_id (int)

An integer determined by the client that is used in replies from the server. This is used to ensure the client knows which request the data is in response to. Another alternative would be to respond in the same order the requests come in, but this could cause lag if an earlier request takes longer to process.

method (str)

The name of the remote method to call. This name can be in dotted format to call other objects or plugins methods.

args (list)

The arguments to call the method with.

kwargs (dict)

The keyword arguments to call the method with.

The request_id is taken care of by the plugin therefore all we have to put in is the method the args and the kwargs. A list of methods and their inputs can be found here:

https://deluge.readthedocs.io/en/develop/reference/api.html

We can now get an object of torrents currently in Deluge.

torrent = delugeClient.call('core.get_torrents_status', {}, [])

This returns a python object of each torrent, for each torrent there are a lot of keys but here are some of the more interesting ones:

  • name
  • state
  • progress
  • total_size
  • download_payload_rate
  • upload_payload_rate
  • eta

It is worth noting that the keys in the object that is returned are in binary and not string format. You therefore need to convert them by looping through and converting bytes to str. The below code is an example of this

for key1, value1 in torrent.items():
	torrents=[]
	new_torrent={}
	for key, value in value1.items():
		if isinstance(value, bytes):
			value = str(value).replace("b'", "").replace("'", "").replace('b"', '').replace('"', '')
		new_torrent[str(key).replace("b'", "").replace("'", "")] = value
		torrents.append(new_torrent)