#/bin/sh ######################################## # name: ytlib.sh # desc: library for shelltube (`yt`) # main: jadedctrl # code: git.eunichx.us/shelltube.git ######################################## # -------------------------------------- # generic functions # STRING --> STRING # Return the first 'word' (space-delimiter) of a string. function car { local string="$1" echo "$string" \ | awk '{print $1}' } # STRING --> STRING # Return all words after the first word of a string. function cdr { local string="$1" echo "$string" \ | awk '{$1=""; print}' \ | sed 's/^ //' } # NUMBER NUMBER --> NUMBER # Well, subtraction. Y'know... function subtract { local operated=$1 local operatee=$2 echo "${1}-${2}" \ | bc } # STRING --> NUMBER # Count how many words are in a string. function length { local string="$1" echo "$string" \ | wc -w } # STRING --> NUMBER # Count how many lines are in a string. function line_length { local string="$1" echo "$string" \ | wc -l } # STRING --> NUMBER # Return the length of a string in chars. function char_length { local string="$1" subtract $(echo "$string" | wc -c) 1 } # STRING NUMBER [STRING] --> STRING # pad string $1 out to the minimum length $2 with padding $3 (default: "0") function min_string_length { local length="$2" local string="$1" if test -z "$3"; then local padding="0" else local padding="$3" fi local new="$string" if test "$length" -le "$(char_length "$string")"; then echo "$string" else while test "$(char_length "$new")" -lt "$length"; do new="${new}${padding}" done echo "$new" fi } # STRING NUMBER --> STRING # get the first $2 characters in string $1 function char_get { local number="$1" local string="$2" string="$(min_string_length "$string" $number " ")" echo "$string" \ | grep -o "^$(min_string_length "." $number ".")" } # -------------------------------------- # ansi colors and such # NIL --> STRING # Print an ANSI "unbold" escape string. function unbold { printf "$(tput sgr0)" } # STRING --> STRING # Print an ANSI "bold" escape string. function bold { printf "$(tput bold)" } # STRING --> STRING # Format a URL appropriately. function format_url { local url="$1" # playlist/playlist video if echo "$url" | grep -q "playlist?"; then echo "$url" \ | sed 's%.*list=%%' # video else echo "$url" fi } # -------------------------------------- # result parsing # (playlist items, search -items) # STRING --> STRING # Pass the raw search HTML, get raw video result-lines function result_lines { local search_html="$1" echo "$search_html" \ | grep " STRING # Return the video ID of a result_line. function result_id { local result_line="$1" if echo "$result_line" | grep -q "playlist-item"; then echo "$result_line" \ | sed 's%.*?list=%%' \ | sed 's%".*%%' elif echo "$result_line" | grep -q "data-title"; then echo "$result_line" \ | sed 's%.* data-video-id="%%' \ | sed 's%"> STRING # Return the URL of a result-line. function result_url { local result_line="$1" local id="$(result_id "$result_line")" # playlist ID if echo "$id" | grep -q "^PL"; then echo "https://youtube.com/playlist?list=${id}" # video ID else # search result echo "https://youtube.com/watch?v=${id}" fi } # STRING --> STRING # Return the title of a result-line. function result_title { local result_line="$1" # playlist video if echo "$result_line" | grep -q "data-title"; then echo "$result_line" \ | sed 's%.*data-title="%%' \ | sed 's%" data-video-id.*%%' \ | sed 's%".*%%' # video else echo "$result_line" \ | sed 's%.*" title="%%' \ | sed 's%".*%%' fi } # STRING --> STRING # Return the duration of a result-line. function result_duration { local result_line="$1" # playlist if echo "$result_line" | grep -q "playlist-item"; then echo "$result_line" \ | sed 's%.*View full playlist (%%' \ | sed 's% videos.*%%' \ | sed 's%$%v%' # playlist video elif echo "$result_line" | grep -q "data-title"; then echo "00:00" # video else echo "$result_line" \ | sed 's%.*> - Duration: %%' \ | sed 's%\..*%%' fi } # STRING --> STRING # Return the channel of a result-line. function result_channel { local result_line="$1" # playlist if echo "$result_line" | grep -q "playlist-item"; then # playlist with /user/ if echo "$result_line" | grep -q "/user/"; then echo "$result_line" \ | sed 's%.*/user/%%' \ | sed 's%.*%%' \ | sed 's%.*>%%' # playlist with /channel/ else echo "$result_line" \ | sed 's%.*/channel/%%' \ | sed 's%.*%%' \ | sed 's%.*>%%' fi # playlist video elif echo "$result_line" | grep -q "data-title"; then echo "Someone, bby <3" # video else # video with /channel/ if echo "$result_line" | grep -q "/channel/"; then echo "$result_line" \ | sed 's%.*href="/channel/%%' \ | sed 's%.*%%' \ | sed 's%.*" >%%' # video with /user/ elif echo "$result_line" | grep -q "/user/"; then echo "$result_line" \ | sed 's%.*href="/user/%%' \ | sed 's%".*%%' fi fi } # STRING --> STRING # Return the .yt-lockup-meta-info