Skip to content

Instantly share code, notes, and snippets.

@fabricionaweb
Last active March 30, 2023 14:26
Show Gist options
  • Save fabricionaweb/89c184932443acb71fe34b7c88e85228 to your computer and use it in GitHub Desktop.
Save fabricionaweb/89c184932443acb71fe34b7c88e85228 to your computer and use it in GitHub Desktop.
Pause/Resume qbittorrent bash script
#!/bin/bash -e
# qbit settings
QBIT_API="http://qbittorrent.lan:8080/api/v2"
# tag to filter by, it will be removed on resume command
TAG="cache"
# days to filter in epoch time
# DAYS_OLD=$(date -d "-1 days" +%s)
# read value from ca.mover.tunning
CA_SETTINGS=$(cat /boot/config/plugins/ca.mover.tuning/ca.mover.tuning.cfg | awk -F '"' '/daysold/{print $2}')
DAYS_OLD=$(date -d "-$CA_SETTINGS days" +%s)
# $1 is the script argument
ACTION="$1"
if [[ "$ACTION" != "pause" && "$ACTION" != "resume" ]]; then
echo "Invalid argument, expected 'resume' or 'pause'"
exit 1
fi
# decide which filter to use for perform search
# to PAUSE we filter for seeding
# to RESUME we filter for paused
if [[ "$ACTION" == "pause" ]]; then
FILTER="completed";
else
FILTER="paused";
fi
# get torrents id with status 'completed' and the tag
# somehow I can not filter by tags in api, so lets filter in jq
TORRENTS_HASH=$(curl -sS "$QBIT_API/torrents/info?filter=$FILTER" \
| jq \
--arg TAG "$TAG" \
--argjson DAYS_OLD "$DAYS_OLD" \
--raw-output \
'.[] | select(.tags | contains($TAG)) | select(.completion_on < $DAYS_OLD) | .hash' \
| tr '[:space:]' '|'
)
# pause or resume
curl -sSX POST "$QBIT_API/torrents/$ACTION" --data "hashes=$TORRENTS_HASH"
# if the action is resume we will assume the move have been done and can remove the tag
if [[ "$ACTION" == "resume" ]]; then
curl -sSX POST "$QBIT_API/torrents/removeTags" --data "hashes=$TORRENTS_HASH&tags=$TAG"
fi
# wait a little
sleep 30
@fabricionaweb
Copy link
Author

./pause-resume-qbit.sh pause and ./pause-resume-qbit.sh resume

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