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
ani-cli -d <query>
# resume watching anime
ani-cli -H
Multiple episodes can be viewed/downloaded by giving the episode range like so
Choose episode [1-13]: 1 6
@ -21,6 +24,7 @@ This would open/download episodes 1 2 3 4 5 6
## Dependencies
* grep
* curl
* sed
* mpv

33
ani-cli
View File

@ -1,10 +1,11 @@
#!/bin/sh
# dependencies: sed curl video_player
# dependencies: grep sed curl video_player
# video_player ( needs to be able to play urls )
player_fn="mpv"
prog="ani-cli"
logfile="${XDG_CACHE_HOME:-$HOME/.cache}/ani-hsts"
c_red="\033[1;31m"
c_green="\033[1;32m"
@ -22,6 +23,7 @@ help_text () {
USAGE: $prog <query>
-h show this help text
-d download episode
-H continue where you left off
EOF
}
@ -92,11 +94,12 @@ dep_ch () {
# Start Up #
############
dep_ch "$player_fn" "curl" "sed"
dep_ch "$player_fn" "curl" "sed" "grep"
# option parsing
is_download=0
while getopts 'hd' OPT; do
resume_watching=0
while getopts 'hdH' OPT; do
case $OPT in
h)
help_text
@ -105,11 +108,15 @@ while getopts 'hd' OPT; do
d)
is_download=1
;;
H)
resume_watching=1
;;
esac
done
shift $((OPTIND - 1))
# get query
[ $resume_watching -eq 0 ] &&
if [ -z "$*" ]; then
printf "Search Anime: "
read -r query
@ -117,11 +124,18 @@ else
query=$*
fi
# create history file
[ -f $logfile ] || printf "" > $logfile
#####################
## Anime selection ##
#####################
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"
@ -173,12 +187,16 @@ read last_ep_number <<EOF
$(search_eps "$selection_id")
EOF
if [ $resume_watching -eq 0 ]; then
[ $is_download -eq 1 ] &&
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
read ep_choice_start ep_choice_end
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"
@ -191,6 +209,10 @@ printf "$c_reset"
fi
}
# add anime to history file
grep -q -w "${selection_id}" $logfile ||
printf "%s\t%d\n" "$selection_id" $((episode+1)) >> $logfile
open_episode () {
anime_id=$1
episode=$2
@ -217,6 +239,11 @@ open_episode () {
esac
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
else
printf "Downloading episode $episode ...\n"