Cleanup and some POSIX compatibility

This commit is contained in:
Jaidyn Ann 2016-12-23 11:12:07 -06:00
parent d0585eef1f
commit c2f788df92
5 changed files with 123 additions and 128 deletions

View File

@ -12,7 +12,7 @@
interactive_mode=0 interactive_mode=0
channel_file="/tmp/yt-channel_$RANDOM" channel_file="/tmp/$(mktemp -u yt-channel_XXXXX)"
# I really need to work out a better way to take flags. # I really need to work out a better way to take flags.
# This is just awful. # This is just awful.
@ -51,7 +51,7 @@ else
fi fi
function get_input() { function get_input() {
printf "\033[0;32m>>>\033[0m " printf '\033[0;32m>>>\033[0m '
read -r n read -r n
if [ "$n" == "exit" ] if [ "$n" == "exit" ]
@ -82,7 +82,7 @@ function get_input() {
row=0 row=0
# Now for displaying the search results # Now for displaying the search results
temp_file="/tmp/yt-channel_$RANDOM" temp_file="/tmp/$(mktemp -u yt-channel_XXXXX)"
grep "href=\"\/watch?v=" $channel_file | grep "title=" | grep -v \ grep "href=\"\/watch?v=" $channel_file | grep "title=" | grep -v \
"<span class=\"yt-thumb-simple\"" > $temp_file "<span class=\"yt-thumb-simple\"" > $temp_file
@ -96,24 +96,25 @@ do
if [ $row -eq 1 ] if [ $row -eq 1 ]
then then
color='\033[1;34m' #color='\033[1;34m'
color2='\033[1;34m' #color2='\033[1;34m'
printf '\033[1;34m'
row=0 row=0
elif [ $row -eq 0 ] elif [ $row -eq 0 ]
then then
color='\033[1;31m' #color='\033[1;31m'
color2='\033[1;31m' #color2='\033[1;31m'
printf '\033[1;31m'
row=1 row=1
fi fi
if echo "$LINE" | grep "View full playlist" > /dev/null if echo "$LINE" | grep "View full playlist" > /dev/null
then then
type="Playlist" type="Playlist"
printf "${color}$item_num. "
title=$(echo "$LINE" | sed 's/.*title="//' | sed 's/".*//') title=$(echo "$LINE" | sed 's/.*title="//' | sed 's/".*//')
items=$(echo "$LINE" | sed 's/.*View full playlist (//' | sed 's/).*//') items=$(echo "$LINE" | sed 's/.*View full playlist (//' | sed 's/).*//')
echo -e "${color}$title" printf '%s. %s\n' "$item_num" "$title"
echo -e "${color2}$type | $items | $itemid" printf '%s | %s | %s\n' "$type" "$items" "$itemid"
else else
type="Video" type="Video"
duration=$(echo "$LINE" | sed 's/.*Duration: //' | sed 's/\..*//') duration=$(echo "$LINE" | sed 's/.*Duration: //' | sed 's/\..*//')
@ -122,15 +123,15 @@ do
if [ $item_num -lt 10 ] if [ $item_num -lt 10 ]
then then
printf "${color}$item_num. " printf '%s. ' "$item_num"
elif [ $item_num -lt 20 ] elif [ $item_num -lt 20 ]
then then
printf "${color}$item_num. " printf '%s. ' "$item_num"
else else
break break
fi fi
echo -e "${color}$title${color2}" echo "$title"
printf " " printf ' '
i=0 i=0
while [ $i -lt 9 ] while [ $i -lt 9 ]
do do
@ -138,12 +139,12 @@ do
char=$(echo $duration | cut -c$i) char=$(echo $duration | cut -c$i)
if [ -z $char ] if [ -z $char ]
then then
printf " " printf ' '
else else
printf "$char" printf '%s' "$char"
fi fi
done done
printf " | $itemid\n" printf ' | %s\n' "$itemid"
fi fi
done done

View File

@ -43,7 +43,7 @@ function select_option ()
line=$((line+1)) line=$((line+1))
echo "${line}. $i" echo "${line}. $i"
done done
printf "\033[0;32m>>>\033[0m " printf '\033[0;32m>>>\033[0m '
read -r n read -r n
if [ "$n" -le "$line" ]; if [ "$n" -le "$line" ];

View File

@ -14,14 +14,14 @@ then
id="$(echo $1 | sed 's/.*video_id=//')" id="$(echo $1 | sed 's/.*video_id=//')"
elif [ -z $1 ] elif [ -z $1 ]
then then
"No video specified." echo "No video specified."
exit 1 exit 1
else else
id="$1" id="$1"
fi fi
video_file="/tmp/yt-video_$RANDOM" video_file="/tmp/$(mktemp -u yt-video_XXXXX)"
if type "wget" &> /dev/null if type "wget" &> /dev/null
then then
wget -q https://youtube.com/watch?v=$id -O $video_file wget -q https://youtube.com/watch?v=$id -O $video_file

View File

@ -24,7 +24,7 @@ else
fi fi
function get_input() { function get_input() {
printf "\033[0;32m>>>\033[0m " printf '\033[0;32m>>>\033[0m '
read -r n read -r n
if [ "$n" == "exit" ] if [ "$n" == "exit" ]
@ -55,15 +55,15 @@ function get_input() {
# layout, this script'll break immediately, haha. # layout, this script'll break immediately, haha.
# ... But at least we aren't using their API, right? # ... But at least we aren't using their API, right?
search_file="/tmp/yt-search_$RANDOM" search_file="/tmp/$(mktemp -u yt-search_XXXXX)"
st-download https://youtube.com/results?search_query=$query $search_file st-download https://youtube.com/results?search_query=$query $search_file
# Now for displaying the search results # Now for displaying the search results
temp_file="/tmp/yt-result_$RANDOM" temp_file="/tmp/$(mktemp -u yt-result_XXXXX)"
grep "<a href=\"\/watch?v=" $search_file | grep -v "<span class=\"yt-thumb-simple\"" > $temp_file grep "<a href=\"\/watch?v=" $search_file | grep -v "<span class=\"yt-thumb-simple\"" > $temp_file
item_num=0 item_num=0
cat $temp_file | while IFS='' read -r CUR_LINE cat $temp_file | while IFS='' read -r CUR_LINE
do do
item_num=$(($item_num+1)) item_num=$(($item_num+1))
# These tags trip up 'title=' and '" >' queries later on. Strip 'em. # These tags trip up 'title=' and '" >' queries later on. Strip 'em.
@ -73,13 +73,15 @@ do
if [ $row -eq 1 ] if [ $row -eq 1 ]
then then
color='\033[1;34m' #color='\033[1;34m'
color2='\033[1;34m' #color2='\033[1;34m'
printf '\033[1;34m'
row=0 row=0
elif [ $row -eq 0 ] elif [ $row -eq 0 ]
then then
color='\033[1;31m' #color='\033[1;31m'
color2='\033[1;31m' #color2='\033[1;31m'
printf '\033[1;31m'
row=1 row=1
fi fi
@ -88,12 +90,12 @@ do
type="Playlist" type="Playlist"
if [ $interactive_mode -eq 1 ] if [ $interactive_mode -eq 1 ]
then then
printf "${color}$item_num. " printf '%s. ' "$item_num"
fi fi
title=$(echo "$LINE" | sed 's/.*title="//' | sed 's/".*//') title=$(echo "$LINE" | sed 's/.*title="//' | sed 's/".*//')
items=$(echo "$LINE" | sed 's/.*View full playlist (//' | sed 's/).*//') items=$(echo "$LINE" | sed 's/.*View full playlist (//' | sed 's/).*//')
echo -e "${color}$title" printf '%s\n' "$title"
echo -e "${color2}$type | $items | $itemid" printf '%s | %s | %s\n' "$type" "$items" "$itemid"
else else
type="Video" type="Video"
duration=$(echo "$LINE" | sed 's/.*Duration: //' | sed 's/\..*//') duration=$(echo "$LINE" | sed 's/.*Duration: //' | sed 's/\..*//')
@ -110,12 +112,12 @@ do
if [ $item_num -lt 10 ] if [ $item_num -lt 10 ]
then then
printf "${color}$item_num. " printf '%s. ' "$item_num"
else else
printf "${color}$item_num. " printf '%s. ' "$item_num"
fi fi
echo -e "${color}$title${color2}" printf '%s\n' "$title"
printf " " printf ' '
fi fi
i=0 i=0
while [ $i -lt 16 ] while [ $i -lt 16 ]
@ -124,12 +126,12 @@ do
char=$(echo $author | cut -c$i) char=$(echo $author | cut -c$i)
if [ -z $char ] if [ -z $char ]
then then
printf " " printf ' '
else else
printf "$char" printf '%s' "$char"
fi fi
done done
printf " | " printf ' | '
i=0 i=0
while [ $i -lt 5 ] while [ $i -lt 5 ]
do do
@ -137,14 +139,16 @@ do
char=$(echo $duration | cut -c$i) char=$(echo $duration | cut -c$i)
if [ -z $char ] if [ -z $char ]
then then
printf " " printf ' '
else else
printf "$char" printf '%s' "$char"
fi fi
done done
printf " | $itemid\n" printf ' | %s\n' "$itemid"
done done
printf '\033[0m'
if [ $interactive_mode -eq 1 ] if [ $interactive_mode -eq 1 ]
then then
get_input get_input

164
shelltube
View File

@ -6,8 +6,7 @@
# Lisc: ISC # Lisc: ISC
# Main: jadedctrl # Main: jadedctrl
# Desc: Full-shell YT client that # Desc: Full-shell YT client that
# avoids the YT API. # avoids the YT API. #####################
#####################
# Roadmap: # Roadmap:
# v1.0 - [ ] Playlist support # v1.0 - [ ] Playlist support
@ -22,14 +21,14 @@
# [ ] Cli args as commands # [ ] Cli args as commands
function search() { function search() {
output="/tmp/yt-search-$RANDOM" output="/tmp/$(mktemp -u yt-search_XXXXX)"
yt-search -i "$1" $output yt-search -i "$1" $output
selected_video=$(cat $output) selected_video=$(cat $output)
metadata metadata
} }
function channel() { function channel() {
output="/tmp/yt-channel-$RANDOM" output="/tmp/$(mktemp -u yt-channel_XXXXX)"
if echo "$1" | grep "^UC" > /dev/null if echo "$1" | grep "^UC" > /dev/null
then then
yt-channel -ic "$1" $output yt-channel -ic "$1" $output
@ -45,10 +44,10 @@ function interactive() {
} }
function about() { function about() {
echo -e "\033[0;35mShelltube v0.4" printf '\033[0;35mShelltube v0.4.5\n'
echo -e "\033[0;32mDesc: \033[0;34mYT client written in shell." printf '\033[0;32mDesc: \033[0;34mYT client written in shell.\n'
echo -e "\033[0;32mMain: \033[0;34mjadedml@openmailbox.org" printf '\033[0;32mMain: \033[0;34mjadedml@openmailbox.org\n'
echo -e "\033[0;32mLisc: \033[0;34mISC; yt-down GPLv2\033[0m" printf '\033[0;32mLisc: \033[0;34mISC; yt-down GPLv2\033[0m\n'
} }
function metadata() { function metadata() {
@ -64,87 +63,78 @@ function stream() {
} }
function get_input() { function get_input() {
printf "\033[0;34m$selected_video \033[0;32m>>\033[0m " printf '\033[0;34m%s \033[0;32m>>\033[0m ' "$selected_video"
read -r n read -r n
if [ "$n" == "help" ] || [ "$n" == "?" ] case $n in
then help|'?')
help help ; interactive ;;
interactive download|dl)
elif echo "$n" | grep "^search " > /dev/null download ; interactive ;;
then metadata|md)
search "$(echo "$n" | sed 's/search //')" metadata ; interactive ;;
interactive stream|str)
elif echo "$n" | grep "^/" > /dev/null stream ; interactive ;;
then about|'!')
search "$(echo "$n" | sed 's^/^^')" about ; interactive ;;
interactive clear|cls)
elif echo "$n" | grep "^channel " > /dev/null clear ; interactive ;;
then exit)
channel "$(echo "$n" | sed 's/channel //')" rm /tmp/yt-* ; exit ;;
interactive *)
elif echo "$n" | grep "^chan " > /dev/null if [ "$n" ] && [ -z "${n%search *}" ]
then then
channel "$(echo "$n" | sed 's/chan //')" search "${n#search }"
interactive interactive
elif echo "$n" | grep "^video " > /dev/null elif [ "$n" ] && [ -z "${n%/*}" ]
then then
if echo "$n" | grep "youtube.com" search "${n#/}"
then interactive
selected_video="$(echo "$n" | sed 's/.*watch?v=//')" elif [ "$n" ] && [ -z "${n%channel *}" ]
else then
selected_video="$(echo "$n" | sed 's/video //')" channel "${n#channel }"
fi interactive
metadata elif [ "$n" ] && [ -z "${n%chan *}" ]
interactive then
elif echo "$n" | grep "^sel " > /dev/null channel "${n#chan }"
then interactive
if echo "$n" | grep "youtube.com" elif echo "$n" | grep "^video " > /dev/null
then then
selected_video="$(echo "$n" | sed 's/.*watch?v=//')" if echo "$n" | grep "youtube.com"
else then
selected_video="$(echo "$n" | sed 's/sel //')" selected_video="$(echo "$n" | sed 's/.*watch?v=//')"
fi else
metadata selected_video="$(echo "$n" | sed 's/video //')"
interactive fi
elif echo "$n" | grep "^url " > /dev/null metadata
then interactive
if echo "$n" | grep "youtube.com" elif echo "$n" | grep "^sel " > /dev/null
then then
selected_video="$(echo "$n" | sed 's/.*watch?v=//')" if echo "$n" | grep "youtube.com"
else then
selected_video="$(echo "$n" | sed 's/url //')" selected_video="$(echo "$n" | sed 's/.*watch?v=//')"
fi else
metadata selected_video="$(echo "$n" | sed 's/sel //')"
interactive fi
elif [ "$n" == "download" ] || [ "$n" == "dl" ] metadata
then interactive
download elif echo "$n" | grep "^url " > /dev/null
interactive then
elif [ "$n" == "metadata" ] || [ "$n" == "md" ] if echo "$n" | grep "youtube.com"
then then
metadata selected_video="$(echo "$n" | sed 's/.*watch?v=//')"
interactive else
elif [ "$n" == "stream" ] || [ "$n" == "str" ] selected_video="$(echo "$n" | sed 's/url //')"
then fi
stream metadata
interactive interactive
elif [ "$n" == "about" ] || [ "$n" == "!" ] else
then get_input
about fi
interactive ;;
elif [ "$n" == "clear" ] || [ "$n" == "cls" ] esac
then
clear
interactive
elif [ "$n" == "exit" ]
then
rm /tmp/yt-*
exit
else
get_input
fi
} }
function help() { function help() {
echo "about | ! View the about page." echo "about | ! View the about page."
echo "clear | cls Clear the screen." echo "clear | cls Clear the screen."
@ -164,5 +154,5 @@ function help() {
echo "command." echo "command."
} }
echo -e "\033[0;35mShelltube v0.3" printf '\033[0;35mShelltube v0.4.5'
interactive interactive