Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- # qbitLabeller.py - Modified to use tags instead of categories
- # Based on original by /u/rj_d2 with thanks (https://www.reddit.com/r/sbtech/comments/1ams0hn/q4d_updated/l9nkq4y/)
- # Edit this tool to reflect your qBittorrent settings (host, port, user, pw)
- # Changes to Q4Dconfig.sh
- # LABELLING=true
- # readonly _LABEL_TOOL='~/.Q4D/qbitLabeller.py ${Event[$HASH_INDEX]} ${Event[$LABEL_INDEX]}'
- import sys
- import os
- import logging
- from datetime import datetime
- # Dependency: pip install qbittorrent-api
- import qbittorrentapi
- # Set up logging
- log_file = "/tmp/qbit_labeller_debug.log"
- logging.basicConfig(
- filename=log_file,
- level=logging.DEBUG,
- format='%(asctime)s - %(levelname)s - %(message)s'
- )
- # Make log file accessible
- try:
- os.chmod(log_file, 0o644)
- except Exception as e:
- logging.error(f"Failed to set permissions on log file: {e}")
- # Configuration variables for qBittorrent WebUI
- host = '127.0.0.1' # Replace with your qBittorrent host URL
- port = 8080 # Replace with your qBittorrent WebUI port
- username = 'admin' # Replace with your qBittorrent username
- password = 'password123' # Replace with your qBittorrent password
- logging.info("========== qbitLabeller.py started ==========")
- # Log command line arguments
- if len(sys.argv) < 3:
- logging.error(f"Not enough arguments. Received: {sys.argv}")
- print(f"Error: Need 2 arguments (hash and label), got {len(sys.argv)-1}")
- sys.exit(1)
- # Command-line arguments
- torrent_hash = sys.argv[1]
- label = sys.argv[2]
- logging.info(f"Args received - hash: {torrent_hash}, label: {label}")
- print(f"Managing tags for {torrent_hash} with status '{label}'")
- # Instantiate a Client using the appropriate WebUI configuration
- qbt_client = qbittorrentapi.Client(
- host=host,
- port=port,
- username=username,
- password=password,
- VERIFY_WEBUI_CERTIFICATE=False # Add this if using HTTPS with self-signed cert
- )
- try:
- # Authenticate to the qBittorrent WebUI
- logging.info("Attempting to authenticate to qBitTorrent WebUI")
- qbt_client.auth_log_in()
- logging.info("Successfully authenticated")
- # Check if the hash exists in qBitTorrent
- logging.info(f"Checking if hash {torrent_hash} exists")
- torrent_info = qbt_client.torrents_info(hashes=[torrent_hash])
- if not torrent_info:
- logging.error(f"Torrent with hash {torrent_hash} not found")
- print(f"Error: Torrent with hash {torrent_hash} not found")
- sys.exit(2)
- logging.info(f"Found torrent: {torrent_info[0].name}")
- # Get existing tags on the torrent
- existing_tags = torrent_info[0].tags.split(', ') if torrent_info[0].tags else []
- logging.info(f"Current tags on torrent: {existing_tags}")
- # Get all available tags
- available_tags = qbt_client.torrent_tags.tags
- logging.info(f"Available tags in qBittorrent: {available_tags}")
- # Process based on the label status
- if label in ["QUEUED", "NOT_QD"]:
- logging.info(f"Processing initial status tag: {label}")
- # Ensure tag exists
- if label not in available_tags:
- logging.info(f"Creating new tag: {label}")
- qbt_client.torrent_tags.create_tags(tags=label)
- logging.info(f"Tag '{label}' created successfully")
- # Add the status tag
- logging.info(f"Adding '{label}' tag to torrent {torrent_hash}")
- qbt_client.torrent_tags.add_tags(tags=label, torrent_hashes=torrent_hash)
- logging.info(f"Added '{label}' tag to torrent {torrent_hash}")
- elif label in ["DONE", "NOPE"]:
- logging.info(f"Processing completion status tag: {label}")
- # Remove QUEUED tag if it exists
- if "QUEUED" in existing_tags:
- logging.info(f"Removing 'QUEUED' tag from torrent {torrent_hash}")
- qbt_client.torrent_tags.remove_tags(tags="QUEUED", torrent_hashes=torrent_hash)
- logging.info(f"Removed 'QUEUED' tag from torrent {torrent_hash}")
- # Ensure completion tag exists
- if label not in available_tags:
- logging.info(f"Creating new tag: {label}")
- qbt_client.torrent_tags.create_tags(tags=label)
- logging.info(f"Tag '{label}' created successfully")
- # Add the completion tag
- logging.info(f"Adding '{label}' tag to torrent {torrent_hash}")
- qbt_client.torrent_tags.add_tags(tags=label, torrent_hashes=torrent_hash)
- logging.info(f"Added '{label}' tag to torrent {torrent_hash}")
- else:
- logging.info(f"Processing custom tag: {label}")
- # Ensure tag exists
- if label not in available_tags:
- logging.info(f"Creating new tag: {label}")
- qbt_client.torrent_tags.create_tags(tags=label)
- logging.info(f"Tag '{label}' created successfully")
- # Add the custom tag
- logging.info(f"Adding '{label}' tag to torrent {torrent_hash}")
- qbt_client.torrent_tags.add_tags(tags=label, torrent_hashes=torrent_hash)
- logging.info(f"Added '{label}' tag to torrent {torrent_hash}")
- # Verify the tags were added correctly
- updated_info = qbt_client.torrents_info(hashes=[torrent_hash])
- updated_tags = updated_info[0].tags.split(', ') if updated_info[0].tags else []
- logging.info(f"Final tags on torrent after operation: {updated_tags}")
- print(f"Successfully managed tags for torrent {torrent_hash}")
- logging.info(f"Tag operation completed successfully for torrent {torrent_hash}")
- sys.exit(0)
- except qbittorrentapi.LoginFailed as e:
- error_msg = f"Failed to authenticate: {e}"
- logging.error(error_msg)
- print(error_msg)
- sys.exit(3)
- except Exception as e:
- error_msg = f"An error occurred: {e}"
- logging.error(error_msg)
- print(error_msg)
- logging.exception("Detailed error traceback:")
- sys.exit(4)
Add Comment
Please, Sign In to add comment