Merge pull request #14 from Dink4n/resume-watching

feat: episode count to continue where we left off
This commit is contained in:
Harshith 2021-06-18 15:12:49 +05:30 committed by GitHub
commit 00912ce2cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 15 deletions

View File

@ -13,6 +13,9 @@ This tool scrapes the site [gogoanime](https://gogoanime.vc).
# download anime # download anime
ani-cli -d <query> ani-cli -d <query>
# resume watching anime
ani-cli -H
Multiple episodes can be viewed/downloaded by giving the episode range like so Multiple episodes can be viewed/downloaded by giving the episode range like so
Choose episode [1-13]: 1 6 Choose episode [1-13]: 1 6
@ -21,6 +24,7 @@ This would open/download episodes 1 2 3 4 5 6
## Dependencies ## Dependencies
* grep
* curl * curl
* sed * sed
* mpv * mpv

49
ani-cli
View File

@ -1,10 +1,11 @@
#!/bin/sh #!/bin/sh
# dependencies: sed curl video_player # dependencies: grep sed curl 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 continue 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" "grep"
# 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,23 +108,34 @@ 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
if [ -z "$*" ]; then [ $resume_watching -eq 0 ] &&
if [ -z "$*" ]; then
printf "Search Anime: " printf "Search Anime: "
read -r query read -r query
else else
query=$* query=$*
fi fi
# create history file
[ -f $logfile ] || printf "" > $logfile
##################### #####################
## Anime selection ## ## Anime selection ##
##################### #####################
search_results=$(search_anime "$query") if [ $resume_watching -eq 0 ]; then
search_results=$(search_anime "$query")
else
search_results=$(sed -n -E 's/\t[0-9]*//p' $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
[ $is_download -eq 1 ] && if [ $resume_watching -eq 0 ]; then
[ $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 { # 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"
@ -191,6 +209,10 @@ printf "$c_reset"
fi 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
@ -217,6 +239,11 @@ open_episode () {
esac esac
if [ $is_download -eq 0 ]; then if [ $is_download -eq 0 ]; then
# write anime and episode number
sed -E "
s/^${selection_id}\t[0-9]+/${selection_id}\t$((episode+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"