mpv-add/mpv-add.sh

30 lines
791 B
Bash
Raw Normal View History

2025-01-29 15:03:31 -08:00
#!/usr/bin/env bash
set -Eeuo pipefail
URL="${1:-$(wl-paste -p)}"
MPV_SOCKET=/tmp/mpvsocket
2025-02-25 01:32:34 -08:00
ICON_PATH="$HOME/.local/share/icons/Magna-Glassy-Dark-Icons/apps/48/mpv.svg"
2025-02-25 01:51:31 -08:00
TITLE="mpv-add.sh"
2025-01-29 15:03:31 -08:00
if [[ -z "$URL" ]]; then
2025-02-25 01:51:31 -08:00
notify-send -i "$ICON_PATH" "$TITLE" "No URL provided"
2025-02-25 01:32:34 -08:00
exit 1
2025-01-29 15:03:31 -08:00
fi
if ! [[ -f "$URL" ]] && ! yt-dlp --simulate "$URL"; then
2025-02-25 01:51:31 -08:00
notify-send -i "$ICON_PATH" "$TITLE" "Invalid URL"
2025-02-25 01:32:34 -08:00
exit 1
2025-01-29 15:03:31 -08:00
fi
2025-02-25 01:32:34 -08:00
if ! pgrep -x mpv &> /dev/null; then
mpv "$URL" &> /dev/null &
2025-02-25 01:51:31 -08:00
notify-send -i "$ICON_PATH" "$TITLE" "Playing $URL"
2025-01-29 15:03:31 -08:00
else
2025-02-25 01:32:34 -08:00
if echo "{ \"command\": [\"script-message\", \"add_to_queue\", \"$URL\" ] }" | socat - "$MPV_SOCKET" &> /dev/null; then
2025-02-25 01:51:31 -08:00
notify-send -i "$ICON_PATH" "$TITLE" "Added $URL to queue"
2025-02-25 01:32:34 -08:00
else
2025-02-25 01:51:31 -08:00
notify-send -i "$ICON_PATH" "$TITLE" "Failed to add $URL to queue"
2025-02-25 01:32:34 -08:00
fi
2025-01-29 15:03:31 -08:00
fi