Created
May 23, 2026 21:19
-
-
Save asaf400/281bee6fc4cc1e2d832a614f210c0422 to your computer and use it in GitHub Desktop.
A python that handles magnet urls and adds the torrents to a qBittorrent Web UI API instance.
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 sys | |
| # --- Configuration --- | |
| # Replace with your qBittorrent Web UI URL, username, and a function to get your password | |
| QBITTORRENT_URL = "http://tower:8080" | |
| USERNAME = "cjohnson" | |
| PASSWORD = "tier3" # The cake is a lie. 🍰 | |
| def add_magnets(magnets): | |
| """ | |
| Logs into qBittorrent and adds magnet links with sequential download | |
| and first/last piece priority enabled. | |
| Args: | |
| magnets (list): A list of magnet URI strings. | |
| """ | |
| if not isinstance(magnets, list): | |
| print("Error: Input must be a list of magnet links.") | |
| return | |
| urls = "\n".join(magnets) # The API supports multiple URLs separated by a newline | |
| with requests.Session() as s: | |
| # 1. Login to the qBittorrent Web UI | |
| try: | |
| login_response = s.post( | |
| f"{QBITTORRENT_URL}/api/v2/auth/login", | |
| data={"username": USERNAME, "password": PASSWORD} | |
| ) | |
| login_response.raise_for_status() # Raise an exception for bad status codes | |
| except requests.exceptions.RequestException as e: | |
| print(f"Error connecting to qBittorrent for login: {e}") | |
| return | |
| # 3. Add the magnet link(s) with the specified options | |
| try: | |
| r = s.post( | |
| f"{QBITTORRENT_URL}/api/v2/torrents/add", | |
| data={ | |
| "urls": urls, | |
| "sequentialDownload": "true", | |
| "firstLastPiecePrio": "true" | |
| } | |
| ) | |
| r.raise_for_status() | |
| if r.text == "Ok.": | |
| print(f"Successfully added {len(magnets)} magnet(s) to the download queue.") | |
| else: | |
| # This case might occur if the request is accepted but there's a non-200 OK message | |
| print(f"Request sent, but failed to add magnets. Server responded: {r.text}") | |
| except requests.exceptions.RequestException as e: | |
| print(f"Error adding magnets to qBittorrent: {e}") | |
| # --- Example Usage --- | |
| if __name__ == '__main__': | |
| # A list of magnet links to add. | |
| # Replace these with actual magnet links you want to download. | |
| #example_magnets = [ | |
| # "magnet:?xt=urn:btih:e334ab2ab222c39d2b27cf1727776100c4c4784a&dn=ubuntu-24.04-desktop-amd64.iso" | |
| # You can add more magnet links to the list | |
| # "magnet:?xt=urn:btih:..." | |
| #] | |
| print("Attempting to add magnets...") | |
| add_magnets(sys.argv[1:]) | |
| print("\nScript finished.") |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For windows handler installation, you can use: