rc/bin/cliplumber

104 lines
3.2 KiB
Bash
Executable file

#!/bin/sh
# Load helper functions
source "$(dirname "$(rreadlink "$0")")/../lib/utils.sh"
# TODO: detect term or set it globally
TERM_EMU="kitty"
if ! check_exists wl-paste && [ ! -z "$WAYLAND_DISPLAY" ]; then
die "Wayland detected and wl-paste not found"
elif ! check_exists xsel && [ -z "$WAYLAND_DISPLAY" ]; then
die "xsel not found"
fi
# TODO check for xsel/wl-clipboard presence ?
if [ -z "$WAYLAND_DISPLAY" ]; then
clip="$(xsel -b -o)"
else
clip="$(wl-paste)"
fi
urls="$(echo $clip | grep -o 'https\?://[a-zA-Z0-9~#%&_+=,.?@/-]\+')"
# Check the requirements for every option
opts="fav|rot13"
check_exists "$TERM_EMU" "mpv" "yt-dlp" &&
opts="audio-search|$opts"
check_exists "dragon" &&
opts="drag-n-drop|$opts"
# Enable additional features when the clipboard contains an URL
if [ -n "$urls" ] && check_exists "yt-dlp"; then
if echo "$urls" | grep -qF "scribblehub.com"; then
check_exists "lncrawl" && opts="crawl|$opts"
else
check_exists "mpv" && opts="play|low-play|$opts"
check_exists "mpc" && opts="queue|$opts"
opts="download|$opts"
fi
fi
# Load default choice last
check_exists "qrencode" "feh" &&
opts="qr|$opts"
# Split into multiple lines for dmenu
opts="$(printf "%s" "$opts" | sed 's/|/\n/g')"
choice="$(printf "%s" "$opts" | dmenu -p "$(printf "%s" "$clip" | cut -c 1-48)")"
case "$choice" in
"qr")
printf "%s" "$clip" | qrencode -o - | feh -. -Z --force-aliasing --geometry 400x400 -
;;
# Download supports optional folder target
"download"*)
path="$HOME/mus/$(printf "%s" "$choice" | cut -d " " -f 2-)"
mp3dl -d "$path" "$urls"
;;
"crawl")
path="$HOME/books/"
[ -d "$path" ] || mkdir -p "$path"
cd "$path" || exit
out="$(lncrawl --single --all --format epub --filename-only --suppress -s "$urls" | grep -vFe "Input is suppressed" -e "Namespace(")"
errors="$(printf "%s" "$out" | grep -Fe " ❗ ")"
if [ -n "$errors" ]; then
printf "%s" "$errors" | notify_err
else
printf "%s" "$out" | sed -ne '/^NOVEL: /s/NOVEL: \(.*\)/Successfully crawled "\1"/p' | xargs -0 -n1 notify-send
fi
;;
"drag-n-drop")
dragon -x "$clip"
;;
"play")
playvideo "$clip"
;;
"low-play")
playvideo -l "$clip"
;;
"queue")
out="$(mpdurlqueue "$urls" 2>&1)" && notify-send "cliplumber" "stream queued"
printf "%s" "$out" | grep "ERROR:" | notify_err
;;
"audio-search")
"$TERM_EMU" --single-instance mpv --ytdl-format=bestaudio ytdl://ytsearch:"$clip"
;;
"rot13")
if [ -z "$WAYLAND_DISPLAY" ]; then
xsel -b -o | tr 'A-Za-z' 'N-ZA-Mn-za-m' | xsel -b -i
else
wl-copy -n "$(wl-paste | tr 'A-Za-z' 'N-ZA-Mn-za-m')"
fi
;;
# Optional argument to set a description for the fav
"f"*)
desc="$(printf "%s" "$choice" | cut -d " " -s -f 2-)"
if [ -n "$desc" ]; then
printf "# %s\n" "$desc" >> "$HOME/favs"
fi
printf "%s\n" "$urls" >> "$HOME/favs"
;;
*)
printf 'Nope\n'
;;
esac