Also add in ability to select number of results and fork when opening entries so that the user can select multiple entries.master
parent
68641aab4f
commit
f24e946721
1 changed files with 97 additions and 30 deletions
@ -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 |
||||||
|
} |
||||||
|
|
||||||
SEARCHTERMS="$( |
youtubesearch() { |
||||||
printf %b "Close Menu\n$HISTORY" | |
QUERY="$1" |
||||||
dmenu -p "Yt Search" -c -l 10 -fn Terminus-20 |
NRESULTS="$2" |
||||||
)" |
RESULTS="$(youtube-dl -j ytsearch"$NRESULTS":"${QUERY}")" |
||||||
pkill "$KEYBOARD" |
i=1 |
||||||
[ "Close Menu" = "$SEARCHTERMS" ] && exit 0 |
while [ $i -lt "$(echo "$NRESULTS" + 1 | bc)" ]; do |
||||||
printf %b "$SEARCHTERMS\n" >> "$HISTORY_FILE" |
TITLE="$(echo "$RESULTS" | awk NR==$i | jsonfieldextract fulltitle string)" |
||||||
|
URL="$(echo "$RESULTS" | awk NR==$i | jsonfieldextract webpage_url string)" |
||||||
IDIOTRESULTS="$(youtube-cli "$SEARCHTERMS")" |
DURATION="$(echo "$RESULTS" | awk NR==$i | jsonfieldextract duration number)s" |
||||||
FMTRESULTS="$( |
echo "$DURATION: $TITLE - $URL" |
||||||
echo "$IDIOTRESULTS" | |
i="$(echo $i + 1 | bc)" |
||||||
grep -Ev '^(Channelid|Atom feed|Channel title|Published|Viewcount|Userid):' | |
done |
||||||
sed -E 's/^(URL|Duration):\s+/\t/g' | |
} |
||||||
tr -d '\n' | |
|
||||||
sed 's/===/\n/g' | |
|
||||||
gawk -F'\t' '{ print $3 " " $1 " " $2}' |
|
||||||
)" |
|
||||||
|
|
||||||
PICKED="$( |
searchmenu() { |
||||||
printf %b "Close Menu\n$FMTRESULTS" | |
HISTORY="$( |
||||||
dmenu -c -l 10 -fn Terminus-20 |
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() { |
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…
Reference in new issue