From c2f788df9230e3c549c91bdc7bc65831b449ec59 Mon Sep 17 00:00:00 2001 From: Jade Levesque Date: Fri, 23 Dec 2016 11:12:07 -0600 Subject: [PATCH] Cleanup and some POSIX compatibility --- lib/yt-channel | 35 ++++++----- lib/yt-down | 2 +- lib/yt-meta | 4 +- lib/yt-search | 46 +++++++------- shelltube | 164 +++++++++++++++++++++++-------------------------- 5 files changed, 123 insertions(+), 128 deletions(-) diff --git a/lib/yt-channel b/lib/yt-channel index a29cf12..44ba51b 100755 --- a/lib/yt-channel +++ b/lib/yt-channel @@ -12,7 +12,7 @@ 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. # This is just awful. @@ -51,7 +51,7 @@ else fi function get_input() { - printf "\033[0;32m>>>\033[0m " + printf '\033[0;32m>>>\033[0m ' read -r n if [ "$n" == "exit" ] @@ -82,7 +82,7 @@ function get_input() { row=0 # 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 \ " $temp_file @@ -96,24 +96,25 @@ do if [ $row -eq 1 ] then - color='\033[1;34m' - color2='\033[1;34m' + #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' + #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" - printf "${color}$item_num. " title=$(echo "$LINE" | sed 's/.*title="//' | sed 's/".*//') items=$(echo "$LINE" | sed 's/.*View full playlist (//' | sed 's/).*//') - echo -e "${color}$title" - echo -e "${color2}$type | $items | $itemid" + printf '%s. %s\n' "$item_num" "$title" + printf '%s | %s | %s\n' "$type" "$items" "$itemid" else type="Video" duration=$(echo "$LINE" | sed 's/.*Duration: //' | sed 's/\..*//') @@ -122,15 +123,15 @@ do if [ $item_num -lt 10 ] then - printf "${color}$item_num. " + printf '%s. ' "$item_num" elif [ $item_num -lt 20 ] then - printf "${color}$item_num. " + printf '%s. ' "$item_num" else break fi - echo -e "${color}$title${color2}" - printf " " + echo "$title" + printf ' ' i=0 while [ $i -lt 9 ] do @@ -138,12 +139,12 @@ do char=$(echo $duration | cut -c$i) if [ -z $char ] then - printf " " + printf ' ' else - printf "$char" + printf '%s' "$char" fi done - printf " | $itemid\n" + printf ' | %s\n' "$itemid" fi done diff --git a/lib/yt-down b/lib/yt-down index 2bd86f7..2612981 100755 --- a/lib/yt-down +++ b/lib/yt-down @@ -43,7 +43,7 @@ function select_option () line=$((line+1)) echo "${line}. $i" done - printf "\033[0;32m>>>\033[0m " + printf '\033[0;32m>>>\033[0m ' read -r n if [ "$n" -le "$line" ]; diff --git a/lib/yt-meta b/lib/yt-meta index a4a355d..9101d90 100755 --- a/lib/yt-meta +++ b/lib/yt-meta @@ -14,14 +14,14 @@ then id="$(echo $1 | sed 's/.*video_id=//')" elif [ -z $1 ] then - "No video specified." + echo "No video specified." exit 1 else id="$1" fi -video_file="/tmp/yt-video_$RANDOM" +video_file="/tmp/$(mktemp -u yt-video_XXXXX)" if type "wget" &> /dev/null then wget -q https://youtube.com/watch?v=$id -O $video_file diff --git a/lib/yt-search b/lib/yt-search index 727d7bd..0b11ff6 100755 --- a/lib/yt-search +++ b/lib/yt-search @@ -24,7 +24,7 @@ else fi function get_input() { - printf "\033[0;32m>>>\033[0m " + printf '\033[0;32m>>>\033[0m ' read -r n if [ "$n" == "exit" ] @@ -55,15 +55,15 @@ function get_input() { # layout, this script'll break immediately, haha. # ... 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 # Now for displaying the search results -temp_file="/tmp/yt-result_$RANDOM" +temp_file="/tmp/$(mktemp -u yt-result_XXXXX)" grep " $temp_file item_num=0 -cat $temp_file | while IFS='' read -r CUR_LINE +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. @@ -73,13 +73,15 @@ do if [ $row -eq 1 ] then - color='\033[1;34m' - color2='\033[1;34m' + #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' + #color='\033[1;31m' + #color2='\033[1;31m' + printf '\033[1;31m' row=1 fi @@ -88,12 +90,12 @@ do type="Playlist" if [ $interactive_mode -eq 1 ] then - printf "${color}$item_num. " + printf '%s. ' "$item_num" fi title=$(echo "$LINE" | sed 's/.*title="//' | sed 's/".*//') items=$(echo "$LINE" | sed 's/.*View full playlist (//' | sed 's/).*//') - echo -e "${color}$title" - echo -e "${color2}$type | $items | $itemid" + printf '%s\n' "$title" + printf '%s | %s | %s\n' "$type" "$items" "$itemid" else type="Video" duration=$(echo "$LINE" | sed 's/.*Duration: //' | sed 's/\..*//') @@ -110,12 +112,12 @@ do if [ $item_num -lt 10 ] then - printf "${color}$item_num. " + printf '%s. ' "$item_num" else - printf "${color}$item_num. " + printf '%s. ' "$item_num" fi - echo -e "${color}$title${color2}" - printf " " + printf '%s\n' "$title" + printf ' ' fi i=0 while [ $i -lt 16 ] @@ -124,12 +126,12 @@ do char=$(echo $author | cut -c$i) if [ -z $char ] then - printf " " + printf ' ' else - printf "$char" + printf '%s' "$char" fi done - printf " | " + printf ' | ' i=0 while [ $i -lt 5 ] do @@ -137,14 +139,16 @@ do char=$(echo $duration | cut -c$i) if [ -z $char ] then - printf " " + printf ' ' else - printf "$char" + printf '%s' "$char" fi done - printf " | $itemid\n" + printf ' | %s\n' "$itemid" done +printf '\033[0m' + if [ $interactive_mode -eq 1 ] then get_input diff --git a/shelltube b/shelltube index 36cea5e..37dd873 100755 --- a/shelltube +++ b/shelltube @@ -6,8 +6,7 @@ # Lisc: ISC # Main: jadedctrl # Desc: Full-shell YT client that -# avoids the YT API. -##################### +# avoids the YT API. ##################### # Roadmap: # v1.0 - [ ] Playlist support @@ -22,14 +21,14 @@ # [ ] Cli args as commands function search() { - output="/tmp/yt-search-$RANDOM" + output="/tmp/$(mktemp -u yt-search_XXXXX)" yt-search -i "$1" $output selected_video=$(cat $output) metadata } function channel() { - output="/tmp/yt-channel-$RANDOM" + output="/tmp/$(mktemp -u yt-channel_XXXXX)" if echo "$1" | grep "^UC" > /dev/null then yt-channel -ic "$1" $output @@ -45,10 +44,10 @@ function interactive() { } function about() { - echo -e "\033[0;35mShelltube v0.4" - echo -e "\033[0;32mDesc: \033[0;34mYT client written in shell." - echo -e "\033[0;32mMain: \033[0;34mjadedml@openmailbox.org" - echo -e "\033[0;32mLisc: \033[0;34mISC; yt-down GPLv2\033[0m" + printf '\033[0;35mShelltube v0.4.5\n' + printf '\033[0;32mDesc: \033[0;34mYT client written in shell.\n' + printf '\033[0;32mMain: \033[0;34mjadedml@openmailbox.org\n' + printf '\033[0;32mLisc: \033[0;34mISC; yt-down GPLv2\033[0m\n' } function metadata() { @@ -64,87 +63,78 @@ function stream() { } 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 - if [ "$n" == "help" ] || [ "$n" == "?" ] - then - help - interactive - elif echo "$n" | grep "^search " > /dev/null - then - search "$(echo "$n" | sed 's/search //')" - interactive - elif echo "$n" | grep "^/" > /dev/null - then - search "$(echo "$n" | sed 's^/^^')" - interactive - elif echo "$n" | grep "^channel " > /dev/null - then - channel "$(echo "$n" | sed 's/channel //')" - interactive - elif echo "$n" | grep "^chan " > /dev/null - then - channel "$(echo "$n" | sed 's/chan //')" - interactive - elif echo "$n" | grep "^video " > /dev/null - then - if echo "$n" | grep "youtube.com" - then - selected_video="$(echo "$n" | sed 's/.*watch?v=//')" - else - selected_video="$(echo "$n" | sed 's/video //')" - fi - metadata - interactive - elif echo "$n" | grep "^sel " > /dev/null - then - if echo "$n" | grep "youtube.com" - then - selected_video="$(echo "$n" | sed 's/.*watch?v=//')" - else - selected_video="$(echo "$n" | sed 's/sel //')" - fi - metadata - interactive - elif echo "$n" | grep "^url " > /dev/null - then - if echo "$n" | grep "youtube.com" - then - selected_video="$(echo "$n" | sed 's/.*watch?v=//')" - else - selected_video="$(echo "$n" | sed 's/url //')" - fi - metadata - interactive - elif [ "$n" == "download" ] || [ "$n" == "dl" ] - then - download - interactive - elif [ "$n" == "metadata" ] || [ "$n" == "md" ] - then - metadata - interactive - elif [ "$n" == "stream" ] || [ "$n" == "str" ] - then - stream - interactive - elif [ "$n" == "about" ] || [ "$n" == "!" ] - then - about - interactive - elif [ "$n" == "clear" ] || [ "$n" == "cls" ] - then - clear - interactive - elif [ "$n" == "exit" ] - then - rm /tmp/yt-* - exit - else - get_input - fi + case $n in + help|'?') + help ; interactive ;; + download|dl) + download ; interactive ;; + metadata|md) + metadata ; interactive ;; + stream|str) + stream ; interactive ;; + about|'!') + about ; interactive ;; + clear|cls) + clear ; interactive ;; + exit) + rm /tmp/yt-* ; exit ;; + *) + if [ "$n" ] && [ -z "${n%search *}" ] + then + search "${n#search }" + interactive + elif [ "$n" ] && [ -z "${n%/*}" ] + then + search "${n#/}" + interactive + elif [ "$n" ] && [ -z "${n%channel *}" ] + then + channel "${n#channel }" + interactive + elif [ "$n" ] && [ -z "${n%chan *}" ] + then + channel "${n#chan }" + interactive + elif echo "$n" | grep "^video " > /dev/null + then + if echo "$n" | grep "youtube.com" + then + selected_video="$(echo "$n" | sed 's/.*watch?v=//')" + else + selected_video="$(echo "$n" | sed 's/video //')" + fi + metadata + interactive + elif echo "$n" | grep "^sel " > /dev/null + then + if echo "$n" | grep "youtube.com" + then + selected_video="$(echo "$n" | sed 's/.*watch?v=//')" + else + selected_video="$(echo "$n" | sed 's/sel //')" + fi + metadata + interactive + elif echo "$n" | grep "^url " > /dev/null + then + if echo "$n" | grep "youtube.com" + then + selected_video="$(echo "$n" | sed 's/.*watch?v=//')" + else + selected_video="$(echo "$n" | sed 's/url //')" + fi + metadata + interactive + else + get_input + fi + ;; + esac } + function help() { echo "about | ! View the about page." echo "clear | cls Clear the screen." @@ -164,5 +154,5 @@ function help() { echo "command." } -echo -e "\033[0;35mShelltube v0.3" +printf '\033[0;35mShelltube v0.4.5' interactive