refactor and clean up code a bit

This commit is contained in:
ksyasuda 2022-01-15 15:12:42 -08:00
parent 266f684817
commit 74d511b982

77
ani-cli
View File

@ -422,7 +422,7 @@ get_search_query() {
msg="Choose from list of searched anime below, or enter a unique name of an anime to search for" msg="Choose from list of searched anime below, or enter a unique name of an anime to search for"
span="$(generate_span "$msg")" span="$(generate_span "$msg")"
if [ -z "$*" ] && [ "$is_rofi" -eq 1 ]; then if [ "$is_rofi" -eq 1 ]; then
query=$(rofi -dpi "$DPI" -dmenu -l 15 -p "Search Anime:" \ query=$(rofi -dpi "$DPI" -dmenu -l 15 -p "Search Anime:" \
-mesg "$span" \ -mesg "$span" \
-config "$ROFI_CFG" < <(run_stmt "$stmt")) -config "$ROFI_CFG" < <(run_stmt "$stmt"))
@ -432,8 +432,6 @@ get_search_query() {
elif [ "$is_rofi" -eq 0 ]; then elif [ "$is_rofi" -eq 0 ]; then
printf "Search Anime: " printf "Search Anime: "
read -r query read -r query
else
query=$*
fi fi
} }
@ -449,7 +447,6 @@ search_anime() {
search="${1// /}" search="${1// /}"
fi fi
logger "Search Query: $search" logger "Search Query: $search"
titlepattern='<a href="/category/'
curl -s "$BASE_URL//search.html" \ curl -s "$BASE_URL//search.html" \
-G \ -G \
-d "keyword=$search" | -d "keyword=$search" |
@ -515,7 +512,7 @@ anime_selection() {
-async-pre-read 33 \ -async-pre-read 33 \
-mesg "$msg" -only-match) -mesg "$msg" -only-match)
[ -z "$user_input" ] && return 1 [ -z "$user_input" ] && return 1
if [ $(awk '{ print $NF }' <<< "$user_input") = "Quit" ]; then if [ "$(awk '{ print $NF }' <<< "$user_input")" = "Quit" ]; then
logger "QUITTING" logger "QUITTING"
return 1 return 1
fi fi
@ -588,7 +585,6 @@ episode_selection() {
FROM watch_history \ FROM watch_history \
WHERE anime_name = '$anime_id';" WHERE anime_name = '$anime_id';"
hist=$(run_stmt "$stmt") hist=$(run_stmt "$stmt")
# logger "HISTORY: ${hist[*]}"
# Get Watch History for $anime_id as comma separated list # Get Watch History for $anime_id as comma separated list
watch_history="" watch_history=""
@ -684,11 +680,11 @@ open_episode() {
{ {
mkdir -p "$dl_dir" || die "Could not create directory" mkdir -p "$dl_dir" || die "Could not create directory"
if command -v "notify-send" > /dev/null; then if command -v "notify-send" > /dev/null; then
aria2c -x 16 -s 16 --referer="$dpage_link" "$video_url" --dir="$dl_dir" -o "$episode.mp4" --download-result=hide && aria2c --referer="$dpage_link" "$video_url" --dir="$dl_dir" -o "$episode.mp4" --download-result=hide &&
notify-send -i "$ANIWRAPPER_ICON_PATH" "Download complete for ${anime_id//-/ } - Episode: $episode" || notify-send -i "$ANIWRAPPER_ICON_PATH" "Download complete for ${anime_id//-/ } - Episode: $episode" ||
notify-send -i "$MAISAN_ICON_PATH" "Download failed for ${anime_id//-/ } - Episode: $episode. Please retry or check your internet connection" notify-send -i "$MAISAN_ICON_PATH" "Download failed for ${anime_id//-/ } - Episode: $episode. Please retry or check your internet connection"
else else
aria2c -x 16 -s 16 --referer="$dpage_link" "$video_url" --dir="$dl_dir" -o "$episode.mp4" --download-result=hide && aria2c --referer="$dpage_link" "$video_url" --dir="$dl_dir" -o "$episode.mp4" --download-result=hide &&
printf "${c_green}Downloaded complete for %s - Episode: %s${c_reset}\n" "${anime_id//-/ }" "$episode" || printf "${c_green}Downloaded complete for %s - Episode: %s${c_reset}\n" "${anime_id//-/ }" "$episode" ||
printf "${c_red}Download failed for %s - Episode: %s, please retry or check your internet connection${c_reset}\n" "${anime_id//-/ }" "$episode" printf "${c_red}Download failed for %s - Episode: %s, please retry or check your internet connection${c_reset}\n" "${anime_id//-/ }" "$episode"
@ -699,10 +695,8 @@ open_episode() {
stream() { stream() {
logger "Running stream()" logger "Running stream()"
logger "args: $*" get_search_query
get_search_query "$*"
searched=0 searched=0
if [ $# -le 1 ]; then
# check if anime has been searched before # check if anime has been searched before
anime_id="${query// /}" anime_id="${query// /}"
[ -z "$anime_id" ] && die "No anime selected or queried" [ -z "$anime_id" ] && die "No anime selected or queried"
@ -710,7 +704,6 @@ stream() {
check_db "$anime_id" "search" check_db "$anime_id" "search"
searched="$?" searched="$?"
logger "Searched before: $searched" logger "Searched before: $searched"
fi
if [ "$searched" -eq 0 ]; then if [ "$searched" -eq 0 ]; then
search_results=$(search_anime "$query") search_results=$(search_anime "$query")
[ -z "$search_results" ] && die "No search results found" [ -z "$search_results" ] && die "No search results found"
@ -729,17 +722,17 @@ stream() {
episode_selection episode_selection
} }
# to clear the colors when exited using SIGINT
trap "printf '$c_reset'" INT HUP
# option parsing # option parsing
scrape=query parse_args() {
quality=best # to clear the colors when exited using SIGINT
is_rofi=1 trap "printf '$c_reset'" INT HUP
is_download=0 scrape=query
download_dir="." quality=best
half_ep=0 is_rofi=1
while getopts 'hd:Hsvq:c-:f:t:T:CQ:D:' OPT; do is_download=0
download_dir="."
half_ep=0
while getopts 'hd:Hsvq:c-:f:t:T:CQ:D:' OPT; do
case "$OPT" in case "$OPT" in
h) h)
help_text help_text
@ -844,18 +837,17 @@ while getopts 'hd:Hsvq:c-:f:t:T:CQ:D:' OPT; do
exit 1 exit 1
;; ;;
esac esac
done done
shift $((OPTIND - 1)) shift $((OPTIND - 1))
}
dep_ch "$player_fn" "curl" "sed" "grep" "sqlite3" "rofi" "git" "aria2c" "jq"
######## ########
# main # # main #
######## ########
main() {
case $scrape in case $scrape in
query) query)
stream "$*" stream
;; ;;
history) history)
stmt="SELECT DISTINCT anime_name FROM watch_history ORDER BY watch_date DESC" stmt="SELECT DISTINCT anime_name FROM watch_history ORDER BY watch_date DESC"
@ -921,24 +913,24 @@ case $scrape in
play_file "$video_path" play_file "$video_path"
exit $? exit $?
;; ;;
esac esac
check_input check_input
# plays selected episode(s) # plays selected episode(s)
for ep in $episodes; do for ep in $episodes; do
open_episode "$selection_id" "$ep" "$download_dir" open_episode "$selection_id" "$ep" "$download_dir"
done done
if [[ "$is_download" -eq 1 ]]; then if [[ "$is_download" -eq 1 ]]; then
logger "Finished downloading episodes: $episodes for $selection_id... exiting" logger "Finished downloading episodes: $episodes for $selection_id... exiting"
exit 0 exit 0
fi fi
episode=${ep_choice_end:-$ep_choice_start} episode=${ep_choice_end:-$ep_choice_start}
choice='' choice=''
while :; do while :; do
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
if [ "$episode" -ne "$last_ep_number" ]; then if [ "$episode" -ne "$last_ep_number" ]; then
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"
@ -994,4 +986,9 @@ while :; do
;; ;;
esac esac
open_episode "$selection_id" "$episode" "$download_dir" open_episode "$selection_id" "$episode" "$download_dir"
done done
}
dep_ch "$player_fn" "curl" "sed" "grep" "sqlite3" "rofi" "git" "aria2c" "jq"
parse_args "$@"
main