From ea073588129c35d2bc056d67f8ff6853c4ba28d3 Mon Sep 17 00:00:00 2001 From: Joey Hafner Date: Thu, 25 Apr 2024 11:07:29 -0700 Subject: [PATCH] Add discord notifier script to qbt container --- .../config/qbittorrent/discord_notifier.sh | 108 ++++++++++++++++++ fighter/config/qbittorrent/docker-compose.yml | 1 + 2 files changed, 109 insertions(+) create mode 100644 fighter/config/qbittorrent/discord_notifier.sh diff --git a/fighter/config/qbittorrent/discord_notifier.sh b/fighter/config/qbittorrent/discord_notifier.sh new file mode 100644 index 0000000..9bca9dc --- /dev/null +++ b/fighter/config/qbittorrent/discord_notifier.sh @@ -0,0 +1,108 @@ +#!/bin/bash + +# Discord webhook details +discord_webhook_url="$(cat discord_notifier_secrets.env)" + +# Get variables from qBitTorrent. Remember to make sure these align with your qBittorrent variables passed in settings. +torrent_name="$1" +size="$2" +files="$3" +tracker="$4" +category="$5" +path="$6" + +# Function to convert size from bytes to gigabytes (GB) +calculate_size_in_gb() { + size_in_gb=$(bc <<< "scale=2; $size / (1024 * 1024 * 1024)") + echo "$size_in_gb GB" +} +# Function to convert size from bytes to megabytes (MB) +calculate_size_in_mb() { + size_in_mb=$(bc <<< "scale=2; $size / (1024 * 1024)") + echo "$size_in_mb MB" +} +# Function to convert size from bytes to kilobytes (KB) +calculate_size_in_kb() { + size_in_kb=$(bc <<< "scale=2; $size / 1024") + echo "$size_in_kb KB" +} + +# Determine download type based on the category. +# This is optional, only if you want to change the notification message based on the category. +# You can also comment out this entire section and simply use $category in the 'title' field of the payload below. +case "$category" in + "emp") + download_type="Empornium" + ;; + "ggn") + download_type="GGN" + ;; + "mam") + download_type="MAM" + ;; + "pub") + download_type="Public Torrent" + ;; + *) + download_type="Uncategorised" + ;; +esac + +# Calculate the size message based on the size in megabytes, kilobytes, or gigabytes +if (( size < 1024 * 1024 )); then + size_message=$(calculate_size_in_kb) +elif (( size < 1024 * 1024 * 1024 )); then + size_message=$(calculate_size_in_mb) +else + size_message=$(calculate_size_in_gb) +fi + +# Construct the JSON payload for Discord +# You can change the color of the left hand stripe of the notification using the "color" field. +payload='{ + "embeds": [ + { + "author": { + "name": "qBittorrent", + "icon_url": "https://i.imgur.com/6LTKLgZ.jpg" + }, + "title": "'$download_type' download completed", + "color": 7506394, + "fields": [ + { + "name": "Torrent", + "value": "'$torrent_name'" + }, + { + "name": "Size", + "value": "'$size_message'", + "inline": true + }, + { + "name": "Files", + "value": "'$files'", + "inline": true + }, + { + "name": "Tracker", + "value": "'$tracker'" + }, + { + "name": "Save Path", + "value": "'$path'" + } + ] + } + ] +}' + +# Function to send a notification to Discord +send_discord_notification() { + curl -H "Content-Type: application/json" -X POST -d "$payload" "$discord_webhook_url" >/dev/null 2>&1 +} + +# Send a notification to Discord +send_discord_notification + +# Print an info message in the console +echo "[$torrent_name] ${download_type} completed. Discord notification sent." \ No newline at end of file diff --git a/fighter/config/qbittorrent/docker-compose.yml b/fighter/config/qbittorrent/docker-compose.yml index c7fcc1a..2207128 100644 --- a/fighter/config/qbittorrent/docker-compose.yml +++ b/fighter/config/qbittorrent/docker-compose.yml @@ -13,6 +13,7 @@ services: volumes: - $DOCKER_DATA:/config - $TORRENT_DATA:/torrenting + - ./discord_notifier.sh:/discord_notifier.sh env_file: - path: ./qbittorrent.env required: true