Created
March 3, 2024 00:17
-
-
Save trevordavies095/0bab4d610a4a2c97c0af21816e4b0e35 to your computer and use it in GitHub Desktop.
remove_orphaned_qbittorrent_files.py
This file contains 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 os | |
import shutil | |
# Define your qBittorrent API endpoint | |
qbittorrent_api_url = '' | |
# Make a request to the qBittorrent API | |
response = requests.get(qbittorrent_api_url) | |
# Check if the request was successful | |
if response.status_code == 200: | |
# Extract the JSON response | |
seeded_torrents = response.json() | |
# Define your downloads directory | |
downloads_directory = '/mnt/qbittorrent/completed/' # Update with the actual path to your downloads directory | |
# Get a list of content paths of seeded torrents | |
# You can remove the replace if the content path from qbit api call is identical to your downloads_directory variable | |
seeded_content_paths = [torrent['content_path'].replace("/downloads/", downloads_directory) for torrent in seeded_torrents] | |
# List all files and directories in the downloads directory | |
all_files = os.listdir(downloads_directory) | |
# Initialize a list to store items to be deleted | |
items_to_delete = [] | |
# Iterate through files and directories in the downloads directory | |
for file_or_dir in all_files: | |
# Construct the full path | |
full_path = os.path.join(downloads_directory, file_or_dir) | |
# Check if the file or directory is not in the list of seeded content paths | |
if full_path not in seeded_content_paths: | |
# Add the item to the list of items to be deleted | |
items_to_delete.append(full_path) | |
# Print the list of items to be deleted | |
print("Items to be deleted:") | |
for item in items_to_delete: | |
print(item) | |
# Ask for user confirmation before deletion | |
user_input = input("Do you want to delete these items? (yes/no): ") | |
if user_input.lower() == "yes": | |
# Delete items | |
for item in items_to_delete: | |
if os.path.isdir(item): | |
try: | |
shutil.rmtree(item) # Remove directory | |
print(f"Removed directory: {item}") | |
except: | |
pass | |
else: | |
os.remove(item) # Remove file | |
print(f"Removed file: {item}") | |
print("Cleanup completed successfully!") | |
else: | |
print("Cleanup aborted.") | |
else: | |
print(f"Failed to fetch data from qBittorrent API. Status code: {response.status_code}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment