svw/svw

102 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env sh
stop_file="/tmp/stop-select-videos-to-watch"
cleanup() {
rm "$videos_file" 2>/dev/null
}
trap cleanup INT TERM
usage() {
echo "Usage: $0 [-1] [-h] [-t] [-s]" 1>&2
echo "" 1>&2
printf "This is a program made to copy multimedia URLs into system clibboard
and play them through \$VIDEOPLAYER (if it is not set, mpv).\n" 1>&2
printf "\t-1 Makes videos be played on a single instance of \$VIDEOPLAYER
instead of each one individually.\n" 1>&2
printf "\t-t Toggle svw.\n" 1>&2
printf "\t-s Stop svw from looking for clipboard changes.\n" 1>&2
printf "\t-h (or any other flag) prints this help menu.\n" 1>&2
exit 1
}
svw_toggle() {
if [ ! -e "$stop_file" ]; then
notify-send "Looking" "Select Video to Watch"
svw
else
notify-send "Stop Looking"
svw_stop
fi
}
svw_stop() {
xargs kill < "$stop_file"
rm "$stop_file"
}
svw() {
printf "%b\n" "" | xclip -i -selection clipboard
if [ ! -f $stop_file ]; then
touch $stop_file
else
notify-send "Error" "Already selecting"
exit 1
fi
# While doesnt say to stop, continue
while [ -f "$stop_file" ]
do
clipnotify &
echo "$!" > "$stop_file"
wait
link="$(xclip -o -selection clipboard)"
if [ -n "$link" ] && ! printf "%b\n" "$links" | grep -q "$link"
then
links="$links\n$link"
echo "$link"
fi
done
# Get first link and download
videos_file="/tmp/selected-videos-to-watch$(date +%s)"
if [ -n "$VIDEOPLAYER" ]; then
player=$VIDEOPLAYER
else
player="mpv"
fi
[ -n "$links" ] &&
if [ -n "$single" ]; then
printf "%b\n" "$links" | tail -n +2 | xargs $player
else
printf "%b\n" "$links" | tail -n +2 | xargs -L1 $player
fi
rm "$videos_file" 2>/dev/null
}
while getopts ":1ts" option; do
case "${option}" in
1)
single="1"
;;
t)
command_to_run="svw_toggle"
;;
s)
command_to_run="svw_stop"
;;
*)
command_to_run="usage"
;;
esac
done
shift $((OPTIND-1))
[ -z "$command_to_run" ] && command_to_run="svw"
$command_to_run