Skip to content

Instantly share code, notes, and snippets.

@asaf400
Created May 23, 2026 21:19
Show Gist options
  • Select an option

  • Save asaf400/281bee6fc4cc1e2d832a614f210c0422 to your computer and use it in GitHub Desktop.

Select an option

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.
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.")
@asaf400

asaf400 commented May 23, 2026

Copy link
Copy Markdown
Author

For windows handler installation, you can use:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\magnet]
@="Magnet URI"
"URL Protocol"=""
"Content Type"="application/x-magnet"

[HKEY_CLASSES_ROOT\magnet\shell\AddToQbittorrent]
@="Add to qBittorrent"

[HKEY_CLASSES_ROOT\magnet\shell\AddToQbittorrent\command]
@="\"C:\\Python313\\pythonw.exe\" \"C:\\path\\to\\magnet_handler.py\" \"%1\""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment