feat: episode count to continue where we left off

This commit is contained in:
JimboKearn 2021-06-14 14:52:02 +05:30
parent 013048b4cd
commit 8d359fef31

28
ani-cli
View File

@ -1,10 +1,11 @@
#!/bin/sh #!/bin/sh
# dependencies: sed curl video_player # dependencies: sed curl jq video_player
# video_player ( needs to be able to play urls ) # video_player ( needs to be able to play urls )
player_fn="mpv" player_fn="mpv"
prog="ani-cli" prog="ani-cli"
logfile="${XDG_CACHE_HOME:-$HOME/.cache}/ani-hsts"
c_red="\033[1;31m" c_red="\033[1;31m"
c_green="\033[1;32m" c_green="\033[1;32m"
@ -22,6 +23,7 @@ help_text () {
USAGE: $prog <query> USAGE: $prog <query>
-h show this help text -h show this help text
-d download episode -d download episode
-H watch where you left off
EOF EOF
} }
@ -92,11 +94,12 @@ dep_ch () {
# Start Up # # Start Up #
############ ############
dep_ch "$player_fn" "curl" "sed" dep_ch "$player_fn" "curl" "sed" "jq"
# option parsing # option parsing
is_download=0 is_download=0
while getopts 'hd' OPT; do resume_watching=0
while getopts 'hdH' OPT; do
case $OPT in case $OPT in
h) h)
help_text help_text
@ -105,11 +108,15 @@ while getopts 'hd' OPT; do
d) d)
is_download=1 is_download=1
;; ;;
H)
resume_watching=1
;;
esac esac
done done
shift $((OPTIND - 1)) shift $((OPTIND - 1))
# get query # get query
[ $resume_watching -eq 0 ] &&
if [ -z "$*" ]; then if [ -z "$*" ]; then
printf "Search Anime: " printf "Search Anime: "
read -r query read -r query
@ -117,11 +124,18 @@ else
query=$* query=$*
fi fi
# create history file
[ -f $logfile ] || printf "{}" > $logfile
##################### #####################
## Anime selection ## ## Anime selection ##
##################### #####################
if [ $resume_watching -eq 0 ]; then
search_results=$(search_anime "$query") search_results=$(search_anime "$query")
else
search_results=$(jq -r 'keys[]' $logfile)
fi
[ -z "$search_results" ] && die "No search results found" [ -z "$search_results" ] && die "No search results found"
@ -173,12 +187,16 @@ read last_ep_number <<EOF
$(search_eps "$selection_id") $(search_eps "$selection_id")
EOF EOF
if [ $resume_watching -eq 0 ]; then
[ $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=$(jq ".\"${selection_id}\"" $logfile)
fi
{ # checking input { # checking input
[ "$ep_choice_start" -eq "$ep_choice_start" ] 2>/dev/null || die "Invalid number entered" [ "$ep_choice_start" -eq "$ep_choice_start" ] 2>/dev/null || die "Invalid number entered"
@ -217,6 +235,10 @@ open_episode () {
esac esac
if [ $is_download -eq 0 ]; then if [ $is_download -eq 0 ]; then
# write anime and episode number
jq ".\"${selection_id}\" = $((ep+1))" $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"