Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate to newer api version #2

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 98 additions & 52 deletions anilibria-cli
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ CONTACTS="Telegram <https://t.me/Loureas>"

# AniLibria.TV API prefix, version and URL
API_PREFIX="v"
API_VERSION=2
API_VERSION=3
API_URL="https://api.anilibria.tv/${API_PREFIX}${API_VERSION}"

# Video quality
Expand Down Expand Up @@ -65,9 +65,12 @@ Usage:
$NAME [-h | -V | -U]

Other:
-h Print this help message
-V Print version and about POSIX script
-U Update script from GitHub repository
-h -- Print this help message
-V -- Print version and about POSIX script
-U -- Update script from GitHub repository
-m cmd -- Use external menu for titles (use {PROMPT} placeholder to pass prompt for external menu)
-M cmd -- Use external menu for episodes (use {PROMPT} placeholder to pass prompt for external menu)
-p cmd -- Use external prompt (use {PROMPT} placeholder to pass prompt for external menu)

Search:
Search queries are performed in English and Russian language.
Expand Down Expand Up @@ -224,31 +227,6 @@ prompt () {
done
}

prompt_sel () {
local count=$(wc -l <<< $1)
while true; do
printf "${C_BLUE}%s${C_RESET} ${C_MAGENTA}[${C_GREEN}1${C_RESET}${C_BOLD}-${C_GREEN}%s${C_MAGENTA}]${C_BLUE}: ${C_YELLOW}" "$2" $count
read -r
tput sgr0
[ -z "$REPLY" ] && print_red "Empty choice" && continue
grep -qi q <<< $REPLY && die 0 "Quit..."
grep -qi p <<< $REPLY && REPLY=p && return 0

[ "$REPLY" = "-1" ] && REPLY=$count && break

if ! [ "$REPLY" -eq "$REPLY" ] 2>/dev/null; then
print_red "Invalid choice entered"
continue
fi
if ! [ "$REPLY" -ge 1 ] || ! [ "$REPLY" -le $count ]; then
print_red "Out of range"
continue
fi
break
done
REPLY=$(sed -n "${REPLY}p" <<< $1)
}

prompt_episode () {
local start=$2
local end=$3
Expand All @@ -263,7 +241,7 @@ prompt_episode () {
tput sgr0
[ -z "$REPLY" ] && print_red "Empty choice" && continue
grep -qi q <<< $REPLY && die 0 "Quit..."
grep -qi p <<< $REPLY && REPLY=p && return 0
grep -qi p <<< $REPLY && unset REPLY && return 0

[ "$REPLY" = "-1" ] && REPLY=$end && return 0

Expand Down Expand Up @@ -307,16 +285,70 @@ prompt_episode () {
done
}

# Wraps any external menu (such as fzf, dmenu, rofi, etc.) such that
# it can be used in place of menu_selection function
# use {PROMPT} placeholder to pass prompt for external menu

external_prompt_wrapper () {
local external_prompt="${EXTERNAL_PROMPT//\{PROMPT\}/$1}"
REPLY=$(eval $external_prompt)
}

external_menu_wrapper () {
local external_menu="${EXTERNAL_MENU//\{PROMPT\}/$2}"
REPLY=$(eval $external_menu <<< "$1")
}

set_menu () {
if command_exists fzf; then
echo 'fzf --prompt "{PROMPT}: "'
fi
}

external_multi_menu_wrapper () {
local external_multi_menu="${EXTERNAL_MULTI_MENU//\{PROMPT\}/$1}"
REPLY=$(seq "$2" "$3" | eval $external_multi_menu)
}

set_multi_menu() {
if command_exists fzf; then
echo 'fzf -m --prompt "{PROMPT}: "'
fi
}

# Selections
menu_selection () {
local count=1
local count=0
[ -z "$1" ] && return 1 # Returns 1 if string is empty
while read -r line; do
printf "${C_CYAN}[${C_YELLOW}%s${C_CYAN}]${C_RESET} %s\n" "$count" "$line"
: $(( count+=1 ))
printf "${C_CYAN}[${C_YELLOW}%s${C_CYAN}]${C_RESET} %s\n" "$count" "$line"
done <<< "$1"
printf "%b\n" "${C_CYAN}[${C_YELLOW}p${C_CYAN}]${C_RESET} Back" \
"${C_CYAN}[${C_YELLOW}q${C_CYAN}]${C_RESET} Quit"

while true; do
printf "${C_BLUE}%s${C_RESET} ${C_MAGENTA}[${C_GREEN}1${C_RESET}${C_BOLD}-${C_GREEN}%s${C_MAGENTA}]${C_BLUE}: ${C_YELLOW}" "$2" $count
read -r
tput sgr0
[ -z "$REPLY" ] && print_red "Empty choice" && continue
grep -qi q <<< $REPLY && die 0 "Quit..."
grep -qi p <<< $REPLY && unset REPLY && return 0

[ "$REPLY" -lt 0 ] && REPLY=$((count + 1 + REPLY))

if ! [ "$REPLY" -eq "$REPLY" ] 2>/dev/null; then
print_red "Invalid choice entered"
continue
fi

if [ "$REPLY" -lt 1 ] || [ "$REPLY" -gt $count ]; then
print_red "Out of range"
continue
fi
break
done
REPLY=$(sed -n "${REPLY}p" <<< $1)
}

### PLAYER SETUP ###
Expand Down Expand Up @@ -398,16 +430,24 @@ search_titles () {
res=$(${FETCH} \
--data-urlencode "search=${1}" \
--data-urlencode "limit=-1" \
"${API_URL}/searchTitles" 2> /dev/null) || return $?
"${API_URL}/title/search" 2> /dev/null) || return $?
echo $res
}

### START MAIN SCRIPT ###

trap 'die 1 "\nInterrupted by user"' INT HUP

# Check dependencies
OS=$(os_detect)
check_req_dep || die $?
FETCH="curl -s -G --connect-timeout $TIMEOUT --retry $RETRY"
PLAYER=$(check_players) || die $?
EXTERNAL_MENU=$(set_menu)
EXTERNAL_MULTI_MENU=$(set_multi_menu)

# Parse options
while getopts ":hVU" opt; do
while getopts ":hVUm:M:p:" opt; do
case $opt in
h)
SHOW_HELP=true
Expand All @@ -418,6 +458,15 @@ while getopts ":hVU" opt; do
U)
UPDATE_SCRIPT=true
;;
m)
EXTERNAL_MENU=${OPTARG}
;;
M)
EXTERNAL_MULTI_MENU=${OPTARG}
;;
p)
EXTERNAL_PROMPT=${OPTARG}
;;
?)
die 2 "Invalid option: -${OPTARG}\nType to print help message: $NAME -h"
;;
Expand All @@ -429,11 +478,9 @@ done
[ -n "$SHOW_HELP" ] && help_text && die 0
[ -n "$SHOW_ABOUT" ] && about_text && die 0

