Guest User

Untitled

a guest
Apr 15th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # qbitLabeller.py - Modified to use tags instead of categories
  3. # Based on original by /u/rj_d2 with thanks (https://www.reddit.com/r/sbtech/comments/1ams0hn/q4d_updated/l9nkq4y/)
  4.  
  5. # Edit this tool to reflect your qBittorrent settings (host, port, user, pw)
  6.  
  7. # Changes to Q4Dconfig.sh
  8. # LABELLING=true
  9. # readonly _LABEL_TOOL='~/.Q4D/qbitLabeller.py ${Event[$HASH_INDEX]} ${Event[$LABEL_INDEX]}'
  10.  
  11. import sys
  12. import os
  13. import logging
  14. from datetime import datetime
  15.  
  16. # Dependency: pip install qbittorrent-api
  17. import qbittorrentapi
  18.  
  19. # Set up logging
  20. log_file = "/tmp/qbit_labeller_debug.log"
  21. logging.basicConfig(
  22. filename=log_file,
  23. level=logging.DEBUG,
  24. format='%(asctime)s - %(levelname)s - %(message)s'
  25. )
  26.  
  27. # Make log file accessible
  28. try:
  29. os.chmod(log_file, 0o644)
  30. except Exception as e:
  31. logging.error(f"Failed to set permissions on log file: {e}")
  32.  
  33. # Configuration variables for qBittorrent WebUI
  34. host = '127.0.0.1' # Replace with your qBittorrent host URL
  35. port = 8080 # Replace with your qBittorrent WebUI port
  36. username = 'admin' # Replace with your qBittorrent username
  37. password = 'password123' # Replace with your qBittorrent password
  38.  
  39. logging.info("========== qbitLabeller.py started ==========")
  40.  
  41. # Log command line arguments
  42. if len(sys.argv) < 3:
  43. logging.error(f"Not enough arguments. Received: {sys.argv}")
  44. print(f"Error: Need 2 arguments (hash and label), got {len(sys.argv)-1}")
  45. sys.exit(1)
  46.  
  47. # Command-line arguments
  48. torrent_hash = sys.argv[1]
  49. label = sys.argv[2]
  50.  
  51. logging.info(f"Args received - hash: {torrent_hash}, label: {label}")
  52. print(f"Managing tags for {torrent_hash} with status '{label}'")
  53.  
  54. # Instantiate a Client using the appropriate WebUI configuration
  55. qbt_client = qbittorrentapi.Client(
  56. host=host,
  57. port=port,
  58. username=username,
  59. password=password,
  60. VERIFY_WEBUI_CERTIFICATE=False # Add this if using HTTPS with self-signed cert
  61. )
  62.  
  63. try:
  64. # Authenticate to the qBittorrent WebUI
  65. logging.info("Attempting to authenticate to qBitTorrent WebUI")
  66. qbt_client.auth_log_in()
  67. logging.info("Successfully authenticated")
  68.  
  69. # Check if the hash exists in qBitTorrent
  70. logging.info(f"Checking if hash {torrent_hash} exists")
  71. torrent_info = qbt_client.torrents_info(hashes=[torrent_hash])
  72.  
  73. if not torrent_info:
  74. logging.error(f"Torrent with hash {torrent_hash} not found")
  75. print(f"Error: Torrent with hash {torrent_hash} not found")
  76. sys.exit(2)
  77.  
  78. logging.info(f"Found torrent: {torrent_info[0].name}")
  79.  
  80. # Get existing tags on the torrent
  81. existing_tags = torrent_info[0].tags.split(', ') if torrent_info[0].tags else []
  82. logging.info(f"Current tags on torrent: {existing_tags}")
  83.  
  84. # Get all available tags
  85. available_tags = qbt_client.torrent_tags.tags
  86. logging.info(f"Available tags in qBittorrent: {available_tags}")
  87.  
  88. # Process based on the label status
  89. if label in ["QUEUED", "NOT_QD"]:
  90. logging.info(f"Processing initial status tag: {label}")
  91.  
  92. # Ensure tag exists
  93. if label not in available_tags:
  94. logging.info(f"Creating new tag: {label}")
  95. qbt_client.torrent_tags.create_tags(tags=label)
  96. logging.info(f"Tag '{label}' created successfully")
  97.  
  98. # Add the status tag
  99. logging.info(f"Adding '{label}' tag to torrent {torrent_hash}")
  100. qbt_client.torrent_tags.add_tags(tags=label, torrent_hashes=torrent_hash)
  101. logging.info(f"Added '{label}' tag to torrent {torrent_hash}")
  102.  
  103. elif label in ["DONE", "NOPE"]:
  104. logging.info(f"Processing completion status tag: {label}")
  105.  
  106. # Remove QUEUED tag if it exists
  107. if "QUEUED" in existing_tags:
  108. logging.info(f"Removing 'QUEUED' tag from torrent {torrent_hash}")
  109. qbt_client.torrent_tags.remove_tags(tags="QUEUED", torrent_hashes=torrent_hash)
  110. logging.info(f"Removed 'QUEUED' tag from torrent {torrent_hash}")
  111.  
  112. # Ensure completion tag exists
  113. if label not in available_tags:
  114. logging.info(f"Creating new tag: {label}")
  115. qbt_client.torrent_tags.create_tags(tags=label)
  116. logging.info(f"Tag '{label}' created successfully")
  117.  
  118. # Add the completion tag
  119. logging.info(f"Adding '{label}' tag to torrent {torrent_hash}")
  120. qbt_client.torrent_tags.add_tags(tags=label, torrent_hashes=torrent_hash)
  121. logging.info(f"Added '{label}' tag to torrent {torrent_hash}")
  122.  
  123. else:
  124. logging.info(f"Processing custom tag: {label}")
  125.  
  126. # Ensure tag exists
  127. if label not in available_tags:
  128. logging.info(f"Creating new tag: {label}")
  129. qbt_client.torrent_tags.create_tags(tags=label)
  130. logging.info(f"Tag '{label}' created successfully")
  131.  
  132. # Add the custom tag
  133. logging.info(f"Adding '{label}' tag to torrent {torrent_hash}")
  134. qbt_client.torrent_tags.add_tags(tags=label, torrent_hashes=torrent_hash)
  135. logging.info(f"Added '{label}' tag to torrent {torrent_hash}")
  136.  
  137. # Verify the tags were added correctly
  138. updated_info = qbt_client.torrents_info(hashes=[torrent_hash])
  139. updated_tags = updated_info[0].tags.split(', ') if updated_info[0].tags else []
  140. logging.info(f"Final tags on torrent after operation: {updated_tags}")
  141.  
  142. print(f"Successfully managed tags for torrent {torrent_hash}")
  143. logging.info(f"Tag operation completed successfully for torrent {torrent_hash}")
  144. sys.exit(0)
  145.  
  146. except qbittorrentapi.LoginFailed as e:
  147. error_msg = f"Failed to authenticate: {e}"
  148. logging.error(error_msg)
  149. print(error_msg)
  150. sys.exit(3)
  151. except Exception as e:
  152. error_msg = f"An error occurred: {e}"
  153. logging.error(error_msg)
  154. print(error_msg)
  155. logging.exception("Detailed error traceback:")
  156. sys.exit(4)
Add Comment
Please, Sign In to add comment