scripts/yt-dlp-ntfy.sh

45 lines
1.3 KiB
Bash
Executable File

#!/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
event=$(echo "$message" | jq -r '.event')
[[ "$event" == "keepalive" ]] && continue
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