##################### # Name: yt-result.sh # Date: 2016-12-10 # Lisc: ISC # Main: jadedctrl # Desc: Search YT videos and display # in easy-to-read and easy-to # -parse results ##################### # Usage: yt-search.sh "$search_string" if [ -e $HOME/.config/shelltube ] then . $HOME/.config/shelltube else results=21 fi if [ -e "./lib/yt-meta" ] then alias yt-channel="./lib/yt-channel" alias yt-search="./lib/yt-down" alias yt-meta="./lib/yt-meta" alias yt-search="./lib/yt-search" alias yt-down="./lib/yt-channel" alias st-download="./lib/st-download" alias st-video="./lib/st-video" fi row=0 if [ "$1" == "-i" ] then interactive_mode=1 query="$(echo "$2" | sed 's/ /+/g')" output="$3" else interactive_mode=0 query="$(echo "$1" | sed 's/ /+/g')" fi function get_input() { printf '\033[0;32m>>>\033[0m ' read -r n if [ "$n" == "exit" ] then exit fi test $n -ge 0 &> /dev/null if [ $? -gt 1 ] then echo "Bad input, mate. Type in a valid number or 'exit'." get_input elif [ $n -gt 20 ] then echo "Out of range. Type in a valid number or 'exit'." get_input elif [ $n -gt 0 ] && [ $n -lt 20 ] then if sed -n ${n}p $temp_file | grep "View full playlist" > /dev/null then sed -n ${n}p $temp_file | sed 's/.*&list=//' | sed 's/".*//' > $output else sed -n ${n}p $temp_file | sed 's/.* $output fi else echo "Bad input, mate. Type in a valid number or 'exit'." get_input fi } # If Google adds any extra features or changes the webpage # layout, this script'll break immediately, haha. # ... But at least we aren't using their API, right? search_file="/tmp/$(mktemp -u yt-search_XXXXXX)" st-download https://youtube.com/results?search_query=$query $search_file # Now for displaying the search results temp_file="/tmp/$(mktemp -u yt-result_XXXXXX)" grep " $temp_file item_num=0 cat $temp_file | while IFS='' read -r CUR_LINE do item_num=$(($item_num+1)) # These tags trip up 'title=' and '" >' queries later on. Strip 'em. LINE="$(echo $CUR_LINE | sed 's/.*//')" LINE="$(echo $LINE | sed 's/views<\/li>.*//')" LINE="$(echo $LINE | sed 's/title="Verified"//')" if [ $row -eq 1 ] then #color='\033[1;34m' #color2='\033[1;34m' printf '\033[1;34m' row=0 elif [ $row -eq 0 ] then #color='\033[1;31m' #color2='\033[1;31m' printf '\033[1;31m' row=1 fi if echo "$LINE" | grep "View full playlist" > /dev/null then type="Playlist" title=$(echo "$LINE" | sed 's/.*title="//' | sed 's/".*//') items=$(echo "$LINE" | sed 's/.*View full playlist (//' | sed 's/).*//') itemid=$(echo "$LINE" | sed 's/.*&list=//' | sed 's/".*//') if [ $item_num -ge $((results+1)) ] then break elif [ $item_num -lt 10 ] then printf '%s. ' "$item_num" else printf '%s. ' "$item_num" fi printf '%s\n' "$title" printf ' ' printf '%s | %s | %s\n' "$type" "$items" "$itemid" else type="Video" duration=$(echo "$LINE" | sed 's/.*Duration: //' | sed 's/\..*//') itemid=$(echo "$LINE" | sed 's/.* /dev/null then # For /channel/ users author=$(echo "$LINE" | sed 's/.*" >//' | sed 's/<\/a>.*//') else # For /user/ users author=$(echo "$LINE" | sed 's/.*\/user\///' | sed 's/".*//') fi if [ $item_num -ge $((results+1)) ] then break elif [ $item_num -lt 10 ] then printf '%s. ' "$item_num" else printf '%s. ' "$item_num" fi printf '%s\n' "$title" printf ' ' i=0 while [ $i -lt 16 ] do i=$((i+1)) char=$(echo $author | cut -c$i) if [ -z $char ] then printf ' ' else printf '%s' "$char" fi done printf ' | ' i=0 while [ $i -lt 5 ] do i=$((i+1)) char=$(echo $duration | cut -c$i) if [ -z $char ] then printf ' ' else printf '%s' "$char" fi done printf ' | %s\n' "$itemid" fi done printf '\033[0m' if [ $interactive_mode -eq 1 ] then get_input fi