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

33
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,10 +722,10 @@ stream() {
episode_selection episode_selection
} }
# option parsing
parse_args() {
# to clear the colors when exited using SIGINT # to clear the colors when exited using SIGINT
trap "printf '$c_reset'" INT HUP trap "printf '$c_reset'" INT HUP
# option parsing
scrape=query scrape=query
quality=best quality=best
is_rofi=1 is_rofi=1
@ -846,16 +839,15 @@ while getopts 'hd:Hsvq:c-:f:t:T:CQ:D:' OPT; do
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"
@ -995,3 +987,8 @@ 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