Shell scripts minimizing machine time, maximizing tao time.

git clone git://watertao.xyz/programs/tao_shells.git

tao_youtube_query.sh (3526B)


      1 #!/bin/sh
      2 
      3 #
      4 # Query youtube for videos.
      5 #
      6 # Default - fetch list of latest vids of channels listed in config file:
      7 # $HOME/.config/tao/tao_channel_fetch_latest.sites
      8 #
      9 # -c "CHANNEL"
     10 # Fetch videos from CHANNEL
     11 #
     12 # -q "QUERY TERMS"
     13 # Search for yt videos for QUERY TERMS
     14 #
     15 # -a
     16 # Generate of all video results. Default returns 10.
     17 #
     18 # Requires dmenu, tao_url.sh, yt-dlp
     19 # Optionally uses fully functional ps (not busybox ver)
     20 #
     21 
     22 # Defaults (fetching latest from channels list)
     23 typ="d"
     24 l_dflt=10; l=$l_dflt
     25 pts_title="Fetching latest channel videos..."
     26 yt_prnt_opt="%(upload_date)s @ %(playlist_channel)s > %(title)s < ( %(view_count)s ) [ %(duration_string)s ] %(webpage_url)s"
     27 yt_url="-a -"
     28 
     29 export FETCHING_LATEST=1
     30 
     31 dn=/dev/null
     32 cmdf(){ command -v $1 >$dn 2>&1; }
     33 prnt(){ printf "%s\n" "$1"; }
     34 usepts=$(cmdf "ps" && ps -e 2>$dn 1>&2 && printf "1")
     35 
     36 # dmenu command
     37 if [ -z "$DMENU" ]
     38 then
     39 	a="dmenu sxmo_dmenu.sh bemenu rofi"
     40 	for i in $a; do cmdf "$i" && export DMENU="$i" && break; done
     41 fi
     42 dm="$DMENU"
     43 
     44 # term command (using TERMINAL not TERM for st reasons)
     45 if [ -z "$TERMINAL" ]
     46 then
     47 	a="st urxvt xterm sxmo_terminal.sh"
     48 	for i in $a; do cmdf "$i" && export TERMINAL="$i" && break; done
     49 fi
     50 
     51 # optional configs
     52 if [ -z "$DMENU_OPTS" ]
     53 then
     54 	a="$HOME/.config/tao/tao_dmenu.conf" && [ -f "$a" ] && \
     55 		DMENU_OPTS=$(<"$a" sed -n '1p' | tr -d "\n")
     56 	export DMENU_OPTS="${DMENU_OPTS:-""}"
     57 fi
     58 dmo="$DMENU_OPTS"
     59 
     60 # Safety first
     61 cu(){
     62 
     63 	rm -fr "$t" 2>$dn 1>&2
     64 	kill -9 $(ps -oppid,pid | sed -n "s/^[ ]*$$ //p") 2>$dn
     65 	exit 0
     66 }
     67 trap cu INT
     68 
     69 t=$(mktemp)
     70 
     71 dflt_chk()
     72 {
     73 	# All video sites live in the following file
     74 	yt_sites="$HOME/.config/tao/tao_channel_fetch_latest.sites"
     75 	if [ ! -s $yt_sites ]
     76 	then
     77 		<$dn "$dm" -i $dmo -p "<{ $yt_sites file empty. }>"
     78 		exit 0
     79 	fi
     80 }
     81 
     82 f_ytdlp()
     83 {
     84 	f_ytdlp_stdin | yt-dlp --flat-playlist -s \
     85 		--playlist-end $l \
     86 		--extractor-args "youtubetab:approximate_date" \
     87 		$prnt_tty \
     88 		--print "$yt_prnt_opt" \
     89 		-a - 2>$dn >> "$t"
     90 }
     91 
     92 f_ytdlp_stdin()
     93 {
     94 	if [ "$typ" = "q" ]; then
     95 		prnt "ytsearch$l:$q" && return 0
     96 	elif [ "$typ" = "c" ]; then
     97 		prnt "$q" && return 0
     98 	else
     99 		cat "$yt_sites" && return 0
    100 	fi
    101 }
    102 
    103 pts_open()
    104 {
    105 	prnt_tty=""
    106 	if [ "$usepts" = "1" ]
    107 	then
    108 		"$TERMINAL" -t "$pts_title $q" -- tail - 2>$dn &
    109 		a="$!"
    110 		sleep 1
    111 		ttyno=$(ps -e -opid,tty | sed -n "/^ *$a /{
    112 			:st n; /\?$/{b st}
    113 			s/.*\([0-9]\+\)$/\1/p; q}" | \
    114 			tr -d "\n")
    115 
    116 		prnt_tty="--print-to-file playlist:%(title)s /dev/pts/$ttyno"
    117 	fi
    118 }
    119 
    120 runthething()
    121 {
    122 	if [ "$typ" = "d" ]; then
    123 		u=$(<$t grep -v '^N' | sort -r | \
    124 			"$dm" -i $dmo -p "[ Latest videos ]" 2>$dn)
    125 	else
    126 		u=$(<$t "$dm" -i $dmo -p "[ $yt_dm_title > $q ]")
    127 	fi || return 1
    128 
    129 	[ -z "$u" ] && return 1
    130 
    131 	tao_url.sh -r "${u##* }" 2>$dn && return 0
    132 }
    133 
    134 a=; while getopts "l:acq" a
    135 do
    136 	case $a in
    137 		l)	l=$OPTARG
    138 			prnt "$l" | grep -q '^[0-9]\+$' || l=$l_dflt ;;
    139 		a)
    140 			l=10000 ;; 
    141 		c)
    142 			typ="c"
    143 			pts_title="Fetching channel videos >"
    144 			yt_dm_title="channel videos"
    145 			yt_prnt_opt="%(upload_date)s > %(title)s < ( %(view_count)s ) [ %(duration_string)s ] %(webpage_url)s" ;;
    146 		q)
    147 			typ="q"
    148 			pts_title="YT query >"
    149 			yt_dm_title="query results"
    150 			yt_prnt_opt="%(upload_date)s @ %(channel)s > %(title)s < (%(view_count)s) [ %(duration_string)s ] %(webpage_url)s" ;;
    151 	esac
    152 done
    153 shift $(($OPTIND - 1))
    154 q="${1:-""}"
    155 
    156 [ "$typ" = "d" ] && dflt_chk
    157 
    158 # Open a pts for f_ytdlp monitoring
    159 pts_open
    160 
    161 # Query the tube
    162 f_ytdlp
    163 
    164 # If nothing retrieved, go for a walk
    165 [ -s $t ] || cu
    166 
    167 # Otherwise, lets see what we got
    168 while
    169 	runthething
    170 do :; done
    171 cu
    172