aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpystardust <notpiestardust@gmail.com>2021-03-02 13:43:36 +0530
committerpystardust <notpiestardust@gmail.com>2021-03-02 13:43:36 +0530
commit4711323b822758c7c0617b3068348ddffa580104 (patch)
treeeefb2e265303e39ef78d691735c675cff7ae4fcf
parent013ca45d20f656371b0bad1fe82d590820232434 (diff)
downloadytfzf-4711323b822758c7c0617b3068348ddffa580104.tar.gz
clean: add more comments and made the code more readable
-rwxr-xr-xytfzf178
1 files changed, 114 insertions, 64 deletions
diff --git a/ytfzf b/ytfzf
index 7d8628f..06c2cf8 100755
--- a/ytfzf
+++ b/ytfzf
@@ -1,8 +1,12 @@
#!/bin/sh
-# https://github.com/pystardust/ytfzf
+######################################
+#> https://github.com/pystardust/ytfzf
+######################################
-# DEFAULTS
+############################
+# Defaults #
+############################
[ -z "$YTFZF_HIST" ] && YTFZF_HIST=1
[ -z "$YTFZF_LOOP" ] && YTFZF_LOOP=0
[ -z "$YTFZF_CUR" ] && YTFZF_CUR=1
@@ -10,23 +14,26 @@
[ -z "$YTFZF_PREF" ] && YTFZF_PREF=""
[ -z "$YTFZF_EXTMENU" ] && YTFZF_EXTMENU='dmenu -i -l 30 -p Search:'
[ -z "$YTFZF_EXTMENU_LEN" ] && YTFZF_EXTMENU_LEN=220
+
+#> Clearing/Enabling fzf_defaults
[ -z "$YTFZF_ENABLE_FZF_DEFAULT_OPTS" ] && YTFZF_ENABLE_FZF_DEFAULT_OPTS=0
+[ $YTFZF_ENABLE_FZF_DEFAULT_OPTS -eq 0 ] && FZF_DEFAULT_OPTS=""
-## files and directories
+#> files and directories
history_file="$YTFZF_CACHE"/ytfzf_hst
current_file="$YTFZF_CACHE"/ytfzf_cur
thumb_dir="$YTFZF_CACHE"/thumb
[ -d $YTFZF_CACHE ] || mkdir -p $YTFZF_CACHE
[ -d $thumb_dir ] || mkdir -p $thumb_dir
-
-## player settings
+#> player
player="mpv"
player_format="mpv --ytdl-format="
-## misc
-[ $YTFZF_ENABLE_FZF_DEFAULT_OPTS -eq 0 ] && FZF_DEFAULT_OPTS=""
+############################
+# Help Texts #
+############################
helpinfo () {
printf "Usage: %bytfzf %b<search query>%b\n" "\033[1;32m" "\033[1;33m" "\033[0m";
printf " -h Show this help text\n";
@@ -71,7 +78,12 @@ printf " 'ytfzf -h' for more information\n";
errinfo () {
printf "Check for new versions and report at: https://github.com/pystardust/ytfzf\n"
}
-# Adjusting size of menu
+
+
+############################
+# Formatting #
+############################
+#> To determine the length of each field (title, channel ... etc)
format_ext_menu () {
frac=$(((YTFZF_EXTMENU_LEN - 5 - 12)/11))
title_len=$((frac * 6 - 1))
@@ -87,6 +99,7 @@ format_fzf () {
date_len=14
url_len=12
+ #> Get the terminal size to adjust length accordingly
t_size="$(stty size 2> /dev/null | cut -f2 -d' ')"
if [ "$t_size" = "" ]; then
printf "\e[31mWhen using stdin put - for the search query\033[000m\n" && exit 2
@@ -119,10 +132,11 @@ format_fzf () {
date_len=$((frac * 2 + 20))
fi
}
+#> Formats the fields depending on which menu is needed. And assigns the menu command.
format_menu () {
if [ $is_ext_menu -eq 0 ]; then
dep_ck "fzf"
- prompt_menu='fzf -m --bind change:top --tabstop=1 --layout=reverse --delimiter="$(printf "\t")" --nth=1,2 $FZF_DEFAULT_OPTS'
+ menu_command='fzf -m --bind change:top --tabstop=1 --layout=reverse --delimiter="$(printf "\t")" --nth=1,2 $FZF_DEFAULT_OPTS'
if [ $is_stdin -eq 0 ] ; then
format_fzf
else
@@ -130,31 +144,38 @@ format_menu () {
fi
else
# dmenu doesnt render tabs so removing it
- prompt_menu='tr -d "$(printf "\t")" | '"$YTFZF_EXTMENU"
+ menu_command='tr -d "$(printf "\t")" | '"$YTFZF_EXTMENU"
format_ext_menu
fi
}
+#> Trucating and formatting the fields based on the decided lengths
format_awk () {
- awk -F'\t' \
+ printf "%s" "$*" | awk -F'\t' \
-v A=$title_len -v B=$channel_len -v C=$dur_len -v D=$view_len -v E=$date_len -v F=$url_len \
'{ printf "%-"A"."A"s\t%-"B"."B"s\t%-"C"."C"s\t%-"D"."D"s\t%-"E"."E"s\t%-"F"."F"s\n",$1,$2,$4,$3,$5,$6}'
}
+
+
+
+############################
+# Video selection Menu #
+############################
video_menu () {
- format_awk | eval "$prompt_menu"
+ format_awk "$*" | eval "$menu_command"
}
video_menu_img () {
title_len=400
- format_awk | fzf -m --tabstop=1 --bind change:top --delimiter="$(printf "\t")" --nth=1,2 $FZF_DEFAULT_OPTS \
+ format_awk "$*" | fzf -m --tabstop=1 --bind change:top --delimiter="$(printf "\t")" --nth=1,2 $FZF_DEFAULT_OPTS \
--layout=reverse --preview "sh $0 -U {}" \
--preview-window "left:50%:noborder:wrap"
}
-save_before_exit () {
- [ $is_url -eq 1 ] && exit
- [ $YTFZF_HIST -eq 1 ] && printf "$selected_data\n" >> "$history_file" ;
- [ $YTFZF_CUR -eq 1 ] && printf "" > "$current_file" ;
-}
+
+
+############################
+# Image previews #
+############################
## The following snippet of code has been copied and modified from
-## https://github.com/OliverLew/fontpreview-ueberzug MIT License
+# https://github.com/OliverLew/fontpreview-ueberzug MIT License
# Ueberzug related variables
FIFO="/tmp/ytfzf-ueberzug-fifo"
IMAGE="/tmp/ytfzf-ueberzug-img.png"
@@ -168,7 +189,6 @@ start_ueberzug () {
ueberzug layer --parser json --silent < "$FIFO" &
exec 3>"$FIFO"
}
-
stop_ueberzug () {
exec 3>&-
rm "$FIFO" "$IMAGE" > /dev/null 2>&1
@@ -185,19 +205,12 @@ preview_img () {
echo "$@" | tr -d '|' | sed "s/ *\t/\t/g" | awk -F'\t' \
'{ printf "\n%s\nChannel: %s\nViews: %s\nDuration: %s\nUploaded %s\n",$1,$2,$4,$3,$5}'
}
-### Ueberzug end
-browser_history () {
- if [ $YTFZF_HIST -eq 1 ]; then
- [ -e "$history_file" ] || touch "$history_file"
- hist_data="$(tac "$history_file")"
- [ -z "$hist_data" ] && printf "History is empty!\n" && exit;
- videos_data="$(echo "$hist_data" | uniq )"
- scrape=0
- else
- printf "History is not enabled. Please enable it to use this option (-H).\n";
- exit;
- fi
-}
+
+
+############################
+# Scraping #
+############################
+
download_thumbnails () {
thumb_urls="$(printf "%s" "$videos_json" |\
jq '.thumbnail.thumbnails[0].url,.videoId' )"
@@ -219,32 +232,15 @@ EOF
wait && [ $show_link_only -eq 0 ] && echo ""
}
-get_query () {
- if [ $is_stdin -eq 1 ]; then
- while read line; do
- search_query="$search_query $line"
- done
- else
- if [ -z "$search_query" ]; then
- if [ $is_ext_menu -eq 1 ]; then
- search_query="$(printf "" | $YTFZF_EXTMENU)"
- else
- printf "Search Youtube: "
- read search_query
- fi
- [ -z "$search_query" ] && { exit 0; }
- fi
- fi
-}
-scrape_fn () {
- # needs search_query ( get_query )
- ## Scrape data and store video information ( and thumbnails download)
+scrape_yt () {
+ # needs search_query
+ ## Scrape data and store video information in videos_data ( and thumbnails download)
## GETTING DATA
yt_html="$(
useragent='user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.152 Safari/537.36'
curl 'https://www.youtube.com/results' -s \
- -G --data-urlencode "search_query=$search_query" \
+ -G --data-urlencode "search_query=$*" \
-H 'authority: www.youtube.com' \
-H "$useragent" \
-H 'accept-language: en-US,en;q=0.9' \
@@ -275,8 +271,33 @@ scrape_fn () {
[ $show_thumbnails -eq 1 ] && download_thumbnails
}
+
+
+############################
+# User selection #
+############################
+#> To get search query
+get_search_query () {
+ if [ $is_stdin -eq 1 ]; then
+ while read line; do
+ search_query="$search_query $line"
+ done
+ else
+ if [ -z "$search_query" ]; then
+ if [ $is_ext_menu -eq 1 ]; then
+ search_query="$(printf "" | $YTFZF_EXTMENU)"
+ else
+ printf "Search Youtube: "
+ read search_query
+ fi
+ [ -z "$search_query" ] && { exit 0; }
+ fi
+ fi
+}
+#> To select videos from videos_data
user_selection () {
[ $is_url -eq 1 ] && return
+
format_menu
if [ $auto_select -eq 1 ] ; then
@@ -286,10 +307,10 @@ user_selection () {
elif [ $show_thumbnails -eq 1 ] ; then
dep_ck "ueberzug"
start_ueberzug
- selected_data="$(echo "$videos_data" | video_menu_img )"
+ selected_data="$( video_menu_img "$videos_data" )"
stop_ueberzug
else
- selected_data="$(echo "$videos_data" | video_menu )"
+ selected_data="$( video_menu "$videos_data" )"
fi
shorturls="$(echo "$selected_data" | sed -E -e 's_.*\|([^|]+) *$_\1_')"
@@ -299,7 +320,6 @@ user_selection () {
while read surl; do
[ -z "$surl" ] && continue
urls="$urls\nhttps://www.youtube.com/watch?v=$surl"
- # to get back untrucated titles from formatting
selected_data="$selected_data\n$(echo "$videos_data" | grep -m1 -e "$surl" )"
done<<EOF
$shorturls
@@ -312,15 +332,19 @@ EOF
exit
fi
- # selecte format if flag given
+ # select format if flag given
if [ $show_format -eq 1 ]; then
YTFZF_PREF="$(youtube-dl -F "$(printf "$urls" | sed 1q)" | sed '1,3d' | tac - |\
- eval "$prompt_menu" | sed -E 's/^([^ ]*) .*/\1/')"
+ eval "$menu_command" | sed -E 's/^([^ ]*) .*/\1/')"
[ -z $YTFZF_PREF ] && exit;
fi
}
play_url () {
+ #> output the current track to current file before playing
[ $YTFZF_CUR -eq 1 ] && printf "$selected_data" > "$current_file" ;
+
+ #> Play url with $player or $player_format based on options
+ #> if player format fails, then use normal player
[ -n "$YTFZF_PREF" ] && {
eval "$player_format"\'$YTFZF_PREF\' "\"$(printf "$urls" | tr '\n' '\t' | sed 's_\t_" "_g' )\""
} || {
@@ -330,6 +354,17 @@ play_url () {
printf "\033[31mERROR[#03]: Couldn't play the video/audio using the current player.\n\tTry updating youtube-dl\033[000m\n"; errinfo ; save_before_exit ; exit 2;
}
}
+save_before_exit () {
+ [ $is_url -eq 1 ] && exit
+ [ $YTFZF_HIST -eq 1 ] && printf "$selected_data\n" >> "$history_file" ;
+ [ $YTFZF_CUR -eq 1 ] && printf "" > "$current_file" ;
+}
+
+
+############################
+# Misc #
+############################
+#> if the input is a url then skip video selection and play the url
check_if_url () {
# to check if given input is a url
regex='^https\?://.*'
@@ -342,6 +377,21 @@ check_if_url () {
is_url=0
fi
}
+#> Loads history in videos_data
+get_history () {
+ if [ $YTFZF_HIST -eq 1 ]; then
+ [ -e "$history_file" ] || touch "$history_file"
+ hist_data="$(tac "$history_file")"
+ [ -z "$hist_data" ] && printf "History is empty!\n" && exit;
+ videos_data="$(echo "$hist_data" | uniq )"
+ scrape=0
+ else
+ printf "History is not enabled. Please enable it to use this option (-H).\n";
+ exit;
+ fi
+}
+
+
# DEP CHECK
dep_ck () {
for dep in "$@"; do
@@ -375,7 +425,7 @@ while getopts "LhDmdfxHarltsn:U:" opt; do
;;
f) show_format=1
;;
- H) browser_history
+ H) get_history
;;
x) [ -e "$history_file" ] && rm "$history_file" && touch "$history_file" && printf "History has been cleared\n"
exit;
@@ -417,8 +467,8 @@ check_if_url "$search_query"
#call scrape function
if [ $scrape -eq 1 ]; then
- get_query
- scrape_fn
+ get_search_query
+ scrape_yt "$search_query"
fi
while true; do
@@ -432,7 +482,7 @@ while true; do
#if -s was specified make another search query
if [ $search_again -eq 1 ]; then
search_query=""
- get_query
- scrape_fn
+ get_search_query
+ scrape_yt "$search_query"
fi
done
Un proyecto texto-plano.xyz