Skip to content

Instantly share code, notes, and snippets.

@jef
Last active August 2, 2024 00:12
Show Gist options
  • Save jef/e29126da5953c331310c1b6c58502be0 to your computer and use it in GitHub Desktop.
Save jef/e29126da5953c331310c1b6c58502be0 to your computer and use it in GitHub Desktop.

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.

@JackDandy
Copy link

JackDandy commented Jun 8, 2020

The wiki states...

Now whenever a .torrent file gets dropped in your watch folders, it'll set the category correctly.

This is a little misleading, it would be better if written something more like;

when the torrent has complete, its category will be changed.

At the moment, the text reads as though the cat is set "whenever" the torrent is dropped into the watched folder.

@jef
Copy link
Author

jef commented Jun 8, 2020

This is a little misleading, it would be better if written something more like;

Good point, I'll go update that.

@mgaulton
Copy link

mgaulton commented Feb 9, 2022

I currently assign as many categories as I can recognize though api calls, but for the ones that aren't recognized, I'm wondering if I could leverage this.

  1. put torrent in watch folder
  2. run api and assign
  3. modify my exist postproc bash to determine if no category was set and assign it.
    How long the delay for this to take effect, not sure
  4. Run TorrentToMedia.py using a. the assigned category from the external app call or b. parse it from the watch folder path.

Does this require that I don't use an non-aggregate /processing folder and instead keep the temp data in the watch folder?

I import from /loader/category, savepath is /data/category and all temp files go into /processing.

@kevingatera
Copy link

Hi, as it stands the script no longer works perhaps due to changes in the qBittorrrent API but here are the updates required as of December 13th 2023.

https://gist.github.com/kevingatera/1f2860ecf68782ab413468d6e961f8da/revisions#diff-ddc2c72e571aef7c0ac8419e261d52f0f17d515df62f9b06d3cbb9cd0e1ba231

@jef
Copy link
Author

jef commented Mar 23, 2024

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

@kpupp
Copy link

kpupp commented Aug 2, 2024

I cannot believe qBittorrent still doesn't do this. Thanks for this, will give it a shot.

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