From a1aa5984e1c5e681747e3dc954c1e800ee506608 Mon Sep 17 00:00:00 2001 From: Lucas Faber Date: Fri, 25 Sep 2026 11:22:46 +0000 Subject: [PATCH] feat: torrent complete script to copy files to mediahost gets name, path and category from qbittorrent sends mqtt command copies files to mediahost --- torrent/torrent_qbittorrent_complete.sh | 174 ++++++++++++++++++++++++ torrent/torrentcomplete.sh | 12 -- 2 files changed, 174 insertions(+), 12 deletions(-) create mode 100755 torrent/torrent_qbittorrent_complete.sh delete mode 100755 torrent/torrentcomplete.sh diff --git a/torrent/torrent_qbittorrent_complete.sh b/torrent/torrent_qbittorrent_complete.sh new file mode 100755 index 0000000..d5c9923 --- /dev/null +++ b/torrent/torrent_qbittorrent_complete.sh @@ -0,0 +1,174 @@ +#!/bin/bash + +############################################################################### +# Description: +# qBittorrent external script that: +# 1. Receives torrent name, root path and category from qBittorrent. +# 2. Sends the torrent name to torrent_mqtt.py. +# 3. Copies the torrent contents to mediahost via SCP. +# 4. Logs all output to a timestamped logfile. +# +# Usage: +# torrent_complete.sh +# +# qBittorrent configuration: +# /home/lucas/scripts/serverhost/torrent_complete.sh "%N" "%R" "%L" +# +# Arguments: +# $1 Torrent name +# $2 Root path +# $3 Torrent category +# +# Log files: +# /home/lucas/.LOG/log_torrent_name_YYYY-MM-DD_HH-MM-SS.log +# +# Requirements: +# - bash +# - scp +# - SSH access to "mediahost" +# - /home/lucas/scripts/serverhost/torrent_mqtt.py +############################################################################### + +set -u + +SCRIPT_NAME="$(basename "$0")" +LOG_DIR="/home/lucas/.LOG" + +############################################################################### +# Usage +############################################################################### + +usage() { + cat < + +Description: + Sends the torrent name to torrent_mqtt.py and copies the torrent + contents to mediahost. + +Arguments: + torrent_name Torrent name + root_path Torrent root path + category Torrent category + +Example: + $SCRIPT_NAME "Ubuntu 24.04" "/downloads/Ubuntu 24.04" "Linux" + +qBittorrent: + $SCRIPT_NAME "%N" "%R" "%L" + +Log directory: + $LOG_DIR +EOF +} + +############################################################################### +# Check arguments +############################################################################### + +if [[ $# -ne 3 ]]; then + usage >&2 + exit 1 +fi + +TORRENT_NAME="$1" +ROOT_PATH="$2" +CATEGORY="$3" + +############################################################################### +# Logging +############################################################################### + +mkdir -p "$LOG_DIR" + +LOG_FILE="$LOG_DIR/log_torrent_name_$(date '+%Y-%m-%d_%H-%M-%S').log" + +# Redirect all stdout and stderr to the logfile +exec >> "$LOG_FILE" 2>&1 + +############################################################################### +# Start +############################################################################### + +echo "==================================================" +echo "Torrent transfer started" +echo "Date: $(date)" +echo "Script: $SCRIPT_NAME" +echo "Torrent name: $TORRENT_NAME" +echo "Root path: $ROOT_PATH" +echo "Category: $CATEGORY" +echo "Log file: $LOG_FILE" +echo "==================================================" + +############################################################################### +# Send torrent name to MQTT script +############################################################################### + +echo +echo "--- Sending torrent name to torrent_mqtt.py ---" + +if [[ ! -x "/home/lucas/scripts/serverhost/torrent_mqtt.py" ]]; then + echo "WARNING: torrent_mqtt.py is not executable." + echo "Attempting to run it through Python..." + python3 "/home/lucas/scripts/serverhost/torrent_mqtt.py" "$TORRENT_NAME" +else + "/home/lucas/scripts/serverhost/torrent_mqtt.py" "$TORRENT_NAME" +fi + +MQTT_STATUS=$? + +echo "torrent_mqtt.py exit status: $MQTT_STATUS" + +############################################################################### +# Check root path +############################################################################### + +echo +echo "--- Checking root path ---" + +if [[ ! -d "$ROOT_PATH" ]]; then + echo "ERROR: Root path does not exist or is not a directory:" + echo " $ROOT_PATH" + exit 1 +fi + +echo "Root path exists." + +############################################################################### +# Copy torrent to mediahost +############################################################################### + +echo +echo "--- Copying torrent to mediahost ---" +echo "Source: $ROOT_PATH" +echo "Destination: mediahost:/torrent/qbittorrent/mediahost/" + +scp -r "$ROOT_PATH"/. mediahost:/torrent/qbittorrent/mediahost/ +SCP_STATUS=$? + +echo "scp exit status: $SCP_STATUS" + +############################################################################### +# Result +############################################################################### + +echo +echo "==================================================" + +if [[ $MQTT_STATUS -ne 0 ]]; then + echo "WARNING: torrent_mqtt.py failed." +fi + +if [[ $SCP_STATUS -eq 0 ]]; then + echo "Torrent transfer completed successfully." +else + echo "ERROR: Torrent transfer failed." +fi + +echo "Finished: $(date)" +echo "==================================================" + +# Return SCP's exit status to qBittorrent +exit "$SCP_STATUS" + diff --git a/torrent/torrentcomplete.sh b/torrent/torrentcomplete.sh deleted file mode 100755 index e9f8e7b..0000000 --- a/torrent/torrentcomplete.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash - -echo "$@" >> /home/lucas/.LOG/log_torrentlog.txt - -/home/lucas/scripts/serverhost/torrent_mqtt.py "$*" - -#sudo mail -s 'Torr Completed' lucas-f@live.at <<< "Finished at : $(date)" - -#sudo /home/lucas/scripts/serverhost/lucas_cmd.sh ping -c 1 quiethost >> /dev/null -#if [ $? -eq 0 ]; then -# sudo /home/lucas/scripts/serverhost/lucas_cmd.sh ssh lucas@quiethost -p 1504 notify-send 'Torrent Complete' -#fi