qbittorrent / qBittorrent

qBittorrent BitTorrent client
https://www.qbittorrent.org
Other
28.49k stars 3.99k forks source link

Option to automaticly remove torrents within a category that are inactive for too long #15550

Open Little-Karl opened 3 years ago

Little-Karl commented 3 years ago

Suggestion

Option to automatically remove torrents within a category that are inactive for too long or last activity

I know there is a way to do that in Linux but I can't find any solution for windows

glassez commented 3 years ago

I know there is a way to do that in Linux but I can't find any solution for windows

Does this method use any external tools?

shtreifling commented 2 weeks ago

You can create a script that will check it for you.

Just install pip install qbittorrent-api

Create the Python Script

import qbittorrentapi
from datetime import datetime, timedelta

# Connect to qBittorrent
qb = qbittorrentapi.Client(host='http://localhost:8080', username='your_username', password='your_password')

# Set inactivity threshold to 7 days
threshold = timedelta(days=7)
now = datetime.now()

# Specify the category you want to check
category_to_check = 'your_category_name'

try:
    # Authenticate to the qBittorrent client
    qb.auth_log_in()

    # Check torrents in the specified category
    torrents = qb.torrents_info(filter='all')

    for torrent in torrents:
        if torrent.category == category_to_check:
            last_activity = datetime.fromtimestamp(torrent.last_activity)
            if now - last_activity > threshold:
                qb.torrents_delete(delete_files=True, torrent_hashes=torrent.hash)
                print(f"Removed torrent: {torrent.name} (Hash: {torrent.hash}) - Inactive since {last_activity}")

except qbittorrentapi.LoginFailed as e:
    print(f"Login failed: {e}")
except Exception as e:
    print(f"An error occurred: {e}")

Replace your_username, your_password, localhost:8080, and your_category_name with your actual qBittorrent credentials and the category you want to monitor.

Save the file with a .py extension, for example, remove_inactive_category.py. Choose a directory you can easily access, like C:\Scripts.

Navigate to the directory where your script is located

cd C:\Scripts

python remove_inactive_category.py

Schedule the Script Press Win + R, type taskschd.msc, and hit Enter.

Create a New Task: In the Task Scheduler window, click on Create Basic Task in the right-hand menu. Set Task Details: Name your task (e.g., "Remove Inactive Torrents") and click Next. Choose Trigger: Select how often you want the task to run (e.g., Daily) and click Next. Set the start date and time. Choose Action: Select Start a program and click Next. Set the Program/Script: In the Program/script field, enter: python In the Add arguments (optional) field, enter the path to your script: C:\Scripts\remove_inactive_category.py In the Start in (optional) field, enter the directory where your script is located: C:\Scripts

Click Next, review the settings, and click Finish.