TRANSMISSION - kickass-seedings-torrents.sh

From IT-Arts.net


Return to Wiki Index

#!/usr/bin/env bash

# Reannounce completed Transmission torrents
#
# Usage:
#   ./kickass-seedings-torrents.sh
#   ./kickass-seedings-torrents.sh -d   (debug / dry-run)
#
# Journal:
#   journalctl -t kickass-seedings-torrents -f

HOST="localhost"
PORT="9091"
USER="your_username"
PASS="your_password"

DELAY=10          # seconds between announces
MAX=50            # safety maximum torrents per run
TIMEOUT=15        # timeout per RPC command

TAG="kickass-seedings-torrents"

DEBUG=0

# CHECK IF TRANSMISSION IS RUNNING
if ! systemctl is-active --quiet transmission-daemon; then
    log_error "Transmission daemon is not running. Exiting."
    exit 1
fi

# DEBUG MODE
if [[ "$1" == "-d" ]]; then
    DEBUG=1
fi

# LOG FUNCTION
log()
{
    echo "$1"
    logger -t "$TAG" "$1"
}

log_error()
{
    echo "$1" >&2
    logger -t "$TAG" "$1"
}

# COMMAND
REMOTE=(transmission-remote "$HOST:$PORT" -n "${USER}:${PASS}")

if [[ "$DEBUG" -eq 1 ]]; then
    log "DEBUG MODE ENABLED (dry run, no reannounce sent)"
    printf "Command: "
    printf "%q " "${REMOTE[@]}"
    echo

    logger -t "$TAG" "Command: $(printf '%q ' "${REMOTE[@]}")"
fi

log "Getting torrent list..."

LIST=$("${REMOTE[@]}" -l 2>&1)

if [[ $? -ne 0 ]]; then
    log_error "ERROR: transmission-remote failed:"
    log_error "$LIST"
    exit 1
fi

if [[ "$DEBUG" -eq 1 ]]; then
    log "--- Raw transmission output ---"
    while IFS= read -r line; do
        log "$line"
    done <<< "$LIST"
    log "--- End output ---"
fi

TORRENTS=$(echo "$LIST" | awk '
NR > 1 && $1 != "Sum:" {
    gsub(/\*/, "", $1)

    # Completed torrents only
    if ($2 == "100%" && $3 != "None")
        print $1
}')

if [[ -z "$TORRENTS" ]]; then
    log "No completed torrents found."
    exit 0
fi

COUNT=0

while read -r id; do

    [[ -z "$id" ]] && continue

    if [[ "$COUNT" -ge "$MAX" ]]; then
        log "Safety limit reached ($MAX torrents). Stopping."
        break
    fi

    log "Torrent ID $id selected"

    if [[ "$DEBUG" -eq 1 ]]; then
        log "DRY RUN:"
        CMD=$(printf "%q " "${REMOTE[@]}" -t "$id" --reannounce)
        log "  $CMD"

    else
        log "Reannouncing torrent ID $id..."

        OUTPUT=$(timeout "$TIMEOUT" \
            "${REMOTE[@]}" -t "$id" --reannounce 2>&1)

        RET=$?

        if [[ $RET -eq 0 ]]; then
            log "Torrent ID $id: OK"
        else
            log "Torrent ID $id: FAILED (exit $RET)"
            while IFS= read -r line; do
                log "  $line"
            done <<< "$OUTPUT"
        fi
    fi

    COUNT=$((COUNT + 1))

    if [[ "$DEBUG" -eq 0 ]]; then
        sleep "$DELAY"
    fi

done <<< "$TORRENTS"

log "Finished. Processed $COUNT torrent(s)."

Return to Wiki Index