2019-11-20 21:58:02 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# TODO: detect term or set it globally
|
|
|
|
TERM_EMU="kitty"
|
|
|
|
|
|
|
|
die() {
|
|
|
|
notify-send "$@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
check_exists() {
|
2019-11-20 23:55:19 +00:00
|
|
|
for c in $@; do
|
|
|
|
which "$c" > /dev/null 2>&1 || return 1
|
|
|
|
done
|
2019-11-20 21:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ! which wl-paste > /dev/null && [ ! -z "$WAYLAND_DISPLAY" ]; then
|
|
|
|
die "Wayland detected and wl-paste not found"
|
|
|
|
elif ! which xsel > /dev/null && [ -z "$WAYLAND_DISPLAY" ]; then
|
|
|
|
die "xsel not found"
|
|
|
|
fi
|
|
|
|
|
2019-12-25 16:03:17 +00:00
|
|
|
# TODO check for xsel/wl-clipboard presence ?
|
2019-11-20 21:58:02 +00:00
|
|
|
if [ -z "$WAYLAND_DISPLAY" ]; then
|
|
|
|
clip="$(xsel -b -o)"
|
|
|
|
else
|
|
|
|
clip="$(wl-paste)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
urls="$(echo $clip | grep -o 'https\?://[a-zA-Z0-9~#%&_+=,.?/-]\+')"
|
2019-11-20 23:55:19 +00:00
|
|
|
|
|
|
|
# Check the requirements for every option
|
2019-11-21 00:06:23 +00:00
|
|
|
opts="rot13"
|
2019-11-20 23:55:19 +00:00
|
|
|
check_exists "$TERM_EMU" "mpv" "youtube-dl" &&
|
2020-07-10 04:13:36 +00:00
|
|
|
opts="audio-search|$opts"
|
2019-11-20 23:55:19 +00:00
|
|
|
check_exists "dragon" &&
|
2020-07-10 04:13:36 +00:00
|
|
|
opts="drag-n-drop|$opts"
|
2019-11-20 23:58:17 +00:00
|
|
|
# Enable additional features when the clipboard contains an URL
|
2019-11-20 23:55:19 +00:00
|
|
|
if [ -n "$urls" ] && check_exists "youtube-dl"; then
|
2020-07-10 04:13:36 +00:00
|
|
|
if echo "$urls" | grep -qF "scribblehub.com"; then
|
|
|
|
check_exists "lncrawl" && opts="crawl|$opts"
|
|
|
|
else
|
|
|
|
check_exists "mpv" && opts="play|$opts"
|
|
|
|
opts="download|$opts"
|
|
|
|
fi
|
2019-11-20 21:58:02 +00:00
|
|
|
fi
|
2019-11-20 23:58:17 +00:00
|
|
|
# Load default choice last
|
2019-11-20 23:55:19 +00:00
|
|
|
check_exists "qrencode" "feh" &&
|
2020-07-10 04:13:36 +00:00
|
|
|
opts="qr|$opts"
|
|
|
|
|
|
|
|
# Split into multiple lines for dmenu
|
|
|
|
opts="$(printf "%s" "$opts" | sed 's/|/\n/g')"
|
2019-11-20 21:58:02 +00:00
|
|
|
|
|
|
|
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-)"
|
|
|
|
[ -d "$path" ] || mkdir -p "$path"
|
|
|
|
cd "$path" || exit
|
2020-07-10 04:13:36 +00:00
|
|
|
youtube-dl -x --audio-format mp3 --no-playlist -o "%(title)s.%(ext)s" "$urls" 2>&1 | grep 'ERROR:' | xargs -n1 -d "\n" notify-send
|
2019-11-20 21:58:02 +00:00
|
|
|
#notify-send "Error while downloading: $clip"
|
|
|
|
;;
|
2020-07-10 04:13:36 +00:00
|
|
|
"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" | xargs -n1 -d "\n" notify-send
|
|
|
|
else
|
|
|
|
printf "%s" "$out" | sed -ne '/^NOVEL: /s/NOVEL: \(.*\)/Successfully crawled "\1"/p' | xargs -0 -n1 notify-send
|
|
|
|
fi
|
|
|
|
;;
|
2019-11-20 23:55:19 +00:00
|
|
|
"drag-n-drop"*)
|
|
|
|
dragon -x "$clip"
|
|
|
|
;;
|
2019-11-20 21:58:02 +00:00
|
|
|
"play")
|
|
|
|
mpv --ytdl-format='bestvideo[height<=?720]+bestaudio/best' "$clip" 2>&1 | grep 'ERROR:' | xargs -n1 -d "\n" notify-send
|
|
|
|
|
|
|
|
;;
|
2019-11-20 23:55:19 +00:00
|
|
|
"audio-search")
|
2019-11-20 21:58:02 +00:00
|
|
|
"$TERM_EMU" --single-instance mpv --ytdl-format=bestaudio ytdl://ytsearch:"$clip"
|
|
|
|
;;
|
2019-11-21 00:06:23 +00:00
|
|
|
"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
|
|
|
|
;;
|
2019-11-20 21:58:02 +00:00
|
|
|
*)
|
|
|
|
printf 'Nope\n'
|
|
|
|
;;
|
|
|
|
esac
|