2016-12-11 19:33:57 -06:00
|
|
|
#####################
|
|
|
|
# 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"
|
|
|
|
|
2016-12-23 23:15:53 -06:00
|
|
|
if [ -e $HOME/.config/shelltube ]
|
|
|
|
then
|
|
|
|
. $HOME/.config/shelltube
|
|
|
|
else
|
|
|
|
results=21
|
|
|
|
fi
|
|
|
|
|
2017-01-05 22:38:38 -06:00
|
|
|
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
|
|
|
|
|
2016-12-11 19:33:57 -06:00
|
|
|
row=0
|
|
|
|
|
|
|
|
if [ "$1" == "-i" ]
|
|
|
|
then
|
|
|
|
interactive_mode=1
|
|
|
|
query="$(echo "$2" | sed 's/ /+/g')"
|
|
|
|
output="$3"
|
|
|
|
else
|
2016-12-22 19:14:27 -06:00
|
|
|
interactive_mode=0
|
|
|
|
query="$(echo "$1" | sed 's/ /+/g')"
|
2016-12-11 19:33:57 -06:00
|
|
|
fi
|
|
|
|
|
|
|
|
function get_input() {
|
2016-12-23 11:12:07 -06:00
|
|
|
printf '\033[0;32m>>>\033[0m '
|
2016-12-11 19:33:57 -06:00
|
|
|
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
|
2017-01-07 11:07:16 -06:00
|
|
|
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/.*<a href="\/watch?v=//' | sed 's/".*//' > $output
|
|
|
|
fi
|
2016-12-11 19:33:57 -06:00
|
|
|
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?
|
|
|
|
|
2016-12-23 13:28:20 -06:00
|
|
|
search_file="/tmp/$(mktemp -u yt-search_XXXXXX)"
|
2016-12-11 19:33:57 -06:00
|
|
|
|
2016-12-22 19:14:27 -06:00
|
|
|
st-download https://youtube.com/results?search_query=$query $search_file
|
2016-12-11 19:33:57 -06:00
|
|
|
|
|
|
|
# Now for displaying the search results
|
2016-12-23 13:28:20 -06:00
|
|
|
temp_file="/tmp/$(mktemp -u yt-result_XXXXXX)"
|
2016-12-11 19:33:57 -06:00
|
|
|
grep "<a href=\"\/watch?v=" $search_file | grep -v "<span class=\"yt-thumb-simple\"" > $temp_file
|
|
|
|
item_num=0
|
2016-12-23 11:12:07 -06:00
|
|
|
cat $temp_file | while IFS='' read -r CUR_LINE
|
2016-12-11 19:33:57 -06:00
|
|
|
do
|
|
|
|
item_num=$(($item_num+1))
|
|
|
|
# These tags trip up 'title=' and '" >' queries later on. Strip 'em.
|
|
|
|
LINE="$(echo $CUR_LINE | sed 's/<span class=\"yt-badge \" >.*//')"
|
|
|
|
LINE="$(echo $LINE | sed 's/views<\/li>.*//')"
|
|
|
|
LINE="$(echo $LINE | sed 's/title="Verified"//')"
|
|
|
|
|
|
|
|
if [ $row -eq 1 ]
|
|
|
|
then
|
2016-12-23 11:12:07 -06:00
|
|
|
#color='\033[1;34m'
|
|
|
|
#color2='\033[1;34m'
|
|
|
|
printf '\033[1;34m'
|
2016-12-11 19:33:57 -06:00
|
|
|
row=0
|
|
|
|
elif [ $row -eq 0 ]
|
|
|
|
then
|
2016-12-23 11:12:07 -06:00
|
|
|
#color='\033[1;31m'
|
|
|
|
#color2='\033[1;31m'
|
|
|
|
printf '\033[1;31m'
|
2016-12-11 19:33:57 -06:00
|
|
|
row=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if echo "$LINE" | grep "View full playlist" > /dev/null
|
|
|
|
then
|
|
|
|
type="Playlist"
|
2016-12-23 23:15:53 -06:00
|
|
|
title=$(echo "$LINE" | sed 's/.*title="//' | sed 's/".*//')
|
|
|
|
items=$(echo "$LINE" | sed 's/.*View full playlist (//' | sed 's/).*//')
|
2017-01-07 11:07:16 -06:00
|
|
|
itemid=$(echo "$LINE" | sed 's/.*&list=//' | sed 's/".*//')
|
2016-12-23 23:15:53 -06:00
|
|
|
if [ $item_num -ge $((results+1)) ]
|
|
|
|
then
|
|
|
|
break
|
|
|
|
elif [ $item_num -lt 10 ]
|
2016-12-11 19:33:57 -06:00
|
|
|
then
|
2016-12-23 23:15:53 -06:00
|
|
|
printf '%s. ' "$item_num"
|
|
|
|
else
|
2016-12-23 11:12:07 -06:00
|
|
|
printf '%s. ' "$item_num"
|
2016-12-11 19:33:57 -06:00
|
|
|
fi
|
2016-12-23 11:12:07 -06:00
|
|
|
printf '%s\n' "$title"
|
2016-12-23 23:15:53 -06:00
|
|
|
printf ' '
|
2017-01-07 11:07:16 -06:00
|
|
|
printf '%s | %s | %s\n' "$type" "$items" "$itemid"
|
2016-12-11 19:33:57 -06:00
|
|
|
else
|
|
|
|
type="Video"
|
|
|
|
duration=$(echo "$LINE" | sed 's/.*Duration: //' | sed 's/\..*//')
|
|
|
|
itemid=$(echo "$LINE" | sed 's/.*<a href="\/watch?v=//' | sed 's/".*//')
|
|
|
|
title=$(echo "$LINE" | sed 's/.* title="//' | sed 's/".*//')
|
|
|
|
if echo "$LINE" | grep /channel/ > /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
|
|
|
|
|
2016-12-23 23:15:53 -06:00
|
|
|
if [ $item_num -ge $((results+1)) ]
|
|
|
|
then
|
|
|
|
break
|
|
|
|
elif [ $item_num -lt 10 ]
|
2016-12-22 19:14:27 -06:00
|
|
|
then
|
2016-12-23 11:12:07 -06:00
|
|
|
printf '%s. ' "$item_num"
|
2016-12-22 19:14:27 -06:00
|
|
|
else
|
2016-12-23 11:12:07 -06:00
|
|
|
printf '%s. ' "$item_num"
|
2016-12-11 19:33:57 -06:00
|
|
|
fi
|
2016-12-23 11:12:07 -06:00
|
|
|
printf '%s\n' "$title"
|
|
|
|
printf ' '
|
2017-01-07 11:07:16 -06:00
|
|
|
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"
|
2016-12-11 19:33:57 -06:00
|
|
|
fi
|
2017-01-07 11:07:16 -06:00
|
|
|
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
|
2016-12-11 19:33:57 -06:00
|
|
|
done
|
|
|
|
|
2016-12-23 11:12:07 -06:00
|
|
|
printf '\033[0m'
|
|
|
|
|
2016-12-11 19:33:57 -06:00
|
|
|
if [ $interactive_mode -eq 1 ]
|
|
|
|
then
|
|
|
|
get_input
|
|
|
|
fi
|