mirror of
https://github.com/ksyasuda/aniwrapper.git
synced 2024-11-22 03:19:53 -08:00
refactor: modular code
- created fuctions for most snippets - zero padding to the download file name
This commit is contained in:
parent
00912ce2cf
commit
79fd433746
151
ani-cli
151
ani-cli
@ -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,57 +93,25 @@ 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
|
|
||||||
search_results=$(sed -n -E 's/\t[0-9]*//p' $logfile)
|
|
||||||
fi
|
|
||||||
|
|
||||||
[ -z "$search_results" ] && die "No search results found"
|
|
||||||
|
|
||||||
# Creating menu
|
|
||||||
|
|
||||||
menu_format_string='[%d] %s\n'
|
menu_format_string='[%d] %s\n'
|
||||||
menu_format_string_c1="$c_blue[$c_cyan%d$c_blue] $c_reset%s\n"
|
menu_format_string_c1="$c_blue[$c_cyan%d$c_blue] $c_reset%s\n"
|
||||||
menu_format_string_c2="$c_blue[$c_cyan%d$c_blue] $c_yellow%s$c_reset\n"
|
menu_format_string_c2="$c_blue[$c_cyan%d$c_blue] $c_yellow%s$c_reset\n"
|
||||||
@ -154,7 +125,7 @@ while read anime_id; do
|
|||||||
|
|
||||||
printf "$menu_format_string" "$count" "$anime_id"
|
printf "$menu_format_string" "$count" "$anime_id"
|
||||||
count=$((count+1))
|
count=$((count+1))
|
||||||
done <<EOF
|
done <<-EOF
|
||||||
$search_results
|
$search_results
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
@ -174,51 +145,40 @@ while read anime_id; do
|
|||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
count=$((count+1))
|
count=$((count+1))
|
||||||
done <<EOF
|
done <<-EOF
|
||||||
$search_results
|
$search_results
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
[ -z "$selection_id" ] && die "Invalid number entered"
|
[ -z "$selection_id" ] && die "Invalid number entered"
|
||||||
|
|
||||||
|
read last_ep_number <<-EOF
|
||||||
|
$(search_eps "$selection_id")
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
##################
|
##################
|
||||||
## 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
|
||||||
@ -242,12 +202,14 @@ open_episode () {
|
|||||||
# 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"
|
||||||
|
Loading…
Reference in New Issue
Block a user