From 99736427be2e0831a322735a134c08a0ef2a538b Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Fri, 30 Jul 2021 13:14:50 +0100 Subject: [PATCH] BIN: Deletes open-youtube and puts functionality in yt The yt script now accepts a --url option which will take a url as an option like yt --url youtube.com/watch?v=blar or echo youtube.com/watch?v=blar | yt --url It will also use rofi rather than fzf if not being run from a terminal or the --rofi flag is given I have updated the files that used to use open-youtube to reflect this --- bin/.bin/dmenu/ippsec | 4 +- bin/.bin/dmenu/open-youtube | 24 ----- bin/.bin/linkhandler | 6 +- bin/.bin/yt | 113 +++++++++++++++++++----- tridactyl/.config/tridactyl/tridactylrc | 4 +- 5 files changed, 99 insertions(+), 52 deletions(-) delete mode 100755 bin/.bin/dmenu/open-youtube diff --git a/bin/.bin/dmenu/ippsec b/bin/.bin/dmenu/ippsec index 3028a2e0..d300a932 100755 --- a/bin/.bin/dmenu/ippsec +++ b/bin/.bin/dmenu/ippsec @@ -20,5 +20,7 @@ url="$(curl https://ippsec.rocks/dataset.json 2>/dev/null |\ fzf -d ' ' --with-nth '3,2' --header-lines 1 --preview-window 'up:1' --preview 'bash -c '\''buildURL "$0"'\'' {}' |\ buildURL)" -open-youtube "$url" +echo "$url" + +yt "$url" diff --git a/bin/.bin/dmenu/open-youtube b/bin/.bin/dmenu/open-youtube deleted file mode 100755 index 4f946a3b..00000000 --- a/bin/.bin/dmenu/open-youtube +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -isxclient=$( readlink /dev/fd/2 | grep -q 'tty' && [[ -n $DISPLAY ]] ; echo $? ) -if [[ ! -t 2 || $isxclient == "0" ]]; then - DMENU="rofi -dmenu -p" -else - DMENU="fzf --prompt" -fi - -url="$1" - -if [ -z "$url" ]; then - url=$($DMENU 'URL') -fi - -code=$( ( youtube-dl "$url" -F; echo "bb Best of both") | sed -n '/format code/,$ p' | tail -n +2 | $DMENU "Quality " -m | awk '{print $1}') - -code="$(echo "$code" | tr '\n' '+' | sed 's/+$//')" - -case "$code" in - "bb") mpv "$url" --ytdl-format="bestvideo+bestaudio" ;; - *) mpv "$url" --ytdl-format="$code" -esac - diff --git a/bin/.bin/linkhandler b/bin/.bin/linkhandler index e8db8801..c8d602f4 100755 --- a/bin/.bin/linkhandler +++ b/bin/.bin/linkhandler @@ -9,9 +9,9 @@ url="$1" browser="${BROWSER:-firefox-developer-edition}" case "$url" in - *"//invidious.*/"*) open-youtube "$url"; exit 0 ;; - *//*.youtube.*/*) open-youtube "$url"; exit 0 ;; - *"//videos.*"*) open-youtube "$url"; exit 0 ;; + *"//invidious.*/"*) yt --url "$url"; exit 0 ;; + *//*.youtube.*/*) yt --url "$url"; exit 0 ;; + *"//videos.*"*) yt --url "$url"; exit 0 ;; *"//xkcd.com/"*) xkcd "$url"; exit 0 ;; "mailto:"*) neomutt "$url"; exit 0 ;; *) $browser "$url"; exit 0 ;; diff --git a/bin/.bin/yt b/bin/.bin/yt index e763c455..20d1a904 100755 --- a/bin/.bin/yt +++ b/bin/.bin/yt @@ -2,10 +2,13 @@ # Requires: # * curl -# * fzf +# * fzf | rofi # * jq -# * hq # * youtube-dl +# * hq | pup (recomended, although optional) + +rofi="auto" + ttyecho(){ # Same as echo but always to tty echo "$@" > /dev/tty @@ -42,15 +45,30 @@ forceread(){ echo "$answer" } +useRofi(){ + [ "$rofi" = "yes" ] && return 0 + local isxclient=$( readlink /dev/fd/2 | grep -q 'tty' && [[ -n $DISPLAY ]] ; echo $? ) + if [[ ! -t 2 || $isxclient == "0" ]]; then + return 0 + else + return 1 + fi +} + getSearchTerm(){ - # Uses fzf to get a search term - # It will filter suggestions using youtube's autocomplete - export -f getSearchSuggestions - export -f urlencodespecial - echo "" | fzf --bind 'change:reload:bash -c "getSearchSuggestions {q}" || true' \ - --header="Search Suggestions" \ - --phony \ - --height=50% --layout=reverse + if useRofi; then + # Rofi can't reload the suggestions so just display a search box + echo "" | rofi -dmenu -lines 0 + else + # Uses fzf to get a search term + # It will filter suggestions using youtube's autocomplete + export -f getSearchSuggestions + export -f urlencodespecial + echo "" | fzf --bind 'change:reload:bash -c "getSearchSuggestions {q}" || true' \ + --header="Search Suggestions" \ + --phony \ + --height=50% --layout=reverse + fi } getSearchSuggestions(){ @@ -86,18 +104,35 @@ jq '..|.videoRenderer?' | sed '/^null$/d' | } chooseVideo(){ - (echo -e "Title\tChannel\tViews\tTime\tAge\tId" - jq -r '[ .title, .channel, .views, .time, .age, .video ] | @tsv') | - column -t -s" " | sed $'s/ \([^ ]\)/\u00a0\\1/g' | - fzf --header-lines="1" --with-nth=1,2,3,4,5 --delimiter=$'\u00a0' + if useRofi; then + ( jq -r '[ .title, .channel, .views, .time, .age, .video ] | @tsv') | + column -t -s" " | sed $'s/ \([^ ]\)/\u00a0\\1/g' | + rofi -dmenu + else + (echo -e "Title\tChannel\tViews\tTime\tAge\tId" + jq -r '[ .title, .channel, .views, .time, .age, .video ] | @tsv') | + column -t -s" " | sed $'s/ \([^ ]\)/\u00a0\\1/g' | + fzf --header-lines="1" --with-nth=1,2,3,4,5 --delimiter=$'\u00a0' + fi } chooseQuality(){ - videoId="$(cat - | awk -F $'\u00a0' '{print $6}')" - code="$( ( - echo "bb Best of Both" - youtube-dl "$videoId" -F | sed -n '/format code/,$ p' | sed 1d; - ) | fzf --prompt "Quality " -m | cut -d ' ' -f 1 )" + if [ -n "$1" ]; then + videoId="$1" + else + videoId="$(cat - | awk -F $'\u00a0' '{print $6}')" + fi + if useRofi; then + code="$( ( + echo "bb Best of Both" + youtube-dl "$videoId" -F | sed -n '/format code/,$ p' | sed 1d; + ) | rofi -dmenu -prompt "Quality " -m | cut -d ' ' -f 1 )" + else + code="$( ( + echo "bb Best of Both" + youtube-dl "$videoId" -F | sed -n '/format code/,$ p' | sed 1d; + ) | fzf --prompt "Quality " -m | cut -d ' ' -f 1 )" + fi code="$(echo "$code" | tr '\n' '+' | sed 's/+$//')" @@ -107,12 +142,46 @@ chooseQuality(){ esac } + main(){ - local searchTerm="$*" + local url="" + local videoID="" + local searchTerm="" + while [[ "$1" = -?* ]]; do + case "$1" in + -u|--url) + if [ -n "$2" ]; then + url="$2" + shift + else + url="$(cat -)" + fi + shift + ;; + --rofi) + rofi=yes + shift + ;; + esac + done - [ -z "$searchTerm" ] && searchTerm="$(getSearchTerm)" + if [ -n "$url" ]; then + case "$url" in + # If it contains an equals sign, let's assume it's a url + # Get the value of the v parameter + *=*) videoID="$(echo "$url" | grep -Eo '(\?|&)v=[^?&]*' | cut -d '=' -f 2)" ;; + # If there isn't an =, assume it is just the video ID + *) videoID="$url" + esac + chooseQuality "$videoID" + else + searchTerm="$*" + + [ -z "$searchTerm" ] && searchTerm="$(getSearchTerm)" - performSearch "$searchTerm" | extractVideoInfo | chooseVideo | chooseQuality + performSearch "$searchTerm" | extractVideoInfo | chooseVideo | chooseQuality + + fi } main "$@" diff --git a/tridactyl/.config/tridactyl/tridactylrc b/tridactyl/.config/tridactyl/tridactylrc index a2d5afd5..035778fb 100644 --- a/tridactyl/.config/tridactyl/tridactylrc +++ b/tridactyl/.config/tridactyl/tridactylrc @@ -45,8 +45,8 @@ set searchurls.audio https://www.google.com/search?hl=en&ei=cSj3X5L0HYXykwXRpKvY """""""""""""" command withUrl composite get_current_url | -command openvid withUrl !s open-youtube -command openvidLink composite hint -pipe a[href]:not([display="none"]):not([href=""]) href | !s open-youtube +command openvid withUrl !s yt --url +command openvidLink composite hint -pipe a[href]:not([display="none"]):not([href=""]) href | !s yt --url command testclickjacking withUrl !s $HOME/.config/tridactyl/scripts/clickjacking | nativeopen command openwithbrave withUrl !s /usr/bin/brave