Skip to content

Instantly share code, notes, and snippets.

@kevingatera
Forked from jef/savecategory.md
Last active January 31, 2024 14:53
Show Gist options
  • Save kevingatera/1f2860ecf68782ab413468d6e961f8da to your computer and use it in GitHub Desktop.
Save kevingatera/1f2860ecf68782ab413468d6e961f8da to your computer and use it in GitHub Desktop.
qBittorrent: Saves category for watched directories (https://github.com/qbittorrent/qBittorrent/wiki/External-programs:-savecategory)

This script add Categories to Automatically add torrents from per Monitored Folder

👉 This script is in flux, as it may be implemented into qBittorrent in the future.

savecategory expects the user's watch directories to look similar to something like this:

It's based off the popular wiki for setting up hard linking Docker and other torrent grabbers.

Installation

Save the script below as savecategory and make it executable via chmod 755 /path/to/savecategory.

#!/bin/sh

category="$(basename $1)"
torrent_hash="$2"
torrent_name="$3"
host="http://localhost:8112"
username="admin"
password="adminadmin"

echo "running savecategory script"

echo "\tgetting cookie"

cookie=$(curl --silent --fail --show-error \
    --header "Referer: $host" \
    --cookie-jar - \
    --data "username=$username&password=$password" \
    --request POST "$host/api/v2/auth/login")

if [ -z "$cookie" ]; then
    echo "Login failed, exiting script."
    exit 1
fi

echo "\tsetting $torrent_name to category $category"

echo "$cookie" | curl --silent --fail --show-error \
    --cookie - \
    --header "Content-Type: application/x-www-form-urlencoded" \
    --data "hashes=$torrent_hash&category=$category" \
    --request POST "$host/api/v2/torrents/setCategory"

echo "completed savecategory script"

exit 0

ℹ️ Make sure to replace the username and password with your credentials before using or else this will not work. Another caveat is that if your password contains # or &, you'll need to replace with ASCII encoded characters.

🔗 gist for potential script changes or comments.

And set Run external program on torrent completion to:

/path/to/savecategory "%D" "%I" "%N"

On completion, the category will change based on the directory name the .torrent file was placed in the watch directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment