2024-11-14 18:53:46 +00:00
|
|
|
#!/bin/bash
|
2024-11-16 19:28:31 +00:00
|
|
|
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
|
2024-11-16 19:32:45 +00:00
|
|
|
if [ -e "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") &>/dev/null; then
|
2024-11-16 19:34:17 +00:00
|
|
|
echo "Script is already running as PID $(cat $PID_FILE). Exiting."
|
2024-11-14 18:53:46 +00:00
|
|
|
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
|
2024-11-16 19:23:25 +00:00
|
|
|
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
|
2024-11-16 19:29:20 +00:00
|
|
|
trap 'rm -f $PID_FILE' EXIT
|
2024-11-14 18:53:46 +00:00
|
|
|
|