From f24e946721af390fa7fc5e237795c8316d4f366a Mon Sep 17 00:00:00 2001 From: Miles Alan Date: Tue, 25 Aug 2020 20:35:02 -0500 Subject: [PATCH] Rework sxmo_youtube.sh script to use ytdl rather then codemadness youtube-cli Also add in ability to select number of results and fork when opening entries so that the user can select multiple entries. --- scripts/appscripts/sxmo_youtube.sh | 127 ++++++++++++++++++++++------- 1 file changed, 97 insertions(+), 30 deletions(-) diff --git a/scripts/appscripts/sxmo_youtube.sh b/scripts/appscripts/sxmo_youtube.sh index f756164..65b3cbc 100755 --- a/scripts/appscripts/sxmo_youtube.sh +++ b/scripts/appscripts/sxmo_youtube.sh @@ -1,45 +1,112 @@ #!/usr/bin/env sh HISTORY_FILE="$XDG_CONFIG_HOME"/sxmo/youtubehistory.tsv +NRESULTS=5 +AUDIOONLY=0 -menu() { - pidof "$KEYBOARD" || "$KEYBOARD" & - HISTORY="$(tac "$HISTORY_FILE" | nl | sort -uk 2 | sort -k 1 | cut -f 2)" +jsonfieldextract() { + FIELDNAME="$1" + TYPE="$2" + # TODO: be less lazy and use a JSON parser, need to add json2tsv as dep.. + if [ "$TYPE" = "number" ]; then + GREPED="$(grep -oE '"'"$FIELDNAME"'"[ ]*:[ ]*[0-9+]+[ ]*,')" + else + GREPED="$(grep -oE '"'"$FIELDNAME"'"[ ]*:[ ]*"[^"]+"[ ]*,')" + fi + echo "$GREPED" | cut -d: -f2- | tr -d '",' | sed -E 's#^[ ]+##' | head -n1 +} - SEARCHTERMS="$( - printf %b "Close Menu\n$HISTORY" | - dmenu -p "Yt Search" -c -l 10 -fn Terminus-20 - )" - pkill "$KEYBOARD" - [ "Close Menu" = "$SEARCHTERMS" ] && exit 0 - printf %b "$SEARCHTERMS\n" >> "$HISTORY_FILE" - - IDIOTRESULTS="$(youtube-cli "$SEARCHTERMS")" - FMTRESULTS="$( - echo "$IDIOTRESULTS" | - grep -Ev '^(Channelid|Atom feed|Channel title|Published|Viewcount|Userid):' | - sed -E 's/^(URL|Duration):\s+/\t/g' | - tr -d '\n' | - sed 's/===/\n/g' | - gawk -F'\t' '{ print $3 " " $1 " " $2}' - )" +youtubesearch() { + QUERY="$1" + NRESULTS="$2" + RESULTS="$(youtube-dl -j ytsearch"$NRESULTS":"${QUERY}")" + i=1 + while [ $i -lt "$(echo "$NRESULTS" + 1 | bc)" ]; do + TITLE="$(echo "$RESULTS" | awk NR==$i | jsonfieldextract fulltitle string)" + URL="$(echo "$RESULTS" | awk NR==$i | jsonfieldextract webpage_url string)" + DURATION="$(echo "$RESULTS" | awk NR==$i | jsonfieldextract duration number)s" + echo "$DURATION: $TITLE - $URL" + i="$(echo $i + 1 | bc)" + done +} - PICKED="$( - printf %b "Close Menu\n$FMTRESULTS" | - dmenu -c -l 10 -fn Terminus-20 +searchmenu() { + HISTORY="$( + tac "$HISTORY_FILE" | nl | sort -uk 2 | sort -k 1 | cut -f 2 | + sed "s#^#History: #g" )" - [ "Close Menu" = "$PICKED" ] && exit 0 - URL="$(echo "$PICKED" | awk -F " " '{print $NF}')" + while true; do + ENTRY="$( + printf %b " + Close Menu + Show 1 Result $([ "$NRESULTS" = "1" ] && echo "✓") + Show 5 Results $([ "$NRESULTS" = "5" ] && echo "✓") + Show 10 Results $([ "$NRESULTS" = "10" ] && echo "✓") + Show 20 Results $([ "$NRESULTS" = "20" ] && echo "✓") + $HISTORY + " | + xargs -0 echo | + sed '/^[[:space:]]*$/d' | + awk '{$1=$1};1' | + sxmo_dmenu_with_kb.sh -p "Yt Search" -c -l 10 -fn Terminus-20 + )" + + if [ "Close Menu" = "$ENTRY" ]; then + exit 0 + elif echo "$ENTRY" | grep -E "Show [0-9]+ Results*"; then + NRESULTS="$(echo "$ENTRY" | grep -oE "[0-9]+")" + else + ENTRY="$(echo "$ENTRY" | sed 's#^History: ##')" + printf %b "$ENTRY\n" >> "$HISTORY_FILE" + youtubesearch "$ENTRY" "$NRESULTS" | resultsmenu + fi + done +} + +resultsmenu() { + RESULTS="$(cat)" + + while true; do + PICKED="$( + printf %b " + Close Menu\n + Change Search\n + $RESULTS + " | + xargs -0 echo | + sed '/^[[:space:]]*$/d' | + awk '{$1=$1};1' | + dmenu -c -l 10 -p "Yt Results" -fn Terminus-20 + )" + + if [ "Change Search" = "$PICKED" ]; then + return + elif [ "Close Menu" = "$PICKED" ]; then + exit 0 + elif echo $PICKED | grep "Audio & Video or"; then + if [ "$AUDIOONLY" = 0 ]; then + AUDIOONLY=1 + else + AUDIOONLY=0 + fi + elif [ "$AUDIOONLY" = 1 ]; then + URL="$(echo "$PICKED" | awk -F " " '{print $NF}')" + st -e mpv -ao=alsa -v --no-video "$URL" & + else + URL="$(echo "$PICKED" | awk -F " " '{print $NF}')" + st -e mpv -ao=alsa -v --ytdl-format='[height<420]' "$URL" & + fi + done } video() { - menu - st -e mpv -ao=alsa -v --ytdl-format='[height<420]' "$URL" + AUDIOONLY=0 + searchmenu } audio() { - menu - st -e mpv -ao=alsa -v --no-video "$URL" + AUDIOONLY=1 + searchmenu } -$1 +$1 \ No newline at end of file