implement some ani-cli V2 functions

This commit is contained in:
ksyasuda 2022-04-02 14:36:39 -07:00
parent 6af4a46b86
commit 73eb2a274e
2 changed files with 86 additions and 84 deletions

View File

@ -92,10 +92,10 @@ Install the Dependencies
```sh
# Arch
pacman -S --needed aria2 curl grep mpv rofi sed sqlite3
pacman -S --needed aria2 curl ffmpeg grep mpv rofi sed sqlite3
# Debian
apt install aria2 curl grep mpv rofi sed sqlite3
apt install aria2 curl ffmpeg grep mpv rofi sed sqlite3
```
Clone and switch into the repo directory
@ -272,7 +272,7 @@ ani-cli -s
ani-cli -q <best (default)|1080p|720p|480p|360p|worst>
# choose rofi theme from presets
ani-cli -t <aniwrapper (default)|dracula|fancy|flamingo|material|nord|onedark>
ani-cli -t <aniwrapper (default)|dracula|fancy|material|monokai|nord|nord2|onedark>
# Specify starting directory for play_from_file mode (does not work with -c)
ani-cli -f <starting_directory>

164
ani-cli
View File

@ -1,5 +1,6 @@
#!/usr/bin/env bash
BASE_URL="https://gogoplay4.com"
CFG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/aniwrapper"
HISTORY_DB="$CFG_DIR/history.sqlite3"
ROFI_CFG="$CFG_DIR/themes/aniwrapper.rasi"
@ -14,6 +15,7 @@ SILENT=0
FIRST_EP_NUMBER=1
PLAYER_FN="mpv"
# display error message and exit
die() {
((SILENT != 1)) && printf "\033[1;31m%s\033[0m\n" "$*" >&2
exit 1
@ -24,33 +26,34 @@ err() {
((SILENT != 1)) && printf "\033[1;31m%s\033[0m\n" "$*" >&2
}
# display a log message if verbose mode is enabled
lg() {
((VERBOSE == 1)) && printf "\033[1;35m%s\033[0m\n" "$*" >&2
}
# display an informational message (first argument in green, second in magenta)
inf() {
printf "\033[1;32m%s \033[1;35m%s\033[0m\n" "$1" "$2"
printf "\033[1;35m%s \033[1;35m%s\033[0m\n" "$1" "$2"
}
# prompts the user with message in $1-2 ($1 in blue, $2 in magenta) and saves the input to the variables in $REPLY and $REPLY2
prompt() {
printf "\033[1;34m%s\033[1;35m%s\033[1;34m: \033[0m" "$1" "$2"
printf "\033[1;35m%s\033[1;35m%s\033[1;34m\033[0m: " "$1" "$2"
}
# displays an even (cyan) line of a menu line with $2 as an indicator in [] and $1 as the option
menu_line_even() {
printf "\033[1;34m[\033[1;36m%s\033[1;34m] \033[1;36m%s\033[0m\n" "$2" "$1"
printf "\033[1;36m(\033[1;36m%s\033[1;36m) \033[1;36m%s\033[0m\n" "$2" "$1"
}
# displays an odd (yellow) line of a menu line with $2 as an indicator in [] and $1 as the option
menu_line_odd() {
printf "\033[1;34m[\033[1;33m%s\033[1;34m] \033[1;33m%s\033[0m\n" "$2" "$1"
printf "\033[1;33m(\033[1;33m%s\033[1;33m) \033[1;33m%s\033[0m\n" "$2" "$1"
}
# display alternating menu lines (even and odd)
menu_line_alternate() {
menu_line_parity=${menu_line_parity:-0}
if [ "$menu_line_parity" -eq 0 ]; then
menu_line_odd "$1" "$2"
menu_line_parity=1
@ -62,9 +65,10 @@ menu_line_alternate() {
# displays a warning (red) line of a menu line with $2 as an indicator in [] and $1 as the option
menu_line_strong() {
printf "\033[1;34m[\033[1;31m%s\033[1;34m] \033[1;31m%s\033[0m\n" "$2" "$1"
printf "\033[1;34m[\033[1;33m%s\033[1;34m] \033[1;33m%s\033[0m\n" "$2" "$1"
}
# check if input is valid and assings value/range to episodes variable
check_input() {
if [[ -z "$ep_choice_start" ]] && [[ -z "$ep_choice_end" ]]; then
die "No episode(s) selected"
@ -81,15 +85,8 @@ check_input() {
get_dpage_link() {
anime_id=$1
ep_no=$2
# credits to fork: https://github.com/Dink4n/ani-cli for the fix
for params in "-episode-$ep_no" "-$ep_no" "-episode-$ep_no-1" "-camrip-episode-$ep_no"; do
anime_page=$(curl -s "$BASE_URL/$anime_id$params")
printf '%s' "$anime_page" | grep -q '<h1 class="entry-title">404</h1>' || break
done
printf '%s' "$anime_page" |
sed -n -E 's/.*class="active" rel="1" data-video="([^"]*)".*/\1/p' | sed 's/^/https:/g'
curl -s "$BASE_URL/videos/${anime_id}${ep_no}" | sed -nE 's_^[[:space:]]*<iframe src="([^"]*)".*_\1_p' |
sed 's/^/https:/g'
}
# download the episode
@ -111,14 +108,14 @@ decrypt_link() {
iv='34373730343738393639343138323637'
ajax_url="https://gogoplay4.com/encrypt-ajax.php"
id=$(printf "%s" "$1" | sed -nE 's/.*id=(.*)&title.*/\1/p')
ajax=$(echo $id | openssl enc -e -aes256 -K "$secret_key" -iv "$iv" | base64)
ajax=$(printf '%s' "$id" | openssl enc -e -aes256 -K "$secret_key" -iv "$iv" | base64)
data=$(curl -s -H "X-Requested-With:XMLHttpRequest" "$ajax_url" -d "id=$ajax" | sed -e 's/{"data":"//' -e 's/"}/\n/' -e 's/\\//g')
printf '%s' "$data" | base64 -d | openssl enc -d -aes256 -K "$secret_key" -iv "$iv" | sed -e 's/\].*/\]/' -e 's/\\//g' |
grep -Eo 'https:\/\/[-a-zA-Z0-9@:%._\+~#=][a-zA-Z0-9][-a-zA-Z0-9@:%_\+.~#?&\/\/=]*'
lg "END: decrypt_link()" > /dev/stderr
}
# chooses the link for the set quality
get_video_quality() {
dpage_url=$1
video_links=$(decrypt_link "$dpage_url")
@ -143,6 +140,7 @@ get_video_quality() {
printf '%s' "$video_link"
}
# sets the video quality
set_video_quality() {
if ((IS_ROFI == 1)); then
qualities="1. best|2. 1080p|3. 720p|4. 480p|5. 360p|6. worst"
@ -199,6 +197,7 @@ dep_ch() {
done
}
# sends a notification to notify-send if available, else prints to stdout
notification() {
((SILENT == 1)) && return 0
msg="$*"
@ -209,16 +208,14 @@ notification() {
fi
}
# generates a pre-styled span tag with the given msg
generate_span() {
msg="$*"
span="<span foreground='#ecbe7b' style='italic' size='small'>$msg</span>"
printf "%s\n" "$span"
}
#####################
## Database Code ##
#####################
# runs sql command on the history database
run_stmt() {
printf "%s\n" "$1" | sqlite3 -noheader -list "$HISTORY_DB"
}
@ -368,10 +365,6 @@ sync_watch_history() {
lg "$errs rows skipped on insert"
}
#####################
### Play from file###
#####################
# opens the passed in file with $PLAYER_FN
play_file() {
lg "Checking if file is playable"
@ -394,6 +387,8 @@ play_file() {
fi
}
# gets the list of directories and playable files from the passed in directory
# sets the $watched list to the list of watched files
get_directory_data() {
search_dir="$1"
inputlist=""
@ -478,10 +473,7 @@ find_media() {
esac
}
#####################
## main code ##
#####################
# get the search query from user or from args
get_search_query() {
if [ $# -gt 0 ]; then
query="${*// /-}"
@ -509,31 +501,64 @@ search_anime() {
search="${1// /}"
fi
lg "Search Query: $search"
curl -s "$BASE_URL//search.html" \
-G \
-d "keyword=$search" |
sed -n -E '
s_^[[:space:]]*<a href="/category/([^"]*)" title="([^"]*)".*_\1_p
'
curl -s "$BASE_URL/search.html" -G -d "keyword=$search" |
sed -nE 's_^[[:space:]]*<a href="/videos/([^"]*)">_\1_p'
}
# only lets the user pass in case of a valid search
process_search() {
search_results=$(search_anime "$@")
while [ -z "$search_results" ]; do
lg "No results found for $*" 1> /dev/stderr
extended_search_results=$(extended_search "$@")
if [ -n "$extended_search_results" ]; then
extended_search_results=$(printf '%s' "$extended_search_results" | head -n 1)
search_results=$(search_anime "$extended_search_results")
break
fi
err 'No search results found'
if [ "$IS_ROFI" -eq 1 ]; then
stmt="SELECT id || '. ' || anime_name FROM search_history ORDER BY search_date DESC;"
msg="Choose from list of searched anime below, or enter a unique name of an anime to search for"
query=$(rofi -dpi "$DPI" -dmenu -l 15 -p "Search Anime:" \
-mesg "$(generate_span "$msg")" \
-config "$ROFI_CFG" -window-title 'aniwrapper' < <(run_stmt "$stmt"))
query="${query//[1-9]*\. /}"
else
prompt "Search Anime"
read -r query
fi
search_results=$(search_anime "$query")
done
lg "Search Results: $search_results"
printf '%s' "$search_results"
}
# searches on gogoanime (instead of gogoplay) because they index english titles
extended_search() {
indexing_url=$(curl -s -L -o /dev/null -w "%{url_effective}\n" https://gogoanime.cm)
search=$(printf '%s' "$1" | tr ' ' '-')
curl -s "$indexing_url//search.html" -G -d "keyword=$search" |
sed -n -E 's_^[[:space:]]*<a href="/category/([^"]*)" title="([^"]*)".*_\1_p'
}
check_episode() {
data=$(curl -s "$BASE_URL/videos/$1")
if [ "$data" != "404" ]; then
del=$(printf "%s" "$data" | grep -n "Latest Episodes" | cut -d ":" -f1)
printf "%s" "$data" | sed "$del,$ d" | sed -nE "s_^[[:space:]]*<a href.*videos/${2}(.*)\">_\1_p"
fi
}
search_eps() {
anime_id=$1
result=$(get_dpage_link "$anime_id" 0)
FIRST_EP_NUMBER="${result:-1}"
LAST_EP_NUMBER=$(curl -s "$BASE_URL/category/$anime_id" |
sed -n -E '
/^[[:space:]]*<a href="#" class="active" ep_start/{
s/.* '\''([0-9]*)'\'' ep_end = '\''([0-9]*)'\''.*/\2/p
q
}
')
# lg "Anime $anime_id not in anime database... inserting into database" > /dev/stderr
# insert_history "anime" "$anime_id" "$FIRST_EP_NUMBER" "$LAST_EP_NUMBER"
FIRST_EP_NUMBER="${FIRST_EP_NUMBER%% }"
LAST_EP_NUMBER="${LAST_EP_NUMBER%% }"
lg "FIRST EP NUMBER: $FIRST_EP_NUMBER"
lg "LAST EP NUMBER: $LAST_EP_NUMBER"
lg "Searching for episodes of $anime_id"
select_ep_result=$(check_episode "$anime_id-episode-1" "$anime_id" | sed -E 's/\-episode\-//')
lg "Select Episode Result: $select_ep_result"
FIRST_EP_NUMBER=$(printf "%s" "$select_ep_result" | tail -n 1)
LAST_EP_NUMBER=$(printf "%s" "$select_ep_result" | head -n 1)
lg "First episode: $FIRST_EP_NUMBER"
lg "Last episode: $LAST_EP_NUMBER"
}
# Select anime from query results
@ -544,6 +569,7 @@ anime_selection() {
searched=""
cnt=0
while read -r anime_id; do
anime_id=$(printf "%s" "$anime_id" | sed -E 's/\-episode\-.*//')
[[ -z "$menu" ]] && menu="$((cnt + 1)). $anime_id" ||
menu="$menu|$((cnt + 1)). $anime_id"
if ! check_db "search" "$anime_id"; then
@ -569,6 +595,7 @@ anime_selection() {
else
count=1
while read -r anime_id; do
anime_id=$(printf "%s" "$anime_id" | sed -E 's/\-episode\-.*//')
menu_line_alternate "$anime_id" "$count"
: count=$((count += 1))
done <<< "$search_results"
@ -582,6 +609,7 @@ anime_selection() {
count=1
while read -r anime_id; do
if [[ "$count" -eq "$choice" ]]; then
anime_id=$(printf "%s" "$anime_id" | sed -E 's/\-episode\-.*//')
selection_id=$anime_id
break
fi
@ -595,6 +623,7 @@ anime_selection() {
return 0
}
# select episode from query results
episode_selection() {
ep_choice_start=1
if ((IS_ROFI == 1)); then
@ -624,7 +653,7 @@ episode_selection() {
ep_choice_start=$(printf '%s\n' "${choice}" | awk '{print $1}')
ep_choice_end=$(printf '%s\n' "${choice}" | awk '{print $NF}')
lg "START: $ep_choice_start | END: $ep_choice_end"
elif ((LAST_EP_NUMBER > 1)); then
elif [ -n "$LAST_EP_NUMBER" ]; then
[[ "$is_download" -eq 1 ]] &&
inf "Range of episodes can be specified:" "start_number end_number"
inf "Anime:" "$anime_id"
@ -632,18 +661,9 @@ episode_selection() {
read -r ep_choice_start ep_choice_end
[[ -z "$ep_choice_end" ]] && ep_choice_end="$ep_choice_start"
fi
if [[ "$(echo "$ep_choice_start" | awk '{ printf substr($0, 1, 1) }')" = "h" ]]; then
lg "IS A HALF EPISODE"
half_ep=1
ep_choice_start=$(echo "$ep_choice_start" | awk '{ printf substr($0, 2) }')
ep_choice_end=$ep_choice_start
fi
if (((ep_choice_start <= 0 || ep_choice_start > LAST_EP_NUMBER) || ep_choice_end < ep_choice_start || ep_choice_end > LAST_EP_NUMBER)); then
die "Invalid episode/range entered: ep_start -> $ep_choice_start | ep_end -> $ep_choice_end"
fi
# if only one episode was entered, set ep_choice_end to empty string so only selected episode plays
# otherwise plays from ep 1 - ep_choice_start
((ep_choice_start == ep_choice_end)) && ep_choice_end=""
}
open_episode() {
@ -651,16 +671,11 @@ open_episode() {
episode=$2
ddir="$3"
if [ $half_ep -eq 1 ]; then
temp_ep=$episode
episode=$episode"-5"
fi
# Don't update watch history if downloading episode
insert_history "watch" "$anime_id" "$episode"
lg "Getting data for episode $episode"
dpage_link=$(get_dpage_link "$anime_id" "$episode")
dpage_link=$(get_dpage_link "$anime_id-episode-" "$episode")
if [[ -z "$dpage_link" ]]; then
die "Could not get download page link"
else
@ -669,11 +684,6 @@ open_episode() {
lg "Download link: $dpage_link"
lg "Video url: $video_url"
if [ $half_ep -eq 1 ]; then
episode=$temp_ep
half_ep=0
fi
if [ "$is_download" -eq 0 ]; then
kill "$PID" > /dev/null 2>&1
case "$PLAYER_FN" in
@ -729,9 +739,7 @@ stream() {
lg "Checking if anime: $anime_id has been searched before..."
if check_db "search" "$anime_id"; then
lg "$anime_id has been searched before"
search_results=$(search_anime $query) # want word splitting to account for both input cases
[ -z "$search_results" ] && die "No search results found"
lg "SEARCH RESULTS: $search_results"
search_results=$(process_search $query) # want word splitting to account for both input cases
if ! anime_selection "$search_results"; then
die "No anime selection found"
fi
@ -748,7 +756,6 @@ parse_args() {
quality=best
is_download=0
download_dir="."
half_ep=0
is_resume=0
while getopts 'd:Hsvq:cf:t:T:CQ:D:Sp:r' OPT; do
case "$OPT" in
@ -835,13 +842,9 @@ parse_args() {
done
}
########
# main #
########
main() {
case $scrape in
query)
BASE_URL="$(curl -s -L -o /dev/null -w "%{url_effective}\n" https://gogoanime.fi)"
stmt="SELECT anime_name FROM watch_history ORDER BY watch_date DESC LIMIT 1;"
((is_resume == 1)) && anime="$(run_stmt "$stmt")" || anime="$*"
if [ -z "$anime" ]; then
@ -851,7 +854,6 @@ main() {
fi
;;
history)
BASE_URL="$(curl -s -L -o /dev/null -w "%{url_effective}\n" https://gogoanime.fi)"
stmt="SELECT anime_name FROM search_history ORDER BY search_date DESC"
search_results="$(run_stmt "$stmt")"
[ -z "$search_results" ] && die "History is empty"