From e547a5802603c53969b850f2d40d468e6915102e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Thu, 14 Nov 2024 19:53:46 +0100 Subject: [PATCH] add yt-dlp-ntfy --- yt-dlp-ntfy.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 yt-dlp-ntfy.sh diff --git a/yt-dlp-ntfy.sh b/yt-dlp-ntfy.sh new file mode 100755 index 0000000..a754b53 --- /dev/null +++ b/yt-dlp-ntfy.sh @@ -0,0 +1,42 @@ +#!/bin/bash +set -uexo pipefail + +# Define PID file location +PID_FILE="/tmp/yt-dlp-ntfy.pid" + +# Check if the script is already running +if [ -e "$PID_FILE" ] && kill -0 $(cat "$PID_FILE"); then + echo "Script is already running. Exiting." + exit 1 +fi + +# Store the PID of the current process +echo $$ > "$PID_FILE" + +# Define ntfy server and channel +NTFY_SERVER="https://notify.kucharczyk.xyz" +CHANNEL="clipboard" +ACCESS_TOKEN="$NTFY_ACCESS_TOKEN" + +# Run the script in an infinite loop to listen for new messages +while true; do + while read -r message; do + video_url=$(echo "$message" | jq -r '.message') + + if [[ $video_url =~ ^https?:// ]]; then + echo "Downloading video from $video_url" + yt-dlp "$video_url" + curl -s -H "Authorization: Bearer $ACCESS_TOKEN" -d "Finished downloading." "$NTFY_SERVER/$CHANNEL" + else + echo "Received non-URL message: $video_url" + fi + done + + # Wait a bit before reconnecting if the connection drops + echo "Reconnecting..." + sleep 5 +done < <(curl --no-buffer -s -H "Authorization: Bearer $ACCESS_TOKEN" "$NTFY_SERVER/$CHANNEL/json") + +# Cleanup PID file on script exit +trap "rm -f $PID_FILE" EXIT +