How to Automate Torrents Using the uTorrentClient API Automation turns manual file management into a seamless, hands-off background process. By leveraging the uTorrent Client API (often utilizing the Web-UI framework), you can programmatically add, pause, remove, and monitor torrent downloads. This guide will walk you through setting up the API, authenticating your requests, and executing core automation scripts. Setting Up the Environment
Before writing code, you must enable the Web API within your uTorrent desktop application. Open uTorrent and navigate to Options > Preferences. Select the Advanced tab and click on Web UI. Check the box to Enable Web UI. Set a unique Username and Password.
Restrict access to a specific port (default is usually 8080) for security. Authentication and the Token System
uTorrent uses a token-based authentication system to prevent Cross-Site Request Forgery (CSRF). Every session requires you to fetch a unique token using HTTP Basic Authentication before you can send operational commands. Fetching the Token
You must send a GET request to the token endpoint. Here is how to handle authentication and token retrieval using Python:
import requests from requests.auth import HTTPBasicAuth BASE_URL = “http://localhost:8080/gui/” USERNAME = “your_username” PASSWORD = “your_password” auth = HTTPBasicAuth(USERNAME, PASSWORD) # Step 1: Request the token token_url = f”{BASE_URL}token.html” response = requests.get(token_url, auth=auth) # Step 2: Extract token text from the HTML response import re token = re.search(r”
”, response.text).group(1) print(f”Successfully authenticated. Token: {token}“) Use code with caution. Core API Operations
Once you have the token, you can pass it as a query parameter in subsequent HTTP requests. Always include your session cookies to maintain the authenticated state. 1. Adding a Torrent via URL or Magnet Link
To automatically add a download, send the torrent file URL or magnet URI to the action endpoint.
# Define parameters for adding a torrent params = { ‘token’: token, ‘action’: ‘add-url’, ’s’: ‘MAGNET_LINK_OR_TORRENT_URL_HERE’ } # Execute the request add_response = requests.get(BASE_URL, auth=auth, params=params, cookies=response.cookies) if add_response.status_code == 200: print(“Torrent added to queue successfully.”) Use code with caution. 2. Monitoring Active Downloads
Automation requires visibility. You can fetch a list of all current torrents, their download speeds, and completion percentages.
params = { ‘token’: token, ‘list’: ‘1’ } list_response = requests.get(BASE_URL, auth=auth, params=params, cookies=response.cookies) torrent_data = list_response.json() # Loop through and print torrent properties for torrent in torrent_data[‘torrents’]: hash_id = torrent[0] name = torrent[2] progress = torrent[4] / 10 # Represented in per-mille (tenths of a percent) print(f”Name: {name} | Progress: {progress}% | Hash: {hash_id}“) Use code with caution. 3. Managing Torrent States (Pause/Resume/Remove)
Control your bandwidth dynamically by stopping or starting specific transfers using their unique info-hash identifier.
# To pause a torrent params = { ‘token’: token, ‘action’: ‘pause’, ‘hash’: ‘TORRENT_HASH_ID’ } requests.get(BASE_URL, auth=auth, params=params, cookies=response.cookies) Use code with caution. To Resume: Change the action parameter value to ‘start’.
To Delete: Change the action parameter value to ‘remove’ (removes from list) or ‘removedata’ (deletes downloaded files from disk). Best Practices for Automation
Error Handling: Implement retry logic for network timeouts or application restarts.
Token Caching: Reuse your token and cookie session until they expire to avoid overloading the API with authentication requests.
Rate Limiting: Pause your loops for a few seconds between status checks to minimize CPU usage.
If you want to tailor this automation system to your specific setup, tell me: What programming language do you prefer to use?
What is your primary trigger for downloading? (RSS feeds, watch folders, or webhook events?)
Do you need to move or rename files automatically after the download completes? AI responses may include mistakes. Learn more
Leave a Reply