diff --git a/Tools/downloaders_reset.sh b/Tools/downloaders_reset.sh index 219d590..6655033 100644 --- a/Tools/downloaders_reset.sh +++ b/Tools/downloaders_reset.sh @@ -364,55 +364,54 @@ QBIT_COOKIE=$(curl -s -c - \ grep SID | awk '{print "SID="$NF}') if [[ -z "$QBIT_COOKIE" ]]; then - echo "❌ Failed to authenticate with qBittorrent — check URL, username, password" + echo "❌ Failed to authenticate with qBittorrent" else TORRENTS=$(curl -s \ "$QBIT_URL/api/v2/torrents/info" \ -H "Cookie: $QBIT_COOKIE") - HASHES=$(echo "$TORRENTS" | grep -o '"hash":"[^"]*"' | \ - sed 's/"hash":"//;s/"//') - NOW=$(date +%s) DELETED=0 SKIPPED=0 - while IFS= read -r HASH; do - [[ -z "$HASH" ]] && continue + # Each torrent is one JSON object — split on },{ to process individually + echo "$TORRENTS" | tr '}' '\n' | while read -r TORRENT; do + [[ -z "$TORRENT" ]] && continue - ADDED=$(echo "$TORRENTS" | grep -A30 "\"hash\":\"$HASH\"" | \ - grep -o '"added_on":[0-9]*' | grep -o '[0-9]*' | head -1) - RATIO=$(echo "$TORRENTS" | grep -A30 "\"hash\":\"$HASH\"" | \ - grep -o '"ratio":[0-9.]*' | grep -o '[0-9.]*' | head -1) - NAME=$(echo "$TORRENTS" | grep -A30 "\"hash\":\"$HASH\"" | \ - grep -o '"name":"[^"]*"' | sed 's/"name":"//;s/"//' | head -1) + HASH=$(echo "$TORRENT" | grep -o '"hash":"[^"]*"' | \ + sed 's/"hash":"//;s/"//') + NAME=$(echo "$TORRENT" | grep -o '"name":"[^"]*"' | \ + sed 's/"name":"//;s/"//') + ADDED=$(echo "$TORRENT" | grep -o '"added_on":[0-9]*' | \ + grep -o '[0-9]*') + RATIO=$(echo "$TORRENT" | grep -o '"ratio":[0-9.]*' | \ + grep -o '[0-9.]*') - [[ -z "$ADDED" ]] && continue + [[ -z "$HASH" || -z "$ADDED" ]] && continue - # Age in days — skip if under threshold + # Age check — integer only, no bc needed AGE_DAYS=$(( (NOW - ADDED) / 86400 )) - [[ "$AGE_DAYS" -lt "$QBIT_FAILSAFE_MIN_DAYS" ]] && ((SKIPPED++)) && continue + [[ "$AGE_DAYS" -lt "$QBIT_FAILSAFE_MIN_DAYS" ]] && continue - # Ratio gate — skip if ratio below minimum (disabled when set to 0) + # Ratio check — integer comparison only, strip decimal if [[ "$QBIT_FAILSAFE_MIN_RATIO" != "0" ]]; then - RATIO_OK=$(echo "$RATIO >= $QBIT_FAILSAFE_MIN_RATIO" | bc -l 2>/dev/null) - [[ "$RATIO_OK" != "1" ]] && ((SKIPPED++)) && continue + RATIO_INT=${RATIO%.*} + MIN_RATIO_INT=${QBIT_FAILSAFE_MIN_RATIO%.*} + [[ "$RATIO_INT" -lt "$MIN_RATIO_INT" ]] && continue fi if $DRY_RUN; then echo "🧪 Would delete: $NAME (${AGE_DAYS} days old, ratio: $RATIO)" - ((DELETED++)) else curl -s -X POST \ "$QBIT_URL/api/v2/torrents/delete" \ -H "Cookie: $QBIT_COOKIE" \ --data "hashes=$HASH&deleteFiles=false" echo " 🗑️ Deleted: $NAME (${AGE_DAYS} days old, ratio: $RATIO)" - ((DELETED++)) fi - done <<< "$HASHES" + done - echo "✅ qBittorrent: $DELETED deleted, $SKIPPED skipped" + echo "✅ qBittorrent: complete" fi echo ""