Created
October 13, 2023 01:00
-
-
Save jossef/3d116170d68141e43557e7b247286c1f to your computer and use it in GitHub Desktop.
add torrent to deluge rest api python script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import os | |
import sys | |
BASE_URL = 'http://10.0.1.100:49155/' | |
DELUGE_PASSWORD = os.environ.get('DELUGE_PASSWORD', 'deluge') | |
REQUEST_ID = 0 | |
def send_request(session, method, params=None): | |
global REQUEST_ID | |
REQUEST_ID += 1 | |
try: | |
response = session.post( | |
f'{BASE_URL}/json', | |
json={'id': REQUEST_ID, 'method': method, 'params': params or []}) | |
except requests.exceptions.ConnectionError: | |
raise Exception('WebUI seems to be unavailable. Run deluge-web or enable WebUI plugin using other thin client.') | |
data = response.json() | |
error = data.get('error') | |
if error: | |
msg = error['message'] | |
if msg == 'Unknown method': | |
msg += f'. Check WebAPI is enabled ({method}).' | |
raise Exception('API response: %s' % msg) | |
return response, data['result'] | |
def main(): | |
file_path = sys.argv[-1] | |
if not os.path.isfile(file_path): | |
raise Exception(f'Invalid file "{file_path}"') | |
session = requests.session() | |
session.headers['Referer'] = "http://192.168.1.10:49155/" | |
r, data = send_request(session, 'auth.login', [DELUGE_PASSWORD]) | |
session_id = r.cookies['_session_id'] | |
session.headers['Cookie'] = f'_session_id={session_id}' | |
with open(file_path, 'rb') as f: | |
files = {'file': f} | |
r = session.post(f'{BASE_URL}/upload', files=files) | |
r.raise_for_status() | |
data = r.json() | |
uploaded_file_path = data['files'][0] | |
send_request(session, 'web.get_torrent_info', [uploaded_file_path]) | |
send_request(session, 'web.add_torrents', [[{"path": uploaded_file_path, "options": {"file_priorities": [1, 1], "add_paused": False, "compact_allocation": False, "download_location": "/downloads", "move_completed": False, "move_completed_path": "/root/Downloads", "max_connections": -1, "max_download_speed": -1, "max_upload_slots": -1, "max_upload_speed": -1, "prioritize_first_last_pieces": False}}]]) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment