Skip to content

Instantly share code, notes, and snippets.

@jef
Last active March 23, 2024 23:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jef/e29126da5953c331310c1b6c58502be0 to your computer and use it in GitHub Desktop.
Save jef/e29126da5953c331310c1b6c58502be0 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 Docker guide for setting up hard links between virtual volumes (Docker) and machine volumes.

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.

@jef
Copy link
Author

jef commented Mar 23, 2024

Thanks for the update @kevingatera, I have updated the source.

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