anicli.sh

531 lines · 22722 bytes · executable

  1#!/bin/sh
  2
  3version_number="4.8.6"
  4
  5# UI
  6
  7external_menu() {
  8    rofi "$1" -sort -dmenu -i -width 1500 -p "$2"
  9}
 10
 11launcher() {
 12    [ "$use_external_menu" = "0" ] && [ -z "$1" ] && set -- "+m" "$2"
 13    [ "$use_external_menu" = "0" ] && fzf "$1" --reverse --cycle --prompt "$2"
 14    [ "$use_external_menu" = "1" ] && external_menu "$1" "$2"
 15}
 16
 17nth() {
 18    stdin=$(cat -)
 19    [ -z "$stdin" ] && return 1
 20    line_count="$(printf "%s\n" "$stdin" | wc -l | tr -d "[:space:]")"
 21    [ "$line_count" -eq 1 ] && printf "%s" "$stdin" | cut -f2,3 && return 0
 22    prompt="$1"
 23    multi_flag=""
 24    [ $# -ne 1 ] && shift && multi_flag="$1"
 25    line=$(printf "%s" "$stdin" | cut -f1,3 | tr '\t' ' ' | launcher "$multi_flag" "$prompt" | cut -d " " -f 1)
 26    [ -n "$line" ] && printf "%s" "$stdin" | grep -E '^'"${line}"'($|[[:space:]])' | cut -f2,3 || exit 1
 27}
 28
 29die() {
 30    printf "\33[2K\r\033[1;31m%s\033[0m\n" "$*" >&2
 31    exit 1
 32}
 33
 34help_info() {
 35    printf "
 36    Usage:
 37    %s [options] [query]
 38    %s [query] [options]
 39    %s [options] [query] [options]
 40
 41    Options:
 42      -c, --continue
 43        Continue watching from history
 44      -d, --download
 45        Download the video instead of playing it
 46      -D, --delete
 47        Delete history
 48      -s, --syncplay
 49        Use Syncplay to watch with friends
 50      -S, --select-nth
 51        Select nth entry
 52      -q, --quality
 53        Specify the video quality
 54      -v, --vlc
 55        Use VLC to play the video
 56      -V, --version
 57        Show the version of the script
 58      -h, --help
 59        Show this help message and exit
 60      -e, --episode, -r, --range
 61        Specify the number of episodes to watch
 62      --dub
 63        Play dubbed version
 64      --rofi
 65        Use rofi instead of fzf for the interactive menu
 66      --skip
 67        Use ani-skip to skip the intro of the episode (mpv only)
 68      --no-detach
 69        Don't detach the player (useful for in-terminal playback, mpv only)
 70      --skip-title <title>
 71        Use given title as ani-skip query
 72      -N, --nextep-countdown
 73        Display a countdown to the next episode
 74      -U, --update
 75        Update the script
 76    Some example usages:
 77      %s -q 720p banana fish
 78      %s --skip --skip-title \"one piece\" -S 2 one piece
 79      %s -d -e 2 cyberpunk edgerunners
 80      %s --vlc cyberpunk edgerunners -q 1080p -e 4
 81      %s blue lock -e 5-6
 82      %s -e \"5 6\" blue lock
 83    \n" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}"
 84    exit 0
 85}
 86
 87version_info() {
 88    printf "%s\n" "$version_number"
 89    exit 0
 90}
 91
 92update_script() {
 93    update="$(curl -s -A "$agent" "https://raw.githubusercontent.com/pystardust/ani-cli/master/ani-cli")" || die "Connection error"
 94    update="$(printf '%s\n' "$update" | diff -u "$0" -)"
 95    if [ -z "$update" ]; then
 96        printf "Script is up to date :)\n"
 97    else
 98        if printf '%s\n' "$update" | patch "$0" -; then
 99            printf "Script has been updated\n"
100        else
101            die "Can't update for some reason!"
102        fi
103    fi
104    exit 0
105}
106
107# checks if dependencies are present
108dep_ch() {
109    for dep; do
110        command -v "$dep" >/dev/null || die "Program \"$dep\" not found. Please install it."
111    done
112}
113
114# SCRAPING
115
116# extract the video links from response of embed urls, extract mp4 links form m3u8 lists
117get_links() {
118    episode_link="$(curl -e "$allanime_refr" -s "https://${allanime_base}$*" -A "$agent" | sed 's|},{|\
119|g' | sed -nE 's|.*link":"([^"]*)".*"resolutionStr":"([^"]*)".*|\2 >\1|p;s|.*hls","url":"([^"]*)".*"hardsub_lang":"en-US".*|\1|p')"
120
121    case "$episode_link" in
122        *repackager.wixmp.com*)
123            extract_link=$(printf "%s" "$episode_link" | cut -d'>' -f2 | sed 's|repackager.wixmp.com/||g;s|\.urlset.*||g')
124            for j in $(printf "%s" "$episode_link" | sed -nE 's|.*/,([^/]*),/mp4.*|\1|p' | sed 's|,|\
125|g'); do
126                printf "%s >%s\n" "$j" "$extract_link" | sed "s|,[^/]*|${j}|g"
127            done | sort -nr
128            ;;
129        *vipanicdn* | *anifastcdn*)
130            if printf "%s" "$episode_link" | head -1 | grep -q "original.m3u"; then
131                printf "%s" "$episode_link"
132            else
133                extract_link=$(printf "%s" "$episode_link" | head -1 | cut -d'>' -f2)
134                relative_link=$(printf "%s" "$extract_link" | sed 's|[^/]*$||')
135                curl -e "$allanime_refr" -s "$extract_link" -A "$agent" | sed 's|^#.*x||g; s|,.*|p|g; /^#/d; $!N; s|\
136| >|' | sed "s|>|>${relative_link}|g" | sort -nr
137            fi
138            ;;
139        *) [ -n "$episode_link" ] && printf "%s\n" "$episode_link" ;;
140    esac
141    # echo $episode_link >> links
142    [ -z "$ANI_CLI_NON_INTERACTIVE" ] && printf "\033[1;32m%s\033[0m Links Fetched\n" "$provider_name" 1>&2
143}
144
145# innitialises provider_name and provider_id. First argument is the provider name, 2nd is the regex that matches that provider's link
146provider_init() {
147    provider_name=$1
148    provider_id=$(printf "%s" "$resp" | sed -n "$2" | head -1 | cut -d':' -f2 | sed 's/../&\
149/g' | sed 's/^01$/9/g;s/^08$/0/g;s/^05$/=/g;s/^0a$/2/g;s/^0b$/3/g;s/^0c$/4/g;s/^07$/?/g;s/^00$/8/g;s/^5c$/d/g;s/^0f$/7/g;s/^5e$/f/g;s/^17$/\//g;s/^54$/l/g;s/^09$/1/g;s/^48$/p/g;s/^4f$/w/g;s/^0e$/6/g;s/^5b$/c/g;s/^5d$/e/g;s/^0d$/5/g;s/^53$/k/g;s/^1e$/\&/g;s/^5a$/b/g;s/^59$/a/g;s/^4a$/r/g;s/^4c$/t/g;s/^4e$/v/g;s/^57$/o/g;s/^51$/i/g;' | tr -d '\n' | sed "s/\/clock/\/clock\.json/")
150}
151
152# +s links based on given provider
153generate_link() {
154    case $1 in
155        1) provider_init "wixmp" "/Default :/p" ;;     # wixmp(default)(m3u8)(multi) -> (mp4)(multi)
156        2) provider_init "dropbox" "/Sak :/p" ;;       # dropbox(mp4)(single)
157        3) provider_init "wetransfer" "/Kir :/p" ;;    # wetransfer(mp4)(single)
158        4) provider_init "sharepoint" "/S-mp4 :/p" ;;  # sharepoint(mp4)(single)
159        *) provider_init "gogoanime" "/Luf-mp4 :/p" ;; # gogoanime(m3u8)(multi)
160    esac
161    [ -n "$provider_id" ] && get_links "$provider_id"
162    # echo $links >> links
163}
164
165select_quality() {
166    case "$1" in
167        best) result=$(printf "%s" "$links" | head -n1) ;;
168        worst) result=$(printf "%s" "$links" | grep -E '^[0-9]{3,4}' | tail -n1) ;;
169        *) result=$(printf "%s" "$links" | grep -m 1 "$1") ;;
170    esac
171    [ -z "$result" ] && printf "Specified quality not found, defaulting to best\n" 1>&2 && result=$(printf "%s" "$links" | head -n1)
172    printf "%s" "$result" | cut -d'>' -f2
173}
174
175# gets embed urls, collects direct links into provider files, selects one with desired quality into $episode
176get_episode_url() {
177    # get the embed urls of the selected episode
178    episode_embed_gql="query (\$showId: String!, \$translationType: VaildTranslationTypeEnumType!, \$episodeString: String!) {    episode(        showId: \$showId        translationType: \$translationType        episodeString: \$episodeString    ) {        episodeString sourceUrls    }}"
179
180    resp=$(curl -e "$allanime_refr" -s -G "${allanime_api}/api" --data-urlencode "variables={\"showId\":\"$id\",\"translationType\":\"$mode\",\"episodeString\":\"$ep_no\"}" --data-urlencode "query=$episode_embed_gql" -A "$agent" | tr '{}' '\n' | sed 's|\\u002F|\/|g;s|\\||g' | sed -nE 's|.*sourceUrl":"--([^"]*)".*sourceName":"([^"]*)".*|\2 :\1|p')
181    # generate links into sequential files
182    cache_dir="$(mktemp -d)"
183    providers="1 2 3 4 5"
184    for provider in $providers; do
185        generate_link "$provider" >"$cache_dir"/"$provider" &
186    done
187    wait
188    # echo $links > links
189    # select the link with matching quality
190    links=$(cat "$cache_dir"/* | sed 's|^Mp4-||g;/http/!d' | sort -g -r -s)
191    rm -r "$cache_dir"
192    episode=$(select_quality "$quality")
193    echo "links : "$links >> logs
194    [ -z "$episode" ] && die "Episode not released!"
195}
196
197# search the query and give results
198search_anime() {
199    search_gql="query(        \$search: SearchInput        \$limit: Int        \$page: Int        \$translationType: VaildTranslationTypeEnumType        \$countryOrigin: VaildCountryOriginEnumType    ) {    shows(        search: \$search        limit: \$limit        page: \$page        translationType: \$translationType        countryOrigin: \$countryOrigin    ) {        edges {            _id name availableEpisodes __typename       }    }}"
200
201    curl -e "$allanime_refr" -s -G "${allanime_api}/api" --data-urlencode "variables={\"search\":{\"allowAdult\":false,\"allowUnknown\":false,\"query\":\"$1\"},\"limit\":40,\"page\":1,\"translationType\":\"$mode\",\"countryOrigin\":\"ALL\"}" --data-urlencode "query=$search_gql" -A "$agent" | sed 's|Show|\
202|g' | sed -nE "s|.*_id\":\"([^\"]*)\",\"name\":\"([^\"]*)\".*${mode}\":([1-9][^,]*).*|\1	\2 (\3 episodes)|p"
203}
204
205time_until_next_ep() {
206    animeschedule="https://animeschedule.net"
207    curl -s -G "$animeschedule/api/v3/anime" --data-urlencode "q=$1" | sed 's|"id"|\n|g' | sed -nE 's|.*,"route":"([^"]*)","premier.*|\1|p' | while read -r anime; do
208        data=$(curl -s "$animeschedule/anime/$anime" | sed '1,/"anime-header-list-buttons-wrapper"/d' | sed -nE 's|.*countdown-time-raw" datetime="([^"]*)">.*|Next Raw Release: \1|p;s|.*countdown-time" datetime="([^"]*)">.*|Next Sub Release: \1|p;s|.*english-title">([^<]*)<.*|English Title: \1|p;s|.*main-title".*>([^<]*)<.*|Japanese Title: \1|p')
209        status="Ongoing"
210        color="33"
211        printf "%s\n" "$data"
212        ! (printf "%s\n" "$data" | grep -q "Next Raw Release:") && status="Finished" && color="32"
213        printf "Status:  \033[1;%sm%s\033[0m\n---\n" "$color" "$status"
214    done
215    exit 0
216}
217
218# get the episodes list of the selected anime
219episodes_list() {
220    episodes_list_gql="query (\$showId: String!) {    show(        _id: \$showId    ) {        _id availableEpisodesDetail    }}"
221
222    curl -e "$allanime_refr" -s -G "${allanime_api}/api" --data-urlencode "variables={\"showId\":\"$*\"}" --data-urlencode "query=$episodes_list_gql" -A "$agent" | sed -nE "s|.*$mode\":\[([0-9.\",]*)\].*|\1|p" | sed 's|,|\
223|g; s|"||g' | sort -n -k 1
224}
225
226# PLAYING
227
228process_hist_entry() {
229    ep_list=$(episodes_list "$id")
230    ep_no=$(printf "%s" "$ep_list" | sed -n "/^${ep_no}$/{n;p;}") 2>/dev/null
231    [ -n "$ep_no" ] && printf "%s\t%s - episode %s\n" "$id" "$title" "$ep_no"
232}
233
234update_history() {
235    if grep -q -- "$id" "$histfile"; then
236        sed -E "s/^[^\t]+\t${id}\t/${ep_no}\t${id}\t/" "$histfile" >"${histfile}.new"
237    else
238        cp "$histfile" "${histfile}.new"
239        printf "%s\t%s\t%s\n" "$ep_no" "$id" "$title" >>"${histfile}.new"
240    fi
241    mv "${histfile}.new" "$histfile"
242}
243
244download() {
245    case $1 in
246        *m3u8*)
247            if command -v "yt-dlp" >/dev/null; then
248                yt-dlp "$1" --no-skip-unavailable-fragments --fragment-retries infinite -N 16 -o "$download_dir/$2.mp4"
249            else
250                ffmpeg -loglevel error -stats -i "$1" -c copy "$download_dir/$2.mp4"
251            fi
252            ;;
253        *)
254            aria2c --enable-rpc=false --check-certificate=false --continue --summary-interval=0 -x 16 -s 16 "$1" --dir="$download_dir" -o "$2.mp4" --download-result=hide
255            ;;
256    esac
257}
258
259play_episode() {
260    [ "$skip_intro" = 1 ] && skip_flag="$(ani-skip -q "$mal_id" -e "$ep_no")"
261    [ -z "$episode" ] && get_episode_url
262    # shellcheck disable=SC2086
263    case "$player_function" in
264        debug)
265            [ -z "$ANI_CLI_NON_INTERACTIVE" ] && printf "All links:\n%s\nSelected link:\n" "$links"
266            printf "%s\n" "$episode"
267            ;;
268        mpv*)
269            # if [ "$no_detach" = 0 ]; then
270            #     nohup "$player_function" $skip_flag --force-media-title="${allanime_title}Episode ${ep_no}" "$episode" >/dev/null 2>&1 &
271            # else
272
273            echo "PLAY EPISODE VARIABLES START" >> logs
274            echo "player_function : " $player_function >> logs
275            echo "skip_flag : " $skip_flag >> logs
276            echo "allanime_title : " $allanime_title >> logs
277            echo "ep_no : " $ep_no >> logs
278            echo "episode : " $episode >> logs
279            echo "PLAY EPISODE VARIABLES END" >> logs
280            # episode="https://www114.vipanicdn.net/streamhls/28da4cc6217f37bdb61dcfa3c785827f/ep.1.1709229738.1080.m3u8"
281            "$player_function" $skip_flag --force-media-title="${allanime_title}Episode ${ep_no}" "$episode"
282            # fi
283            # ;;
284        # android_mpv) nohup am start --user 0 -a android.intent.action.VIEW -d "$episode" -n is.xyz.mpv/.MPVActivity >/dev/null 2>&1 & ;;
285        # android_vlc) nohup am start --user 0 -a android.intent.action.VIEW -d "$episode" -n org.videolan.vlc/org.videolan.vlc.gui.video.VideoPlayerActivity -e "title" "${allanime_title}Episode ${ep_no}" >/dev/null 2>&1 & ;;
286        # iina) nohup "$player_function" --no-stdin --keep-running --mpv-force-media-title="${allanime_title}Episode ${ep_no}" "$episode" >/dev/null 2>&1 & ;;
287        # flatpak_mpv) flatpak run io.mpv.Mpv --force-media-title="${allanime_title}Episode ${ep_no}" "$episode" >/dev/null 2>&1 & ;;
288        # vlc*) nohup "$player_function" --play-and-exit --meta-title="${allanime_title}Episode ${ep_no}" "$episode" >/dev/null 2>&1 & ;;
289        # *yncpla*) nohup "$player_function" "$episode" -- --force-media-title="${allanime_title}Episode ${ep_no}" >/dev/null 2>&1 & ;;
290        # download) "$player_function" "$episode" "${allanime_title}Episode ${ep_no}" ;;
291        # catt) nohup catt cast "$episode" >/dev/null 2>&1 & ;;
292        # iSH)
293        #     printf "\e]8;;vlc://%s\a~~~~~~~~~~~~~~~~~~~~\n~ Tap to open VLC ~\n~~~~~~~~~~~~~~~~~~~~\e]8;;\a\n" "$episode"
294        #     sleep 5
295        #     ;;
296        # *) nohup "$player_function" "$episode" >/dev/null 2>&1 & ;;
297    esac
298    replay="$episode"
299    unset episode
300    update_history
301    [ "$use_external_menu" = "1" ] && wait
302}
303
304play() {
305    start=$(printf "%s" "$ep_no" | grep -Eo '^(-1|[0-9]+(\.[0-9]+)?)')
306    end=$(printf "%s" "$ep_no" | grep -Eo '(-1|[0-9]+(\.[0-9]+)?)$')
307    [ "$start" = "-1" ] && ep_no=$(printf "%s" "$ep_list" | tail -n1) && unset start
308    [ -z "$end" ] || [ "$end" = "$start" ] && unset start end
309    [ "$end" = "-1" ] && end=$(printf "%s" "$ep_list" | tail -n1)
310    line_count=$(printf "%s\n" "$ep_no" | wc -l | tr -d "[:space:]")
311    if [ "$line_count" != 1 ] || [ -n "$start" ]; then
312        [ -z "$start" ] && start=$(printf "%s\n" "$ep_no" | head -n1)
313        [ -z "$end" ] && end=$(printf "%s\n" "$ep_no" | tail -n1)
314        range=$(printf "%s\n" "$ep_list" | sed -nE "/^${start}\$/,/^${end}\$/p")
315        [ -z "$range" ] && die "Invalid range!"
316        for i in $range; do
317            tput clear
318            ep_no=$i
319            printf "\33[2K\r\033[1;34mPlaying episode %s...\033[0m\n" "$ep_no"
320            play_episode
321        done
322    else
323        play_episode
324    fi
325    # moves upto stored positon and deletes to end
326    [ "$player_function" != "debug" ] && [ "$player_function" != "download" ] && tput rc && tput ed
327}
328
329# MAIN
330
331# setup
332agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0"
333allanime_refr="https://allanime.to"
334allanime_base="allanime.day"
335allanime_api="https://api.${allanime_base}"
336mode="${ANI_CLI_MODE:-sub}"
337download_dir="${ANI_CLI_DOWNLOAD_DIR:-.}"
338quality="${ANI_CLI_QUALITY:-best}"
339case "$(uname -a)" in
340    *Darwin*) player_function="${ANI_CLI_PLAYER:-iina}" ;;            # mac OS
341    *ndroid*) player_function="${ANI_CLI_PLAYER:-android_mpv}" ;;     # Android OS (termux)
342    *steamdeck*) player_function="${ANI_CLI_PLAYER:-flatpak_mpv}" ;;  # steamdeck OS
343    *MINGW* | *WSL2*) player_function="${ANI_CLI_PLAYER:-mpv.exe}" ;; # Windows OS
344    *ish*) player_function="${ANI_CLI_PLAYER:-iSH}" ;;                # iOS (iSH)
345    *) player_function="${ANI_CLI_PLAYER:-mpv}" ;;                    # Linux OS
346esac
347
348no_detach="${ANI_CLI_NO_DETACH:-0}"
349use_external_menu="${ANI_CLI_EXTERNAL_MENU:-0}"
350skip_intro="${ANI_CLI_SKIP_INTRO:-0}"
351# shellcheck disable=SC2154
352skip_title="$ANI_CLI_SKIP_TITLE"
353[ -t 0 ] || use_external_menu=1
354hist_dir="${ANI_CLI_HIST_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/ani-cli}"
355[ ! -d "$hist_dir" ] && mkdir -p "$hist_dir"
356histfile="$hist_dir/ani-hsts"
357[ ! -f "$histfile" ] && : >"$histfile"
358search="${ANI_CLI_DEFAULT_SOURCE:-scrape}"
359
360while [ $# -gt 0 ]; do
361    case "$1" in
362        -v | --vlc)
363            case "$(uname -a)" in
364                *ndroid*) player_function="android_vlc" ;;
365                MINGW* | *WSL2*) player_function="vlc.exe" ;;
366                *ish*) player_function="iSH" ;;
367                *) player_function="vlc" ;;
368            esac
369            ;;
370        -s | --syncplay)
371            case "$(uname -s)" in
372                Darwin*) player_function="/Applications/Syncplay.app/Contents/MacOS/syncplay" ;;
373                MINGW* | *Msys)
374                    export PATH="$PATH":"/c/Program Files (x86)/Syncplay/"
375                    player_function="syncplay.exe"
376                    ;;
377                *) player_function="syncplay" ;;
378            esac
379            ;;
380        -q | --quality)
381            [ $# -lt 2 ] && die "missing argument!"
382            quality="$2"
383            shift
384            ;;
385        -S | --select-nth)
386            [ $# -lt 2 ] && die "missing argument!"
387            index="$2"
388            shift
389            ;;
390        -c | --continue) search=history ;;
391        -d | --download) player_function=download ;;
392        -D | --delete)
393            : >"$histfile"
394            exit 0
395            ;;
396        -V | --version) version_info ;;
397        -h | --help) help_info ;;
398        -e | --episode | -r | --range)
399            [ $# -lt 2 ] && die "missing argument!"
400            ep_no="$2"
401            [ -n "$index" ] && ANI_CLI_NON_INTERACTIVE=1 #Checks for -S presence
402            shift
403            ;;
404        --dub) mode="dub" ;;
405        --no-detach) no_detach=1 ;;
406        --rofi) use_external_menu=1 ;;
407        --skip) skip_intro=1 ;;
408        --skip-title)
409            [ $# -lt 2 ] && die "missing argument!"
410            skip_title="$2"
411            shift
412            ;;
413        -N | --nextep-countdown) search=nextep ;;
414        -U | --update) update_script ;;
415        *) query="$(printf "%s" "$query $1" | sed "s|^ ||;s| |+|g")" ;;
416    esac
417    shift
418done
419[ "$use_external_menu" = "0" ] && multi_selection_flag="${ANI_CLI_MULTI_SELECTION:-"-m"}"
420[ "$use_external_menu" = "1" ] && multi_selection_flag="${ANI_CLI_MULTI_SELECTION:-"-multi-select"}"
421printf "\33[2K\r\033[1;34mChecking dependencies...\033[0m\n"
422dep_ch "curl" "sed" "grep" || true
423[ "$skip_intro" = 1 ] && (dep_ch "ani-skip" || true)
424if [ -z "$ANI_CLI_NON_INTERACTIVE" ]; then dep_ch fzf || true; fi
425case "$player_function" in
426    debug) ;;
427    download) dep_ch "ffmpeg" "aria2c" ;;
428    flatpak*)
429        dep_ch "flatpak"
430        flatpak info io.mpv.Mpv >/dev/null 2>&1 || die "Program \"mpv (flatpak)\" not found. Please install it."
431        ;;
432    android*) printf "\33[2K\rChecking of players on Android is disabled\n" ;;
433    *iSH*) printf "\33[2K\rChecking of players on iOS is disabled\n" ;;
434    *) dep_ch "$player_function" ;;
435esac
436
437# # searching
438# case "$search" in
439#     history)
440#         anime_list=$(while read -r ep_no id title; do process_hist_entry & done <"$histfile")
441#         wait
442#         [ -z "$anime_list" ] && die "No unwatched series in history!"
443#         result=$(printf "%s" "$anime_list" | nl -w 2 | sed 's/^[[:space:]]//' | nth "Select anime: " | cut -f1)
444#         echo "result : " $result >> logs
445#         [ -z "$result" ] && exit 1
446#         resfile="$(mktemp)"
447#         grep "$result" "$histfile" >"$resfile"
448#         read -r ep_no id title <"$resfile"
449#         ep_list=$(episodes_list "$id")
450#         ep_no=$(printf "%s" "$ep_list" | sed -n "/^${ep_no}$/{n;p;}") 2>/dev/null
451#         allanime_title="$(printf "%s" "$title" | cut -d'(' -f1 | tr -d '[:punct:]')"
452#         ;;
453#     *)
454        # if [ "$use_external_menu" = "0" ]; then
455        #     while [ -z "$query" ]; do
456        #         printf "\33[2K\r\033[1;36mSearch anime: \033[0m" && read -r query
457        #     done
458        # else
459            # [ -z "$query" ] && query=$(printf "" | external_menu "" "Search anime: ")
460            # [ -z "$query" ] && exit 1
461        # fi
462        # for checking new releases by specifying anime name
463        # [ "$search" = "nextep" ] && time_until_next_ep "$query"
464
465        # query=$(printf "%s" "$query" | sed "s| |+|g")
466        # echo "query : " $query >> logs
467        
468        ####################################################################################################################
469        
470        query="hunter"
471        anime_list=$(search_anime "$query")
472        
473        echo "anime_list : "$anime_list >> logs
474        
475        [ -z "$anime_list" ] && die "No results found!"
476        [ "$index" -eq "$index" ] 2>/dev/null && result=$(printf "%s" "$anime_list" | sed -n "${index}p")
477        [ -z "$index" ] && result=$(printf "%s" "$anime_list" | nl -w 2 | sed 's/^[[:space:]]//' | nth "Select anime: ")
478        [ -z "$result" ] && exit 1
479        
480        echo "result : " $result >> logs
481
482        title=$(printf "%s" "$result" | cut -f2)
483
484        echo "title : " $title >> logs
485        
486        allanime_title="$(printf "%s" "$title" | cut -d'(' -f1 | tr -d '[:punct:]')"
487        
488        echo "allanime_title : " $allanime_title >> logs
489        
490        id=$(printf "%s" "$result" | cut -f1)
491
492        echo "id : " $id >> logs
493        
494        ep_list=$(episodes_list "$id")
495        
496        echo "ep_list : " $ep_list >> logs
497        
498        [ -z "$ep_no" ] && ep_no=$(printf "%s" "$ep_list" | nth "Select episode: " "$multi_selection_flag")
499        [ -z "$ep_no" ] && exit 1
500        # ;;
501# esac
502# [ "$skip_intro" = 1 ] && mal_id="$(ani-skip -q "${skip_title:-${title}}")"
503
504# moves the cursor up one line and clears that line
505tput cuu1 && tput el
506# stores the positon of cursor
507tput sc
508
509# playback & loop
510
511play
512    # echo $links > link
513
514# [ "$player_function" = "download" ] || [ "$player_function" = "debug" ] && exit 0
515
516# while cmd=$(printf "next\nreplay\nprevious\nselect\nchange_quality\nquit" | nth "Playing episode $ep_no of $title... "); do
517#     case "$cmd" in
518#         next) ep_no=$(printf "%s" "$ep_list" | sed -n "/^${ep_no}$/{n;p;}") 2>/dev/null ;;
519#         replay) episode="$replay" ;;
520#         previous) ep_no=$(printf "%s" "$ep_list" | sed -n "/^${ep_no}$/{g;1!p;};h") 2>/dev/null ;;
521#         select) ep_no=$(printf "%s" "$ep_list" | nth "Select episode: " "$multi_selection_flag") ;;
522#         change_quality)
523#             episode=$(printf "%s" "$links" | launcher)
524#             quality=$(printf "%s" "$episode" | grep -oE "^[0-9]+")
525#             episode=$(printf "%s" "$episode" | cut -d'>' -f2)
526#             ;;
527#         *) exit 0 ;;
528#     esac
529#     [ -z "$ep_no" ] && die "Out of range"
530#     # play
531# done