diff options
-rwxr-xr-x | yt.sh (renamed from yt-player.sh) | 24 |
1 files changed, 12 insertions, 12 deletions
@@ -4,6 +4,7 @@ TERMINAL=alacritty DOWNLOAD_DIR=~/videos/youtube PAGER="rofi -location 2 -width 41 -e" +YOUTUBE_REGEX=".*((https?\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+)$" ## Exit codes @@ -34,14 +35,7 @@ get_action_multi() { ## Check if url is a youtube url check_if_youtube() { url="$1" - [ -z $url ] && return 1; - - count=$(echo "$1" | \ - grep -c -E "^(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$" - ) - - [ "$count" -eq 1 ] && return 0; - return 1; + echo $url | grep -qE "$YOUTUBE_REGEX" && return 0 || return 1 } @@ -53,10 +47,9 @@ prompt_url() { ## Grab url from clipboard/primary selection, else get via prompt grab_url() { - local url - - if url=$(xclip -o -selection clipboard) && check_if_youtube $url; then - elif url=$(xclip -o) && check_if_youtube $url; then + if url=$(grab_url_stdin) && check_if_youtube $url; then + elif url=$(xclip -o -selection clipboard 2>/dev/null || echo) && check_if_youtube $url; then + elif url=$(xclip -o 2>/dev/null || echo) && check_if_youtube $url; then elif url=$(prompt_url) && check_if_youtube $url; then else exit $NO_YOUTUBE_URL @@ -66,6 +59,12 @@ grab_url() { } +## Extract URL from stdin +grab_url_stdin() { + sed -nE "s/$YOUTUBE_REGEX/\1/p" +} + + ## Get title, description, and duration of URL get_info() { url="$1" @@ -133,6 +132,7 @@ print_info() { ## Main mkdir -p "$DOWNLOAD_DIR" url=$(grab_url) + case "$(get_action_single)" in 5) print_info $url |