Deluge API JSON / PHP: How to set Config?

Suggestions and discussion of future versions
Post Reply
vlar
New User
New User
Posts: 3
Joined: Tue Dec 11, 2018 9:39 am

Deluge API JSON / PHP: How to set Config?

Post by vlar »

Hello,

I'm trying to find among the documentations an example I can reproduce but there is no real example showing what deluge is waiting as variable for set a configuration variable in PHP.

I would like to throttle the Deluge bandwidth from my home automation system which would take HTTP post type of action.
It can be pretty slick such as Turning on gaming PC -> cap the speed, leaving home -> unleash the speed.

I'm using the Wrappers on Github:

https://github.com/kaysond/deluge-php

I can list the hash of the torrent and I'm connected to the deluge APi.

The wrappers has this function established:

Code: Select all

	public function setConfig($config) {
		return $this->makeRequest("core.set_config", array($config));
	}

And there i'm struggling to pass the parameters I want since I'm not a dev and I can't find example to inspire myself from.

To keep it simple, I'm starting with max_connection_global parameter and I'm stuck with this:

Code: Select all

$max_connections_global="max_connections_global:2000"; 

$speedlimit =  $deluge->setConfig($max_connections_global);
$deluge->close() //Closes the cURL handle
$deluge being the connection to the API.

Code: Select all

$deluge = new deluge("http://IP:8112", "pass");
Any hint would be rather welcome here :)
vlar
New User
New User
Posts: 3
Joined: Tue Dec 11, 2018 9:39 am

Re: Deluge API JSON / PHP: How to set Config?

Post by vlar »

Self answering here.
I gave up on the PHP wrappers which was quite hard to debug for me and my thin dev skills.
It looked good though.

Since, I haven't found on the forum and docs the proper parameter syntax for a set config but on a well hidden page somewhere, i'm posting it here.

1) We authentify ourselves to the Deluge Web Api

Code: Select all

curl -c cookies.txt --compressed -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"method": "auth.login", "params": ["password"], "id": 1}' http://host:8112/json

2) We set the parameters we want to change:

Code: Select all

curl -b cookies.txt --compressed -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"method": "core.set_config", "params": [{"max_connections_global":500}], "id": 2}' http://host:8112/json

Here are are list a similar parameter:

Code: Select all

max_active_downloading
max_active_limit
max_active_seeding
max_connections_global
max_connections_per_second
max_connections_per_torrent
max_download_speed
max_download_speed_per_torrent
max_half_open_connections
max_upload_slots_global
max_upload_slots_per_torrent
max_upload_speed
max_upload_speed_per_torrent
Now, for those who may wonder what you can do with this and since I find it cool, here is the plan.

Based on these settings, I'll be creating different bash files to remotely set a download speed profile according to the current activities.

For example:

if Gaming PC is on -> cap the speed
if watching Youtube -> cap the speed
if i'm working -> 80% of total INet speed
if i'm leaving the house -> 100% speed
if i'm sleeping -> 100% speed.

For the sleeping detection, I'll be picking up the phone loading state from the night stand charging station as a trigger.
spreadred13
New User
New User
Posts: 2
Joined: Mon Sep 30, 2019 11:00 pm

Re: Deluge API JSON / PHP: How to set Config?

Post by spreadred13 »

Probably not relevant to you anymore, since you've found a solution, but at a glance, I would suggest your problem with the PHP wrapper was that you weren't passing a set of key/value pairs (dictionary) to the setConfig function, which then turns whatever you've passed into it, into an array, before finally transforming it to a JSON representation and then using PHP's cURL library to make the request. You were just passing a string: "max_connections_global:2000". If you take a look at how you formatted the same request in cURL:

[{"max_connections_global":500}]

You'll notice that this is, in fact, a JSON representation of an array of key/value pairs, in your case, just one key/value pair, where "max_connections_global" is the key and 500 is the value.

As for your current solution, you should be able to pass all your required config key/value pairs in the body of your cURL request like so, instead of making multiple requests for each config setting:

[{"max_connections_global":500, "max_connections_per_second":100, "max_upload_slots_per_torrent": 10}]
Post Reply