From b5dca02805c829ac1e14499dd89e7804b7d5d139 Mon Sep 17 00:00:00 2001 From: Eugene Amos Date: Thu, 30 Nov 2023 00:37:30 -0800 Subject: [PATCH] qbittorrent > updates to assign_category.py - Changed 'sys.argv' to handle input better - Updated logging for the category's - Implemented better error handing if torrent is not a hi-def movie or show - confirm that the category's are assigned. Added better --- qbittorrent/assign_category.py | 68 +++++++++++++++++------- qbittorrent/assign_category_01.py | 88 +++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 19 deletions(-) create mode 100644 qbittorrent/assign_category_01.py diff --git a/qbittorrent/assign_category.py b/qbittorrent/assign_category.py index c4a69d8..665af94 100644 --- a/qbittorrent/assign_category.py +++ b/qbittorrent/assign_category.py @@ -13,49 +13,79 @@ # import re import sys +import logging import qbittorrentapi +# Configure logging +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') + +# Checking for correct number of command-line arguments +if len(sys.argv) != 3: + logging.error('[ACAT] - Incorrect number of arguments. Exiting script.') + sys.exit(1) + +# Extract torrent name and hash from command line arguments +torrent_name = sys.argv[1] +torrent_hash = sys.argv[2] + +logging.warning('[ACAT] - -------------') +logging.warning('[ACAT] - -------------') +logging.warning('[ACAT] - *** STARTING ASSIGN CATEGORY SCRIPT ***') +logging.warning('[ACAT] - -------------') +logging.warning(f'[ACAT] - raw torrent name: {torrent_name}') +logging.warning(f'[ACAT] - raw torrent hash: {torrent_hash}') +logging.warning('[ACAT] - -------------') + # qbittorrent webui login crendentials. qbitt_username = '' qbitt_password = '' # qbittorrent webui address. -#url = 'http://192.168.1.55:8080' +#url = 'http://192.168.1.55:8124' +#url = 'http://localhost:8124' url = 'http://:' # The categories to use for matching torrents. Change as needed. category_1080_shows = "1080+ | Shows" category_1080_movies = "1080+ | Movies" -# Connect to the qBittorrent API -qb = qbittorrentapi.Client(host=url) - -# Log in qbittorrent API -qb.auth_log_in(username=qbitt_username, password=qbitt_password) - -# Get information about all torrents in the download queue -torrents = qb.torrents_info() - -# loop through the torrents -for torrent in torrents: - - # Get the name of the completed torrent from the command line arguments inside qbittorrent - torrent_name = sys.argv[1] # Name of the added torrent as defined by "%N" in qbittorrent settings. - torrent_hash = sys.argv[2] # Hash of the added torrent as defined by "%I" in qbittorrent settings. +try: + # Connect to the qBittorrent API + qb = qbittorrentapi.Client(host=url) + qb.auth_log_in(username=qbitt_username, password=qbitt_password) # Define the regular expression pattern for 'SxxExx' or 'sxxexx' format. pattern = r"[Ss]\d{2}[Ee]\d{2}" # Check for 'SxxExx' or 'sxxexx' episode pattern and '1080p' or '2160p' - # If so, assign 1080 show category. if re.search(pattern, torrent_name) and ("1080p" in torrent_name or "2160p" in torrent_name): qb.torrents_set_category(category=category_1080_shows, hashes=torrent_hash) qb.torrents_set_auto_management(hashes=torrent_hash, enable=True) + logging.warning(f'[ACAT] - Torrent "{torrent_name}" category set to: "{category_1080_shows}".') + logging.warning('[ACAT] - -------------') # Then check for movies criteria. elif ("1080p" in torrent_name or "2160p" in torrent_name) and "YTS.MX" in torrent_name: qb.torrents_set_category(category=category_1080_movies, hashes=torrent_hash) qb.torrents_set_auto_management(hashes=torrent_hash, enable=True) + logging.warning(f'[ACAT] - Torrent "{torrent_name}" category set to: "{category_1080_movies}".') + logging.warning('[ACAT] - -------------') + + # Torrent is not a Hi-def movie or show + else: + logging.warning(f'[ACAT] - No category change for Torrent "{torrent_name}".') + logging.warning('[ACAT] - -------------') + + # Disconnect from the qBittorrent API + qb.auth_log_out() + +except Exception as e: + logging.error(f'[ACAT] - Error occurred: {e}') + logging.warning('[ACAT] - -------------') + +logging.warning('[ACAT] - *** ENDING ASSIGN CATEGORY SCRIPT ***') +logging.warning('[ACAT] - -------------') +logging.warning('[ACAT] - -------------') + + -# Disconnect from the qBittorrent API -qb.auth_log_out() diff --git a/qbittorrent/assign_category_01.py b/qbittorrent/assign_category_01.py new file mode 100644 index 0000000..bb3046c --- /dev/null +++ b/qbittorrent/assign_category_01.py @@ -0,0 +1,88 @@ +#!/usr/bin/python3 +# +# qBittorrent-api: +# https://qbittorrent-api.readthedocs.io/en/v2023.4.47/introduction.html +# +# +# +# Make sure to set the folling variables. +# +# +# +# +# +import re +import sys +import logging +import qbittorrentapi + +# Configure logging +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') + +logging.warning('[ACAT] - -------------') +logging.warning('[ACAT] - -------------') +logging.warning('[ACAT] - -------------') +logging.warning('[ACAT] - *** STARTING ASSIGN CATEGORY SCRIPT ***') +logging.warning('[ACAT] - -------------') + +# qbittorrent webui login crendentials. +qbitt_username = '' +qbitt_password = '' + +# qbittorrent webui address. +#url = 'http://192.168.1.55:8080' +url = 'http://:' + +# The categories to use for matching torrents. Change as needed. +category_1080_shows = "1080+ | Shows" +category_1080_movies = "1080+ | Movies" + +# Connect to the qBittorrent API +qb = qbittorrentapi.Client(host=url) + +# Log in qbittorrent API +qb.auth_log_in(username=qbitt_username, password=qbitt_password) + +# Get information about all torrents in the download queue +torrents = qb.torrents_info() + +# loop through the torrents +for torrent in torrents: + + # Get the name of the completed torrent from the command line arguments inside qbittorrent + torrent_name = sys.argv[1] # Name of the added torrent as defined by "%N" in qbittorrent settings. + torrent_hash = sys.argv[2] # Hash of the added torrent as defined by "%I" in qbittorrent settings. + + # Define the regular expression pattern for 'SxxExx' or 'sxxexx' format. + pattern = r"[Ss]\d{2}[Ee]\d{2}" + + # Check for 'SxxExx' or 'sxxexx' episode pattern and '1080p' or '2160p' + # If so, assign 1080 show category. + if re.search(pattern, torrent_name) and ("1080p" in torrent_name or "2160p" in torrent_name): + qb.torrents_set_category(category=category_1080_shows, hashes=torrent_hash) + qb.torrents_set_auto_management(hashes=torrent_hash, enable=True) + + # Then check for movies criteria. + elif ("1080p" in torrent_name or "2160p" in torrent_name) and "YTS.MX" in torrent_name: + qb.torrents_set_category(category=category_1080_movies, hashes=torrent_hash) + qb.torrents_set_auto_management(hashes=torrent_hash, enable=True) + + # Logging and printing the raw torrent name and what category it was changed to + logging.warning(f'[ACAT] - raw torrent name: {torrent_name}') + logging.warning(f'[ACAT] - raw torrent hash: {torrent_hash}') + logging.warning('[ACAT] - -------------') + + logging.warning(f'[ACAT] - Torrent "{torrent_name}" category was changed to: "{category_1080_movies}".') + +logging.warning('[ACAT] - -------------') +logging.warning('[ACAT] - *** ENDING ASSIGN CATEGORY SCRIPT ***') +logging.warning('[ACAT] - -------------') +logging.warning('[ACAT] - -------------') +logging.warning('[ACAT] - -------------') + +# Disconnect from the qBittorrent API +qb.auth_log_out() + + + +