refactor: modular code

- created fuctions for most snippets
- zero padding to the download file name
This commit is contained in:
Harshith 2021-06-18 15:48:16 +05:30
parent 00912ce2cf
commit 79fd433746

239
ani-cli
View File

@ -21,9 +21,9 @@ help_text () {
printf "%s\n" "$line" printf "%s\n" "$line"
done <<-EOF done <<-EOF
USAGE: $prog <query> USAGE: $prog <query>
-h show this help text -h show this help text
-d download episode -d download episode
-H continue where you left off -H continue where you left off
EOF EOF
} }
@ -33,6 +33,10 @@ die () {
exit 1 exit 1
} }
err () {
printf "$c_red%s$c_reset\n" "$*" >&2
}
search_anime () { search_anime () {
# get anime name along with its id # get anime name along with its id
search=$1 search=$1
@ -42,7 +46,6 @@ search_anime () {
-G \ -G \
-d "keyword=$search" | -d "keyword=$search" |
sed -n -E ' sed -n -E '
#<a href="/category/one-punch-man" title="One Punch Man">
s_^[[:space:]]*<a href="/category/([^"]*)" title="([^"]*)".*_\1_p s_^[[:space:]]*<a href="/category/([^"]*)" title="([^"]*)".*_\1_p
' '
} }
@ -90,135 +93,92 @@ dep_ch () {
done done
} }
############
# Start Up #
############
dep_ch "$player_fn" "curl" "sed" "grep"
# option parsing
is_download=0
resume_watching=0
while getopts 'hdH' OPT; do
case $OPT in
h)
help_text
exit 0
;;
d)
is_download=1
;;
H)
resume_watching=1
;;
esac
done
shift $((OPTIND - 1))
# get query # get query
[ $resume_watching -eq 0 ] && get_search_query () {
if [ -z "$*" ]; then if [ -z "$*" ]; then
printf "Search Anime: " printf "Search Anime: "
read -r query read -r query
else else
query=$* query=$*
fi fi
}
# create history file # create history file
[ -f $logfile ] || printf "" > $logfile [ -f "$logfile" ] || : > "$logfile"
##################### #####################
## Anime selection ## ## Anime selection ##
##################### #####################
if [ $resume_watching -eq 0 ]; then anime_selection () {
search_results=$(search_anime "$query") search_results=$*
else menu_format_string='[%d] %s\n'
search_results=$(sed -n -E 's/\t[0-9]*//p' $logfile) menu_format_string_c1="$c_blue[$c_cyan%d$c_blue] $c_reset%s\n"
fi menu_format_string_c2="$c_blue[$c_cyan%d$c_blue] $c_yellow%s$c_reset\n"
[ -z "$search_results" ] && die "No search results found" count=1
while read anime_id; do
# alternating colors for menu
[ $((count % 2)) -eq 0 ] &&
menu_format_string=$menu_format_string_c1 ||
menu_format_string=$menu_format_string_c2
# Creating menu printf "$menu_format_string" "$count" "$anime_id"
count=$((count+1))
done <<-EOF
$search_results
EOF
menu_format_string='[%d] %s\n' # User input
menu_format_string_c1="$c_blue[$c_cyan%d$c_blue] $c_reset%s\n" printf "$c_blue%s$c_green" "Enter number: "
menu_format_string_c2="$c_blue[$c_cyan%d$c_blue] $c_yellow%s$c_reset\n" read choice
printf "$c_reset"
count=1 # Check if input is a number
while read anime_id; do [ "$choice" -eq "$choice" ] 2>/dev/null || die "Invalid number entered"
# alternating colors for menu
[ $((count % 2)) -eq 0 ] &&
menu_format_string=$menu_format_string_c1 ||
menu_format_string=$menu_format_string_c2
printf "$menu_format_string" "$count" "$anime_id" # Select respective anime_id
count=$((count+1)) count=1
done <<EOF while read anime_id; do
$search_results if [ $count -eq $choice ]; then
EOF selection_id=$anime_id
break
fi
count=$((count+1))
done <<-EOF
$search_results
EOF
# User input [ -z "$selection_id" ] && die "Invalid number entered"
printf "$c_blue%s$c_green" "Enter number: "
read choice
printf "$c_reset"
# Check if input is a number read last_ep_number <<-EOF
[ "$choice" -eq "$choice" ] 2>/dev/null || die "Invalid number entered" $(search_eps "$selection_id")
EOF
# Select respective anime_id }
count=1
while read anime_id; do
if [ $count -eq $choice ]; then
selection_id=$anime_id
break
fi
count=$((count+1))
done <<EOF
$search_results
EOF
[ -z "$selection_id" ] && die "Invalid number entered"
################## ##################
## Ep selection ## ## Ep selection ##
################## ##################
read last_ep_number <<EOF
$(search_eps "$selection_id")
EOF
if [ $resume_watching -eq 0 ]; then episode_selection () {
[ $is_download -eq 1 ] && [ $is_download -eq 1 ] &&
printf "Range of episodes can be specified: start_number end_number\n" printf "Range of episodes can be specified: start_number end_number\n"
printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number
read ep_choice_start ep_choice_end read ep_choice_start ep_choice_end
printf "$c_reset" printf "$c_reset"
else
ep_choice_start=$(sed -n -E "s/${selection_id}\t//p" $logfile)
fi
{ # checking input
[ "$ep_choice_start" -eq "$ep_choice_start" ] 2>/dev/null || die "Invalid number entered"
episodes=$ep_choice_start
if [ -n "$ep_choice_end" ]; then
[ "$ep_choice_end" -eq "$ep_choice_end" ] 2>/dev/null || die "Invalid number entered"
# create list of episodes to download/watch
episodes=$(seq $ep_choice_start $ep_choice_end)
fi
} }
# add anime to history file
grep -q -w "${selection_id}" $logfile ||
printf "%s\t%d\n" "$selection_id" $((episode+1)) >> $logfile
open_episode () { open_episode () {
anime_id=$1 anime_id=$1
episode=$2 episode=$2
if [ $episode -lt 1 ] || [ $episode -gt $last_ep_number ]; then if [ $episode -lt 1 ] || [ $episode -gt $last_ep_number ]; then
die "Episode out of range" err "Episode out of range"
printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number
read episode
printf "$c_reset"
fi fi
printf "Getting data for episode %d\n" $episode printf "Getting data for episode %d\n" $episode
@ -239,15 +199,17 @@ open_episode () {
esac esac
if [ $is_download -eq 0 ]; then if [ $is_download -eq 0 ]; then
# write anime and episode number # write anime and episode number
sed -E " sed -E "
s/^${selection_id}\t[0-9]+/${selection_id}\t$((episode+1))/ s/^${selection_id}\t[0-9]+/${selection_id}\t$((episode+1))/
" $logfile > "${logfile}.new" && mv "${logfile}.new" $logfile " "$logfile" > "${logfile}.new" && mv "${logfile}.new" "$logfile"
setsid -f $player_fn "$video_url" >/dev/null 2>&1 setsid -f $player_fn "$video_url" >/dev/null 2>&1
else else
printf "Downloading episode $episode ...\n" printf "Downloading episode $episode ...\n"
printf "%s\n" "$video_url" printf "%s\n" "$video_url"
# add 0 padding to the episode name
episode=$(printf "%03d" $episode)
{ {
curl -L -# "$video_url" -o "${anime_id}-${episode}.mp4" && curl -L -# "$video_url" -o "${anime_id}-${episode}.mp4" &&
printf "${c_green}Downloaded episode: %s${c_reset}\n" "$episode" || printf "${c_green}Downloaded episode: %s${c_reset}\n" "$episode" ||
@ -256,6 +218,67 @@ open_episode () {
fi fi
} }
############
# Start Up #
############
dep_ch "$player_fn" "curl" "sed" "grep"
# option parsing
is_download=0
scrape=query
while getopts 'hdH' OPT; do
case $OPT in
h)
help_text
exit 0
;;
d)
is_download=1
;;
H)
scrape=history
;;
esac
done
shift $((OPTIND - 1))
########
# main #
########
case $scrape in
query)
get_search_query "$*"
search_results=$(search_anime "$query")
[ -z "$search_results" ] && die "No search results found"
anime_selection "$search_results"
episode_selection
;;
history)
search_results=$(sed -n -E 's/\t[0-9]*//p' "$logfile")
[ -z "$search_results" ] && die "History is empty"
anime_selection "$search_results"
ep_choice_start=$(sed -n -E "s/${selection_id}\t//p" "$logfile")
;;
esac
{ # checking input
[ "$ep_choice_start" -eq "$ep_choice_start" ] 2>/dev/null || die "Invalid number entered"
episodes=$ep_choice_start
if [ -n "$ep_choice_end" ]; then
[ "$ep_choice_end" -eq "$ep_choice_end" ] 2>/dev/null || die "Invalid number entered"
# create list of episodes to download/watch
episodes=$(seq $ep_choice_start $ep_choice_end)
fi
}
# add anime to history file
grep -q -w "${selection_id}" "$logfile" ||
printf "%s\t%d\n" "$selection_id" $((episode+1)) >> "$logfile"
for ep in $episodes for ep in $episodes
do do
open_episode "$selection_id" "$ep" open_episode "$selection_id" "$ep"
@ -263,10 +286,6 @@ done
episode=${ep_choice_end:-$ep_choice_start} episode=${ep_choice_end:-$ep_choice_start}
while :; do while :; do
# to donwload/view many episodes at a time
# set episodes to the last episode and continue with menu, only the first time
printf "\n${c_green}Currently playing %s episode ${c_cyan}%d/%d\n" "$selection_id" $episode $last_ep_number printf "\n${c_green}Currently playing %s episode ${c_cyan}%d/%d\n" "$selection_id" $episode $last_ep_number
printf "$c_blue[${c_cyan}%s$c_blue] $c_yellow%s$c_reset\n" "n" "next episode" printf "$c_blue[${c_cyan}%s$c_blue] $c_yellow%s$c_reset\n" "n" "next episode"
printf "$c_blue[${c_cyan}%s$c_blue] $c_magenta%s$c_reset\n" "p" "previous episode" printf "$c_blue[${c_cyan}%s$c_blue] $c_magenta%s$c_reset\n" "p" "previous episode"