scripts/yt-dlp-ntfy.sh

46 lines
1.3 KiB
Bash
Raw Normal View History

2024-11-14 18:53:46 +00:00
#!/bin/bash
set -ueo pipefail
2024-11-14 18:53:46 +00:00
# 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"
2024-11-16 19:29:05 +00:00
echo "Running as PID $(cat $PID_FILE)"
2024-11-14 18:53:46 +00:00
# Define ntfy server and channel
NTFY_SERVER="https://notify.kucharczyk.xyz"
CHANNEL="clipboard"
ACCESS_TOKEN="$NTFY_ACCESS_TOKEN"
2024-11-16 19:29:05 +00:00
echo "Monitoring channel $CHANNEL of server $NTFY_SERVER"
2024-11-14 18:53:46 +00:00
# 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
2024-11-14 18:53:46 +00:00
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"
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