Assistance with JSON/WebAPI

Suggestions and discussion of future versions
Post Reply
bengalih
Member
Member
Posts: 49
Joined: Fri Feb 14, 2014 3:31 am

Assistance with JSON/WebAPI

Post by bengalih »

I'll preface this with saying I have a lot to learn about using python and JSON API. That being said I was able to cobble together some scripts today that support API calls to both couchpotato and sonarr. But, for the life of me I can't seem to script any proper connections to deluge (1.3.15).

It appears that the docs are in a very disreputable state and I've literally spent 3x as much time on this then the other APIs combined with little to show.
At this point I'm simply trying to make a connection to the API and remove a torrent. This is what my test script looks like:

Code: Select all

import requests

rootca_pem_cert = r'D:\ProgramData\ssl\intechral.ddns.net\RootCA.pem'
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}

r=requests.session()

payload='{"id": 1, "method": "auth.login", "params": ["mypassword"]}'
s = r.post("https://mediabase:8113/json", headers=headers, data=payload, verify=rootca_pem_cert)
print s.headers
print s.content

payload2='{"id": 2, "method": "core.remove_torrent", "params": ["cf6c4817edddb56e06d36637ff241b4cc4be0233", "False"]}'
s = r.post("https://mediabase:8113/json", headers=headers, data=payload2, verify=rootca_pem_cert)
print s.headers
print s.text
This appears to run successfully, I get the following output and the torrent is removed:

Code: Select all

D:\>"d:\Python27\python.exe" test.py
{'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Set-Cookie': '_session_id=05ae4d07b88e90c5374acd24750dfbbb2330; Expires=Sun, 10 Sep 2017 05:25:18 GMT; Path=/json', 'Server': 'TwistedWeb/
16.4.1', 'Date': 'Sun, 10 Sep 2017 04:25:18 GMT', 'Content-Type': 'application/x-json'}
{"id": 1, "result": true, "error": null}
{'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Set-Cookie': '_session_id=05ae4d07b88e90c5374acd24750dfbbb2330; Expires=Sun, 10 Sep 2017 05:25:18 GMT; Path=/json', 'Server': 'TwistedWeb/
16.4.1', 'Date': 'Sun, 10 Sep 2017 04:25:18 GMT', 'Content-Type': 'application/x-json'}
{"id": 2, "result": true, "error": null}
However, if I run the script again - or run it with a torrent_id that is invalid (doesn't exist/already removed/etc). The second post request just seems to hang indefinitely. If I put timeout parameters on the post then it will timeout and kill the script (no error handling yet).

I have no idea what is wrong as the methods and commands, etc that I have in my script are really the result of trial and error looking at dozens of posts here and stack exchange, etc.

I can't believe that the correct behavior is just to hang, but I know my basic syntax is correct since the first run will remove the torrent if there.

assistance please?
Post Reply