# Check dependencies
OS=$(os_detect)
check_req_dep || die $?
FETCH="curl -s -G --connect-timeout $TIMEOUT --retry $RETRY"
PLAYER=$(check_players) || die $?
[ -n "$EXTERNAL_MENU" ] && MENU=external_menu_wrapper || MENU=menu_selection
[ -n "$EXTERNAL_MULTI_MENU" ] && MULTI_MENU=external_multi_menu_wrapper || MULTI_MENU=prompt_episode
[ -n "$EXTERNAL_PROMPT" ] && PROMPT=external_prompt_wrapper || PROMPT=prompt

# Update script if the -U option is given
[ -n "$UPDATE_SCRIPT" ] && update && die 0
Expand All @@ -449,7 +496,8 @@ while true; do
search)

# Search query
prompt "Search anime"
$PROMPT "Search anime"
[ -z "$REPLY" ] && die 0
query=$REPLY

# Send request search
Expand All @@ -462,27 +510,25 @@ while true; do
[ $exit_code -eq 7 ] && die 4 "Failed to fetch: could not connect to host"
[ $exit_code -eq 28 ] && die 4 "Failed to fetch: connection timeout"
[ $exit_code -ne 0 ] && die 4 "Failed to fetch: unknown error: $exit_code"

names=$(jq -r '.[].names.ru' <<< $res | sort)
names=$(jq -r '.list | .[].names.ru' <<< $res | sort)
names_count=$(wc -l <<< $names)
[ -z "$names" ] && print_red "No anime found" || state="anime"
;;
anime)

tput clear
if [ $names_count -ne 1 ]; then
menu_selection "$names" # Choose anime
prompt_sel "$names" "Choose anime"
[ "$REPLY" = "p" ] && state="search" && continue
$MENU "$names" "Choose anime"
[ -z "$REPLY" ] && state="search" && continue
anime_ru=$REPLY
else
anime_ru=$names # Choose only one matched
fi
choosed=$(jq ".[] | select(.names.ru == \"${anime_ru}\")" <<< $res)
choosed=$(jq ".list | .[] | select(.names.ru == \"${anime_ru}\")" <<< $res)
anime_en=$(jq -r ".names.en" <<< $choosed)
host_player="https://$(jq -r ".player.host" <<< $choosed)"
ep_start=$(jq -r ".player.series.first" <<< $choosed)
ep_end=$(jq -r ".player.series.last" <<< $choosed)
ep_start=$(jq -r ".player.episodes.first" <<< $choosed)
ep_end=$(jq -r ".player.episodes.last" <<< $choosed)
echo -e "${C_CYAN}Choosed anime: ${C_YELLOW}${anime_ru}${C_RESET}"
state="episode"

Expand All @@ -495,8 +541,8 @@ while true; do
[ $names_count -eq 1 ] && state="search" || state="anime"
else
# Choose episode
prompt_episode "Choose episode" $ep_start $ep_end
if [ "$REPLY" = "p" ]; then
$MULTI_MENU "Choose episode" $ep_start $ep_end
if [ -z "$REPLY" ]; then
[ $names_count -eq 1 ] && state="search" || state="anime"
continue
fi
Expand All @@ -513,12 +559,12 @@ while true; do

# Priority on maximum quality
for q in ${QUALITY[@]}; do
url=$(jq -re ".player.playlist.\"${ep}\".hls.${q}" <<< $choosed) && break
url=$(jq -re ".player.list.\"${ep}\".hls.${q}" <<< $choosed) && break
done
[ -z "$url" ] && die 6 "Episode \"${ep}\" not found with any quality"

ep_array+=($ep)
url_array+=("${host_player}/${url}")
url_array+=("${host_player}${url}")

done

Expand Down