Last active
December 26, 2025 23:21
-
-
Save P6g9YHK6/d067d0acb73b8100d725e5c62a0f0f2e to your computer and use it in GitHub Desktop.
ygg-api-download-updater
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| set -euo pipefail | |
| # ============================================================================= | |
| # SYNOPSIS | |
| # Updates a custom Prowlarr definition from a remote source, creates backups, | |
| # restarts the container if needed, and sends Gotify notifications. | |
| # ============================================================================= | |
| # LANGUAGE: Message language (en or fr) | |
| LANGUAGE="en" | |
| # LOCAL_FILE: Path to the local Prowlarr custom definition file | |
| LOCAL_FILE="/mnt/user/appdata/prowlarr/Definitions/Custom/ygg-api-download.yml" | |
| # BACKUP_DIR: Directory where timestamped backups are stored | |
| BACKUP_DIR="/mnt/user/appdata/prowlarr/Definitions/Custom/backups" | |
| # TEMP_FILE: Temporary file used for the downloaded remote definition | |
| TEMP_FILE="/tmp/ygg-api-download.yml.tmp" | |
| # REMOTE_URL: Remote URL of the definition file to download | |
| REMOTE_URL="https://gist.githubusercontent.com/Clemv95/8bfded23ef23ec78f6678896f42a2b60/raw/ygg-api-download.yml" | |
| # DOCKER_CONTAINER: Name of the Prowlarr Docker container to restart | |
| DOCKER_CONTAINER="Prowlarr" | |
| # GOTIFY_URL: Base Gotify message endpoint URL | |
| GOTIFY_URL="https://your.gotify.server/message" | |
| # GOTIFY_TOKEN: Gotify application token for authentication | |
| GOTIFY_TOKEN="YOUR_APP_TOKEN" | |
| # ====================== | |
| # Translation helper | |
| # ====================== | |
| t() { | |
| case "$LANGUAGE:$1" in | |
| fr:LOCAL_MISSING) echo "Fichier local introuvable — mise à jour annulée." ;; | |
| fr:DOWNLOAD_FAIL) echo "Échec du téléchargement du fichier distant." ;; | |
| fr:NO_UPDATE) echo "Aucune mise à jour nécessaire — fichiers identiques." ;; | |
| fr:BACKUP_CREATED) echo "Sauvegarde créée :" ;; | |
| fr:UPDATE_DONE) echo "Mise à jour appliquée." ;; | |
| fr:RESTARTING) echo "Redémarrage de Prowlarr..." ;; | |
| fr:RESTART_OK) echo "Prowlarr redémarré avec succès." ;; | |
| fr:RESTART_FAIL) echo "Échec du redémarrage du conteneur Prowlarr." ;; | |
| fr:CONTAINER_MISS) echo "Conteneur Prowlarr introuvable." ;; | |
| fr:DOWNLOAD_OK) echo "Fichier distant téléchargé." ;; | |
| fr:BACKUP_CLEAN) echo "Anciennes sauvegardes supprimées." ;; | |
| en:LOCAL_MISSING) echo "Local file does not exist — update aborted." ;; | |
| en:DOWNLOAD_FAIL) echo "Failed to download remote file." ;; | |
| en:NO_UPDATE) echo "No update needed — files match." ;; | |
| en:BACKUP_CREATED) echo "Backup created:" ;; | |
| en:UPDATE_DONE) echo "Update applied." ;; | |
| en:RESTARTING) echo "Restarting Prowlarr..." ;; | |
| en:RESTART_OK) echo "Prowlarr restarted successfully." ;; | |
| en:RESTART_FAIL) echo "Failed to restart Prowlarr container." ;; | |
| en:CONTAINER_MISS) echo "Prowlarr container not found." ;; | |
| en:DOWNLOAD_OK) echo "Remote file downloaded." ;; | |
| en:BACKUP_CLEAN) echo "Old backups removed." ;; | |
| *) echo "$1" ;; | |
| esac | |
| } | |
| # Cleanup temporary file on exit | |
| cleanup() { rm -f "$TEMP_FILE"; } | |
| trap cleanup EXIT | |
| # Log messages to stdout | |
| log() { echo "[YGG-UPDATE] $1"; } | |
| # Send Gotify notification | |
| notify() { | |
| curl -s --fail -X POST "$GOTIFY_URL?token=$GOTIFY_TOKEN" \ | |
| -F "title=$1" -F "message=$2" -F "priority=${3:-5}" >/dev/null || true | |
| } | |
| # Abort if local file does not exist | |
| if [ ! -f "$LOCAL_FILE" ]; then | |
| msg=$(t LOCAL_MISSING) | |
| log "$msg" | |
| notify "YGG Update Error" "$msg" 8 | |
| exit 1 | |
| fi | |
| # Download remote definition | |
| if ! curl -sSL --fail \ | |
| --proto '=https' --tlsv1.2 \ | |
| --connect-timeout 10 --max-time 30 \ | |
| --retry 3 --retry-delay 5 \ | |
| "$REMOTE_URL" -o "$TEMP_FILE"; then | |
| msg=$(t DOWNLOAD_FAIL) | |
| log "$msg" | |
| notify "YGG Update Error" "$msg" 9 | |
| exit 1 | |
| fi | |
| log "$(t DOWNLOAD_OK)" | |
| # Exit if files are identical | |
| if cmp -s "$TEMP_FILE" "$LOCAL_FILE"; then | |
| log "$(t NO_UPDATE)" | |
| exit 0 | |
| fi | |
| # Create backup directory if needed | |
| mkdir -p "$BACKUP_DIR" | |
| # timestamp: Current date/time for backup naming | |
| timestamp=$(date +"%Y-%m-%d_%H-%M-%S") | |
| # backup_file: Full path of the backup file | |
| backup_file="$BACKUP_DIR/ygg-api-download.yml.$timestamp.bak" | |
| cp "$LOCAL_FILE" "$backup_file" | |
| log "$(t BACKUP_CREATED) $backup_file" | |
| # Remove old backups, keeping only the 3 newest | |
| mapfile -t old_backups < <(ls -1t "$BACKUP_DIR"/*.bak 2>/dev/null | tail -n +4) | |
| for f in "${old_backups[@]}"; do rm -f "$f"; done | |
| [ "${#old_backups[@]}" -gt 0 ] && log "$(t BACKUP_CLEAN)" | |
| # Replace local file with updated version | |
| mv "$TEMP_FILE" "$LOCAL_FILE" | |
| log "$(t UPDATE_DONE)" | |
| notify "YGG API Updated" "$(t UPDATE_DONE)" 5 | |
| # Restart Prowlarr container | |
| if docker ps --format '{{.Names}}' | grep -qx "$DOCKER_CONTAINER"; then | |
| log "$(t RESTARTING)" | |
| if docker restart "$DOCKER_CONTAINER" >/dev/null 2>&1; then | |
| log "$(t RESTART_OK)" | |
| notify "Prowlarr" "$(t RESTART_OK)" 4 | |
| else | |
| msg=$(t RESTART_FAIL) | |
| log "$msg" | |
| notify "Prowlarr Error" "$msg" 10 | |
| exit 1 | |
| fi | |
| else | |
| msg=$(t CONTAINER_MISS) | |
| log "$msg" | |
| notify "Prowlarr Error" "$msg" 10 | |
| exit 1 | |
| fi | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment