Add notification function to yt-dlp

This commit is contained in:
Yan Lin 2025-10-09 08:41:28 +02:00
parent 51abd863a3
commit 177dbf3048
2 changed files with 86 additions and 7 deletions

View file

@ -14,6 +14,9 @@
programs.yt-dlp-custom = {
enable = true;
downloadDir = "/mnt/storage/Media/web";
enableNotifications = true;
gotifyUrl = "https://notify.yanlincs.com";
gotifyToken = "Ac9qKFH5cA.7Yly"; # Same token as container-updater and borg-backup
};
services.scheduled-commands.video-downloads = {
@ -39,6 +42,7 @@
"dlv youtube -n 3 -r 1 --min 1 --max 180 'https://www.youtube.com/@thespiffingbrit/videos'"
"dlv youtube -n 3 -r 1 --min 1 --max 180 'https://www.youtube.com/@DoshDoshington/videos'"
"dlv youtube -n 3 -r 1 --min 1 --max 180 'https://www.youtube.com/@ScottTheWoz/videos'"
"dlv bilibili -n 3 -r 1 --title '' 'https://space.bilibili.com/15810/upload/video'"
];
};

View file

@ -23,6 +23,26 @@ in
example = "/mnt/storage/videos";
description = "Base directory for downloaded videos";
};
enableNotifications = mkOption {
type = types.bool;
default = false;
description = "Enable Gotify notifications for download status";
};
gotifyUrl = mkOption {
type = types.str;
default = "";
example = "https://notify.yanlincs.com";
description = "Gotify server URL for notifications";
};
gotifyToken = mkOption {
type = types.str;
default = "";
example = "Ac9qKFH5cA.7Yly";
description = "Gotify API token for notifications (host-specific)";
};
};
config = mkIf cfg.enable {
@ -84,6 +104,11 @@ in
MAX_RETRIES=10
BASE_DELAY=10
# Notification configuration
ENABLE_NOTIFICATIONS="${toString cfg.enableNotifications}"
GOTIFY_URL="${cfg.gotifyUrl}"
GOTIFY_TOKEN="${cfg.gotifyToken}"
# Helper function to create writable cookie file
_setup_temp_cookies() {
local cookies_file="$1"
@ -97,6 +122,26 @@ in
fi
}
# Notification helper function
_send_download_notification() {
local priority="$1"
local title="$2"
local message="$3"
if [[ "$ENABLE_NOTIFICATIONS" == "1" ]] && [[ -n "$GOTIFY_URL" ]] && [[ -n "$GOTIFY_TOKEN" ]]; then
if [[ -x "$HOME/.config/nix/scripts/gotify-notify.sh" ]]; then
"$HOME/.config/nix/scripts/gotify-notify.sh" \
"$GOTIFY_URL" \
"$GOTIFY_TOKEN" \
"$priority" \
"$title" \
"$message" 2>/dev/null || echo "Failed to send notification (non-critical)" >&2
else
echo "Notification script not found or not executable" >&2
fi
fi
}
# Retry wrapper function with exponential backoff
_retry_download() {
local cmd="$1"
@ -285,18 +330,48 @@ in
# Execute download with retry
if _retry_download "$cmd"; then
# Build success message
local success_msg="$platform_name download completed"
[[ "$playlist_mode" == true ]] && success_msg="$platform_name playlist download completed"
# Add filter info if any
local filter_info=""
if [[ -n "$min_duration" ]] || [[ -n "$max_duration" ]] || [[ -n "$title_filter" ]]; then
filter_info=" (Filters:"
[[ -n "$min_duration" ]] && filter_info="$filter_info min ''${min_duration}m"
[[ -n "$max_duration" ]] && filter_info="$filter_info max ''${max_duration}m"
[[ -n "$title_filter" ]] && filter_info="$filter_info title: \"$title_filter\""
filter_info="$filter_info)"
fi
[[ -n "$max_downloads" ]] && filter_info="''${filter_info} [max ''${max_downloads} videos]"
success_msg="''${success_msg}''${filter_info}"
if [[ "$playlist_mode" == true ]]; then
echo " Playlist download completed successfully"
else
echo " Download completed successfully"
fi
# Send success notification
_send_download_notification "normal" "Download Completed" "$success_msg"
local result=0
else
# Build failure message
local fail_msg="$platform_name download failed after $MAX_RETRIES attempts"
[[ "$playlist_mode" == true ]] && fail_msg="$platform_name playlist download failed after $MAX_RETRIES attempts"
fail_msg="''${fail_msg}: $url"
if [[ "$playlist_mode" == true ]]; then
echo " Playlist download failed after $MAX_RETRIES attempts"
else
echo " Download failed after $MAX_RETRIES attempts"
fi
# Send failure notification
_send_download_notification "high" "Download Failed" "$fail_msg"
local result=1
fi