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.
master
Miles Alan 4 years ago
parent 68641aab4f
commit f24e946721
  1. 117
      scripts/appscripts/sxmo_youtube.sh

@ -1,45 +1,112 @@
#!/usr/bin/env sh #!/usr/bin/env sh
HISTORY_FILE="$XDG_CONFIG_HOME"/sxmo/youtubehistory.tsv HISTORY_FILE="$XDG_CONFIG_HOME"/sxmo/youtubehistory.tsv
NRESULTS=5
AUDIOONLY=0
menu() { jsonfieldextract() {
pidof "$KEYBOARD" || "$KEYBOARD" & FIELDNAME="$1"
HISTORY="$(tac "$HISTORY_FILE" | nl | sort -uk 2 | sort -k 1 | cut -f 2)" 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
}
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
}
SEARCHTERMS="$( searchmenu() {
printf %b "Close Menu\n$HISTORY" | HISTORY="$(
dmenu -p "Yt Search" -c -l 10 -fn Terminus-20 tac "$HISTORY_FILE" | nl | sort -uk 2 | sort -k 1 | cut -f 2 |
sed "s#^#History: #g"
)" )"
pkill "$KEYBOARD"
[ "Close Menu" = "$SEARCHTERMS" ] && exit 0 while true; do
printf %b "$SEARCHTERMS\n" >> "$HISTORY_FILE" ENTRY="$(
printf %b "
IDIOTRESULTS="$(youtube-cli "$SEARCHTERMS")" Close Menu
FMTRESULTS="$( Show 1 Result $([ "$NRESULTS" = "1" ] && echo "✓")
echo "$IDIOTRESULTS" | Show 5 Results $([ "$NRESULTS" = "5" ] && echo "✓")
grep -Ev '^(Channelid|Atom feed|Channel title|Published|Viewcount|Userid):' | Show 10 Results $([ "$NRESULTS" = "10" ] && echo "✓")
sed -E 's/^(URL|Duration):\s+/\t/g' | Show 20 Results $([ "$NRESULTS" = "20" ] && echo "✓")
tr -d '\n' | $HISTORY
sed 's/===/\n/g' | " |
gawk -F'\t' '{ print $3 " " $1 " " $2}' 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="$( PICKED="$(
printf %b "Close Menu\n$FMTRESULTS" | printf %b "
dmenu -c -l 10 -fn Terminus-20 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
)" )"
[ "Close Menu" = "$PICKED" ] && exit 0
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}')" URL="$(echo "$PICKED" | awk -F " " '{print $NF}')"
st -e mpv -ao=alsa -v --ytdl-format='[height<420]' "$URL" &
fi
done
} }
video() { video() {
menu AUDIOONLY=0
st -e mpv -ao=alsa -v --ytdl-format='[height<420]' "$URL" searchmenu
} }
audio() { audio() {
menu AUDIOONLY=1
st -e mpv -ao=alsa -v --no-video "$URL" searchmenu
} }
$1 $1
Loading…
Cancel
Save