Repository navigation
Expand file tree
/
Copy pathani-cli
More file actions
executable file
·752 lines (687 loc) · 30.1 KB
/
Copy pathani-cli
File metadata and controls
executable file
·752 lines (687 loc) · 30.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
#!/bin/sh
version_number="5.0.2-anidb"
# UI
menu() {
# shellcheck disable=SC2086
case "$menu_program" in
fzf) fzf --reverse --cycle --prompt "$1" $2 $3 ;;
rofi) rofi -sort -dmenu -i -p "$1" $2 $3 ;;
dmenu) dmenu -l 20 -p "$1" $3 ;;
*) "$menu_program" $3 "$1" ;;
esac
}
nth() {
stdin=$(cat -)
[ -z "$stdin" ] && return 1
line_count="$(printf "%s\n" "$stdin" | wc -l | tr -d "[:space:]")"
[ "$line_count" -eq 1 ] && printf "%s" "$stdin" | cut -f2,3 && return 0
prompt="$1"
[ $# -ne 1 ] && multi_flag="$2"
line=$(printf "%s" "$stdin" | cut -f1,3 | tr '\t' ' ' | menu "$prompt" "$multi_flag" "$menu_extra_flags" | cut -d " " -f 1)
line_start=$(printf "%s" "$line" | head -n1)
line_end=$(printf "%s" "$line" | tail -n1)
[ -n "$line" ] || return 1
if [ "$line_start" = "$line_end" ]; then
printf "%s" "$stdin" | grep -E '^'"${line}"'($|[[:space:]])' | cut -f2,3 || return 1
else
printf "%s" "$stdin" | sed -n '/^'"${line_start}"'$/,/^'"${line_end}$"'/p' || return 1
fi
}
die() {
printf "\33[2K\r\033[1;31m%s\033[0m\n" "$*" >&2
exit 1
}
info() {
printf "\33[2K\r\033[1;34m%s\033[0m\n" "$*"
}
warn() {
printf "\33[2K\r\033[1;33m%s\033[0m\n" "$*" >&2
}
help_info() {
printf "
Usage:
%s [options] [query]
%s [query] [options]
%s [options] [query] [options]
Options:
-c, --continue
Continue watching from history
--resume
Resume playback from saved position (mpv only)
-d, --download
Download the video instead of playing it
--batch
Download all episodes of found anime (with progress)
-D, --delete
Delete history
-l, --logview
Show logs
-s, --syncplay
Use Syncplay to watch with friends
-S, --select-nth
Select nth entry
-q, --quality
Specify the video quality
-v, --vlc
Use VLC to play the video
-V, --version
Show the version of the script
-h, --help
Show this help message and exit
-e, --episode, -r, --range
Specify the number of episodes to watch
--dub
Play dubbed version
--rofi
Use rofi instead of fzf for the interactive menu
--dmenu
Use dmenu instead of fzf for the interactive menu
--skip
Use ani-skip to skip the intro of the episode (mpv only)
--no-detach
Don't detach the player (useful for in-terminal playback, mpv only)
--exit-after-play
Exit the player, and return the player exit code (useful for non interactive scenarios, mpv only)
-N, --nextep-countdown
Display a countdown to the next episode
-U, --update
Update the script
Some example usages:
%s -q 720p banana fish
%s --skip -S 2 one piece
%s -d -e 2 cyberpunk edgerunners
%s --batch one piece
%s --resume -c
%s --vlc cyberpunk edgerunners -q 1080p -e 4
%s blue lock -e 5-6
%s -e \"5 6\" blue lock
\n" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}" "${0##*/}"
exit 0
}
version_info() {
printf "%s\n" "$version_number"
exit 0
}
update_script() {
info "[branch:$branch] Checking for Update.."
update="$(curl --fail-with-body -sL -A "$agent" "https://raw.githubusercontent.com/VVAT3R/ani-cli/$branch/ani-cli")" || die "[branch:$branch] Connection error: $update"
new_version="$(printf "%s" "$update" | sed -nE 's|^version_number="([^"]+)"$|\1|p')"
[ -z "$new_version" ] && die "[branch:$branch] Invalid response."
printf "%s" "$update" | grep -q "^version_number" || die "[branch:$branch] Invalid Response."
update="$(printf "%s\n" "$update" | diff -u "$0" -)"
if [ -z "$update" ]; then
info "[branch:$branch] Script is up to date :) ($version_number)"
else
if printf "%s\n" "$update" | patch "$0" -; then
info "[branch:$branch] Updated: $version_number -> $new_version"
else
die "[branch:$branch] Can't update for some reason!"
fi
fi
exit 0
}
# checks if dependencies are present
dep_ch() {
for dep; do
command -v "${dep%% *}" >/dev/null || die "Program \"${dep%% *}\" not found. Please install it."
done
}
dep_ch_failover() {
[ -z "$1" ] && return 1
prog=$(printf "%s" "$1" | cut -d"," -f 1)
rest=$(printf "%s" "$1" | cut -s -d"," -f 2-)
command -v "$prog" >/dev/null 2>&1 && printf "%s" "$prog" && return 0
[ -e "$prog" ] && printf "%s" "$prog" && return 0
dep_ch_failover "$rest"
}
where_iina() {
[ -e "/Applications/IINA.app/Contents/MacOS/iina-cli" ] && echo "/Applications/IINA.app/Contents/MacOS/iina-cli" && return 0
printf "%s" "iina" && return 0
}
where_mpv() {
command -v "flatpak" >/dev/null && flatpak info io.mpv.Mpv >/dev/null 2>&1 && printf "%s" "flatpak_mpv" && return 0
printf "%s" "mpv" && return 0
}
cleanup() {
tput sgr0 # clears colors
rm -f "${histfile}.new" # remove temp logfile
}
# SCRAPING
select_quality() {
case "$1" in
best) result=$(printf "%s" "$links" | head -n1) ;;
worst) result=$(printf "%s" "$links" | grep -E '^[0-9]{3,4}' | tail -n1) ;;
*) result=$(printf "%s" "$links" | grep -m 1 "$1") ;;
esac
[ -z "$result" ] && warn "Specified quality not found, defaulting to best" && result=$(printf "%s" "$links" | head -n1)
refr_flag="--referrer=https://anidb.app"
episode=$(printf "%s" "$result" | cut -d'>' -f2)
}
# anidb.app: search, get episodes, extract m3u8
anidb_curl() {
_retries=3
_result=""
while [ $_retries -gt 0 ]; do
_result="$($curl_exe -sL --http1.1 \
--ciphers "$ciphers" --tls13-ciphers "$tls13_ciphers" \
-A "$agent" \
-H 'Accept-Language: en-US,en;q=0.9' \
-H 'Accept-Encoding: gzip, deflate' \
--compressed \
-H 'Sec-CH-UA: "Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"' \
-H 'Sec-CH-UA-Mobile: ?0' \
-H 'Sec-CH-UA-Platform: "Windows"' \
-H 'DNT: 1' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'Referer: https://anidb.app' \
--max-time 10 "$@" 2>/dev/null | tr -d '\r')"
[ -n "$_result" ] && printf '%s' "$_result" && return 0
_retries=$((_retries - 1))
[ $_retries -gt 0 ] && sleep 1
done
return 1
}
search_anidb() {
_query="$(printf '%s' "$1" | sed 's/ /+/g')"
_page="$(anidb_curl "https://anidb.app/search/suggestions?q=${_query}" -A "$agent" --max-time 10 | tr '\n' ' ' | sed 's/<a href/\n<a href/g')"
printf "%s" "$_page" | grep -qi "Just a moment" && die "Blocked by cloudflare"
printf '%s' "$_page" | sed -nE 's|.*anime/([a-z0-9-]+-[0-9]+)".*alt="([^"]+)".*|\1\t\2|p' | sed -e "s|&#039;|'|g" -e "s|'|'|g" -e 's|&quot;|"|g' -e 's|"|"|g'
}
anidb_desc() {
_anime_link="$1"
_page="$(anidb_curl "https://anidb.app/anime/${_anime_link}" -A "$agent" --max-time 10 | tr '\n' ' ' | sed 's|<a href|\n<a href|g')"
mal_id="$(printf "%s" "$_page" | sed -nE 's|.*https://myanimelist.net/anime/([0-9]+)/.*|\1|p')"
seasons="$(printf "%s" "$_page" | sed -n '/>Seasons</,/>Details</p' | sed -nE 's|.*anime/([^"]+-[0-9]+)"[^>]*title="([^"]+)".*|\1\t\2|p' | sed -e "s|'|'|g")"
[ -n "$seasons" ] && seasons_option="\nseasons"
}
# cache the episodes JSON per anime to avoid refetching it for every episode
ep_json=""
ep_json_id=""
anime_episodes_json() {
_anime_id="$1"
if [ "$ep_json_id" != "$_anime_id" ]; then
ep_json="$(anidb_curl "https://anidb.app/api/frontend/anime/${_anime_id}/episodes" -A "$agent" -H "Accept: application/json" --max-time 10)"
ep_json_id="$_anime_id"
fi
}
episodes_list_anidb() {
_anime_id="${1##*-}"
anime_episodes_json "$_anime_id"
printf '%s' "$ep_json" | sed 's/},{/}\n{/g' | sed -nE 's|.*"number":([0-9]+).*|\1|p' | sort -n -u
}
get_anidb_m3u8() {
_anime_id="${1##*-}"
_ep_no="$2"
_ep_json="${3:-}"
[ -z "$_ep_json" ] && _ep_json="$(anidb_curl "https://anidb.app/api/frontend/anime/${_anime_id}/episodes" -A "$agent" -H "Accept: application/json" --max-time 10)"
_ep_id="$(printf '%s' "$_ep_json" | sed -nE "s|.*\"id\":([0-9]+),\"number\":${_ep_no}[,}].*|\1|p" | head -1)"
[ -z "$_ep_id" ] && return 1
_lang_pref="jpn"
[ "$mode" = "dub" ] && _lang_pref="eng"
_lang_json="$(anidb_curl "https://anidb.app/api/frontend/episode/${_ep_id}/languages" -A "$agent" -H "Accept: application/json" --max-time 10)"
_embed_url="$(printf '%s' "$_lang_json" | sed -nE "s|.*\"code\":\"${_lang_pref}\"[^}]*\"embed_url\":\"([^\"]*)\".*|\1|p;s|.*\"embed_url\":\"([^\"]*)\"[^}]*\"code\":\"${_lang_pref}\".*|\1|p" | head -1)"
[ -z "$_embed_url" ] && _embed_url="$(printf '%s' "$_lang_json" | sed -nE 's|.*"embed_url":"([^"]*)".*|\1|p' | head -1)"
_embed_url="$(printf '%s' "$_embed_url" | sed 's|\\/|/|g')"
[ -z "$_embed_url" ] && return 1
_page="$(anidb_curl "$_embed_url" -A "$agent" --max-time 10)"
printf '%s' "$_page" | sed -nE "s|.*file: '([^']*)'.*|\1|p" | head -1
}
# gets m3u8 stream URL from anidb.app and selects quality
get_episode_url() {
links=""
# try anidb.app
_anidb_id="${id##*-}"
if [ -n "$_anidb_id" ]; then
anime_episodes_json "$_anidb_id"
_m3u8="$(get_anidb_m3u8 "$_anidb_id" "$ep_no" "$ep_json")"
if [ -n "$_m3u8" ]; then
_m3u8_content="$(anidb_curl "$_m3u8" -A "$agent" --max-time 10)"
if printf '%s' "$_m3u8_content" | grep -q "EXTM3U"; then
links="$(printf '%s' "$_m3u8_content" | sed 's|^#EXT-X-STREAM.*x||g; s|,.*|p|g; /^#/d; $!N; s|\n| >|; /EXT-X-I-FRAME/d' | sort -nr)"
[ -n "$links" ] && printf "\033[1;32m%s\033[0m Links Fetched\n" "anidb.app" 1>&2
fi
fi
fi
select_quality "$quality"
if printf "%s" "$ep_list" | grep -q "^$ep_no$"; then
[ -z "$episode" ] && die "Episode is released, but no valid sources!"
else
[ -z "$episode" ] && die "Episode not released!"
fi
}
time_until_next_ep() {
animeschedule="https://animeschedule.net"
query="$(printf "%s\n" "$*" | tr ' ' '+')"
curl --max-time 5 -s -G "$animeschedule/api/v3/anime" --data "q=${query}" | sed 's|"id"|\n|g' | sed -nE 's|.*,"route":"([^"]*)","premier.*|\1|p' | while read -r anime; do
data=$(curl --max-time 5 -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')
status="Ongoing"
color="33"
printf "%s\n" "$data"
! (printf "%s\n" "$data" | grep -q "Next Raw Release:") && status="Finished" && color="32"
printf "Status: \033[1;%sm%s\033[0m\n---\n" "$color" "$status"
done
exit 0
}
# PLAYING
process_hist_entry() {
ep_list=$(episodes_list_anidb "$id")
latest_ep=$(printf "%s\n" "$ep_list" | tail -n1)
title=$(printf "%s\n" "$title" | sed "s|[0-9]\+ episodes|${latest_ep} episodes|")
ep_no=$(printf "%s" "$ep_list" | sed -n "/^${ep_no}$/{n;p;}")
[ -n "$ep_no" ] && printf "%s\t%s - episode %s\n" "$id" "$title" "$ep_no"
[ -n "$ep_no" ] || printf "%s\t%s - episode %s (up to date)\n" "$id" "$title" "$latest_ep"
}
update_history() {
_pos="${1:-0}"
if grep -q " ${id} " "$histfile"; then
safe_title=$(printf '%s' "$title" | sed 's/[&\|]/\\&/g')
sed -E "s|^[^ ]+ ${id} [^ ]+( [^ ]+)?$|${ep_no} ${id} ${safe_title} ${_pos}|" "$histfile" >"${histfile}.new"
else
cp "$histfile" "${histfile}.new"
printf "%s\t%s\t%s\t%s\n" "$ep_no" "$id" "$title" "$_pos" >>"${histfile}.new"
fi
mv "${histfile}.new" "$histfile"
}
backup_old_hist() {
backupfile="${histfile}.v4"
: >"${histfile}.new"
while IFS=" " read -r _ep _id _title _pos; do
case "$_id" in
*-*) printf "%s\t%s\t%s\t%s\n" "$_ep" "$_id" "$_title" "$_pos" >>"${histfile}.new" ;;
*) printf "%s\t%s\t%s\t%s\n" "$_ep" "$_id" "$_title" "$_pos" >>"$backupfile" ;;
esac
done <"$histfile"
mv "${histfile}.new" "$histfile"
}
get_mpv_position() {
_socket="/tmp/ani-cli-mpv-$$"
if [ -S "$_socket" ]; then
_pos=$(printf '{ "command": ["get_property", "time-pos"] }\n' | socat - UNIX-CONNECT:"$_socket" 2>/dev/null | sed -nE 's|.*"data":([0-9.]+).*|\1|p')
[ -n "$_pos" ] && printf "%s" "$_pos" && return 0
fi
printf "0"
}
resume_episode() {
_hist_id="$1"
_pos=$(grep -- "$_hist_id" "$histfile" | cut -f4)
[ -z "$_pos" ] || [ "$_pos" = "0" ] && return 1
printf "%.0f" "$_pos"
}
download() {
_dl_refr="${refr_flag#--referrer=}"
case $1 in
*m3u8*)
command -v "yt-dlp" >/dev/null && yt-dlp --referer "$_dl_refr" "$1" --no-skip-unavailable-fragments --fragment-retries infinite -N 16 -o "$download_dir/$2.mp4" && return 0
ffmpeg -extension_picky 0 -referer "$_dl_refr" -loglevel error -stats -i "$1" -c copy "$download_dir/$2.mp4"
;;
*)
# shellcheck disable=SC2086
aria2c --referer="$_dl_refr" --enable-rpc=false --check-certificate=false --continue $iSH_DownFix --summary-interval=0 -x 16 -s 16 "$1" --dir="$download_dir" -o "$2.mp4" --download-result=hide
;;
esac
}
android_mpv() {
dir="/storage/emulated/0/mpv"
file="$dir/mpv.config.mp4"
if [ -w "${dir%*/mpv}" ]; then
[ -d "${dir}" ] || mkdir "${dir}"
printf "%s" "$3" | sed 's| --|\n|g; s|^--||g' >"$file"
fi
am start --user 0 -a android.intent.action.VIEW -d "$1" -n is.xyz.mpv/.MPVActivity -e "title" "$2" >/dev/null 2>&1
unset dir file
}
play_episode() {
[ "$log_episode" = 1 ] && [ "$player_function" != "debug" ] && [ "$player_function" != "download" ] && command -v logger >/dev/null && logger -t ani-cli "${anime_title}${ep_no}"
[ "$skip_intro" = 1 ] && skip_flag="$(ani-skip -q "$mal_id" -e "$canon_ep_no")"
[ -z "$episode" ] && get_episode_url
# wait for the previous player to close before starting a new one
wait 2>/dev/null
# resume position
_resume_pos=""
if [ "$resume_enabled" = 1 ]; then
_resume_pos=$(resume_episode "$id")
[ -n "$_resume_pos" ] && [ "$_resume_pos" != "0" ] && warn "Resuming from $_resume_pos seconds..."
fi
# mpv socket for position tracking
_mpv_socket=""
case "$player_function" in
mpv*|flatpak_mpv)
_mpv_socket="/tmp/ani-cli-mpv-$$"
;;
esac
# shellcheck disable=SC2086
case "$player_function" in
debug)
printf "All links:\n%s\nSelected link:\n" "$links"
printf "%s\n" "$episode"
;;
mpv*)
_mpv_args=""
[ -n "$_mpv_socket" ] && rm -f "$_mpv_socket"
[ -n "$_mpv_socket" ] && _mpv_args="--input-ipc-server=$_mpv_socket"
[ -n "$_resume_pos" ] && [ "$_resume_pos" != "0" ] && _mpv_args="$_mpv_args --start=$_resume_pos"
if [ "$no_detach" = 0 ]; then
nohup $player_function $_mpv_args $skip_flag --tls-verify=no --force-media-title="${anime_title}Episode ${ep_no}" "$episode" $refr_flag $player_extra_flags >/dev/null 2>&1 &
else
$player_function $_mpv_args $skip_flag $refr_flag --tls-verify=no --force-media-title="${anime_title}Episode ${ep_no}" "$episode" $player_extra_flags
mpv_exitcode=$?
[ "$exit_after_play" = 1 ] && [ -z "$range" ] && exit "$mpv_exitcode"
fi
;;
android_mpv) "$player_function" "$episode" "${anime_title}Episode ${ep_no}" "--tls-verify=no $refr_flag" $player_extra_flags 2>&1 & ;;
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" "${anime_title}Episode ${ep_no}" $player_extra_flags >/dev/null 2>&1 & ;;
*iina*)
[ -n "$refr_flag" ] && refr_flag="--mpv-${refr_flag#--}"
if pgrep -f "IINA" >/dev/null 2>&1; then
nohup $player_function --no-stdin --mpv-tls-verify=no --mpv-force-media-title="${anime_title}Episode ${ep_no}" $refr_flag "$episode" $player_extra_flags >/dev/null 2>&1 &
else
nohup $player_function --no-stdin --mpv-tls-verify=no --keep-running --mpv-force-media-title="${anime_title}Episode ${ep_no}" $refr_flag "$episode" $player_extra_flags >/dev/null 2>&1 &
fi
;;
flatpak_mpv) flatpak run io.mpv.Mpv --input-ipc-server="$_mpv_socket" --start="$_resume_pos" --tls-verify=no --force-media-title="${anime_title}Episode ${ep_no}" "$episode" $refr_flag $player_extra_flags >/dev/null 2>&1 & ;;
vlc*) nohup $player_function --http-referrer="${refr_flag#--referrer=}" --play-and-exit --meta-title="${anime_title}Episode ${ep_no}" "$episode" $player_extra_flags >/dev/null 2>&1 & ;;
*yncpla*) nohup $player_function "$episode" $player_extra_flags -- --tls-verify=no --force-media-title="${anime_title}Episode ${ep_no}" $refr_flag >/dev/null 2>&1 & ;;
download) "$player_function" "$episode" "${anime_title}Episode ${ep_no}" $player_extra_flags ;;
catt) nohup catt cast "$episode" $player_extra_flags >/dev/null 2>&1 & ;;
iSH)
printf "\e]8;;vlc://%s\a~~~~~~~~~~~~~~~~~~~~\n~ Tap to open VLC ~\n~~~~~~~~~~~~~~~~~~~~\e]8;;\a\n" "$episode"
sleep 5
;;
*) nohup $player_function "$episode" $player_extra_flags >/dev/null 2>&1 & ;;
esac
replay="$episode"
unset episode
# save playback position
case "$player_function" in
mpv*|flatpak_mpv)
if [ "$menu_program" != "fzf" ]; then
_final_pos=0
while kill -0 "$!" 2>/dev/null; do
_pos=$(get_mpv_position)
[ -n "$_pos" ] && [ "$_pos" != "0" ] && _final_pos=$_pos
sleep 2
done
update_history "$_final_pos"
fi
;;
*)
update_history "0"
;;
esac
[ "$menu_program" != "fzf" ] && wait
}
play() {
start=$(printf "%s" "$ep_no" | grep -Eo '^(-1|[0-9]+(\.[0-9]+)?)')
end=$(printf "%s" "$ep_no" | grep -Eo '(-1|[0-9]+(\.[0-9]+)?)$')
[ "$start" = "-1" ] && ep_no=$(printf "%s" "$ep_list" | tail -n1) && unset start
[ -z "$end" ] || [ "$end" = "$start" ] && unset start end
[ "$end" = "-1" ] && end=$(printf "%s" "$ep_list" | tail -n1)
line_count=$(printf "%s\n" "$ep_no" | wc -l | tr -d "[:space:]")
if [ "$line_count" != 1 ] || [ -n "$start" ]; then
[ -z "$start" ] && start=$(printf "%s\n" "$ep_no" | head -n1)
[ -z "$end" ] && end=$(printf "%s\n" "$ep_no" | tail -n1)
range=$(printf "%s\n" "$ep_list" | sed -nE "/^${start}\$/,/^${end}\$/p")
[ -z "$range" ] && die "Invalid range!"
# batch download progress
if [ "$batch_mode" = 1 ]; then
_total=$(printf "%s\n" "$range" | wc -l | tr -d " ")
_count=0
fi
for i in $range; do
tput clear
ep_no=$i
canon_ep_no="$(printf "%s" "$ep_list" | sed -n "/^${ep_no}$/=")"
if [ "$batch_mode" = 1 ]; then
_count=$((_count + 1))
printf "\33[2K\r\033[1;34m[%s/%s] Downloading episode %s...\033[0m\n" "$_count" "$_total" "$ep_no"
# skip if already downloaded
_fname="${anime_title}Episode ${ep_no}.mp4"
if [ -f "$download_dir/$_fname" ]; then
printf "\33[2K\r\033[1;33mSkipping episode %s (already exists)\033[0m\n" "$ep_no"
continue
fi
else
info "Playing episode $ep_no..."
fi
[ "$i" = "$end" ] && unset range
play_episode
done
else
canon_ep_no="$(printf "%s" "$ep_list" | sed -n "/^${ep_no}$/=")"
play_episode
fi
# moves up to stored position and deletes to end
[ "$player_function" != "debug" ] && [ "$player_function" != "download" ] && tput rc && tput ed
}
# MAIN
# setup
agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0"
# browser-ish transport, no curl-impersonate needed
ciphers='ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305'
tls13_ciphers='TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256'
curl_exe=$(dep_ch_failover "curl_chrome136,curl_firefox135,curl_ff117,curl") || die "Program curl not found"
mode="${ANI_CLI_MODE:-sub}"
download_dir="${ANI_CLI_DOWNLOAD_DIR:-.}"
log_episode="${ANI_CLI_LOG:-1}"
quality="${ANI_CLI_QUALITY:-best}"
case "$(uname -a | cut -d " " -f 1,3-)" in
*Darwin*) player_function="${ANI_CLI_PLAYER:-$(where_iina)}" ;; # mac OS
*ndroid*) player_function="${ANI_CLI_PLAYER:-android_mpv}" ;; # Android OS (termux)
*MINGW* | *WSL2*) player_function="${ANI_CLI_PLAYER:-mpv.exe}" ;; # Windows OS
*ish*) player_function="${ANI_CLI_PLAYER:-iSH}" ;; # iOS (iSH)
*) player_function="${ANI_CLI_PLAYER:-$(where_mpv)}" ;; # Linux OS
esac
player_extra_flags="${ANI_CLI_PLAYER_FLAGS:-}"
no_detach="${ANI_CLI_NO_DETACH:-0}"
exit_after_play="${ANI_CLI_EXIT_AFTER_PLAY:-0}"
skip_intro="${ANI_CLI_SKIP_INTRO:-0}"
menu_program="${ANI_CLI_MENU:-fzf}"
menu_extra_flags="${ANI_CLI_MENU_FLAGS:-}"
[ -t 0 ] || { command -v rofi >/dev/null 2>&1 && menu_program=rofi; }
[ -t 0 ] || { command -v dmenu >/dev/null 2>&1 && menu_program=dmenu; }
hist_dir="${ANI_CLI_HIST_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/ani-cli}"
[ ! -d "$hist_dir" ] && mkdir -p "$hist_dir"
histfile="$hist_dir/ani-hsts"
[ ! -f "$histfile" ] && : >"$histfile"
search="search"
branch="${ANI_CLI_BRANCH:-master}"
resume_enabled=0
batch_mode=0
while [ $# -gt 0 ]; do
case "$1" in
-v | --vlc)
case "$(uname -a | cut -d " " -f 1,3-)" in
*ndroid*) player_function="android_vlc" ;;
MINGW* | *WSL2*) player_function="vlc.exe" ;;
*ish*) player_function="iSH" ;;
*) player_function="vlc" ;;
esac
;;
-s | --syncplay)
case "$(uname -s)" in
Darwin*) player_function="/Applications/Syncplay.app/Contents/MacOS/syncplay" ;;
MINGW* | *Msys)
export PATH="$PATH":"/c/Program Files (x86)/Syncplay/"
player_function="syncplay.exe"
;;
*) player_function="syncplay" ;;
esac
;;
-q | --quality)
[ $# -lt 2 ] && die "missing argument!"
quality="$2"
shift
;;
-S | --select-nth)
[ $# -lt 2 ] && die "missing argument!"
index="$2"
shift
;;
-c | --continue) search=history ;;
--resume) resume_enabled=1 ;;
--batch)
[ "$player_function" = "iSH" ] && iSH_DownFix="--async-dns=false"
player_function=download
batch_mode=1
;;
-d | --download)
[ "$player_function" = "iSH" ] && iSH_DownFix="--async-dns=false"
player_function=download
;;
-D | --delete)
: >"$histfile"
exit 0
;;
-l | --logview)
case "$(uname -s)" in
Darwin*) log show --predicate 'process == "logger"' ;;
Linux*) journalctl -t ani-cli ;;
*) die "Logger not implemented for your platform" ;;
esac
exit 0
;;
-V | --version) version_info ;;
-h | --help) help_info ;;
-e | --episode | -r | --range)
[ $# -lt 2 ] && die "missing argument!"
ep_no="$2"
shift
;;
--dub) mode="dub" ;;
--no-detach) no_detach=1 ;;
--exit-after-play) exit_after_play=1 && no_detach=1 ;;
--rofi) menu_program=rofi ;;
--dmenu) menu_program=dmenu ;;
--skip) skip_intro=1 ;;
-N | --nextep-countdown) search=nextep ;;
-U | --update)
dep_ch "patch"
branch="${2:-$branch}"
update_script
;;
*) query="$(printf "%s" "$query $1" | sed "s|^ ||;s| |+|g")" ;;
esac
shift
done
[ "$menu_program" = "fzf" ] && menu_multi_flag="-m"
[ "$menu_program" = "rofi" ] && menu_multi_flag="-multi-select"
info "Using provider: anidb"
info "Checking dependencies..."
dep_ch "curl"
dep_ch "sed"
dep_ch "grep"
[ "$skip_intro" = 1 ] && dep_ch "ani-skip"
dep_ch "$menu_program"
case "$player_function" in
debug) ;;
download) dep_ch_failover "yt-dlp,ffmpeg" >/dev/null || die 'Neither yt-dlp nor ffmpeg found' ;;
android*) printf "\33[2K\rChecking of players on Android is disabled.\n" ;;
*iSH*) printf "\33[2K\rChecking of players on iOS is disabled\n" ;;
mpv|flatpak_mpv) dep_ch "socat" ;;
*) dep_ch "$player_function" ;;
esac
trap 'cleanup; exit 1' INT HUP TERM
# searching
case "$search" in
history)
backup_old_hist
anime_list=$(while read -r ep_no id title _pos; do process_hist_entry & done <"$histfile")
wait
[ -z "$anime_list" ] && die "No unwatched series in history!"
[ -z "${index##*[!0-9]*}" ] && id=$(printf "%s" "$anime_list" | nl -w 2 | sed 's/^[[:space:]]//' | nth "Select anime: " | cut -f1)
[ -z "${index##*[!0-9]*}" ] || id=$(printf "%s" "$anime_list" | sed -n "${index}p" | cut -f1)
[ -z "$id" ] && exit 1
title=$(printf "%s" "$anime_list" | grep "$id" | cut -f2 | sed 's/ - episode.*//')
ep_list=$(episodes_list_anidb "$id")
ep_no=$(printf "%s" "$anime_list" | grep "$id" | cut -f2 | sed -nE 's/.*- episode (.+)$/\1/p')
anime_title="$(printf "%s" "$title" | cut -d'(' -f1 | tr -d '[:punct:]')"
anidb_desc "$id" || true
printf "%s" "$anime_list" | grep "$id" | cut -f2 | grep -q "(up to date)" && time_until_next_ep "$anime_title"
;;
*)
if [ "$menu_program" = "fzf" ]; then
while [ -z "$query" ]; do
printf "\33[2K\r\033[1;36mSearch anime: \033[0m" && read -r query
done
else
[ -z "$query" ] && query=$(: | menu "Search anime: " "" "$menu_extra_flags")
[ -z "$query" ] && exit 1
fi
# for checking new releases by specifying anime name
[ "$search" = "nextep" ] && time_until_next_ep "$query"
query=$(printf "%s" "$query" | sed "s| |+|g")
anime_list=$(search_anidb "$query")
[ -z "$anime_list" ] && die "No results found!"
# check if select-nth index is a valid number, if so skip interactive selection
[ "$index" -eq "$index" ] 2>/dev/null && result=$(printf "%s" "$anime_list" | sed -n "${index}p")
[ -z "$index" ] && result=$(printf "%s" "$anime_list" | nl -w 2 | sed 's/^[[:space:]]//' | nth "Select anime: ")
[ -z "$result" ] && die "Invalid/No Anime Selection"
title="$(printf "%s" "$result" | cut -f2)"
anime_title="$(printf "%s" "$title" | cut -d'(' -f1 | tr -d '[:punct:]')"
id="$(printf "%s" "$result" | cut -f1)"
anidb_desc "$(printf "%s" "$result" | cut -f1)" || true
ep_list=$(episodes_list_anidb "$id")
[ -n "$ep_no" ] && [ "$ep_no" -ne -1 ] && printf "%s" "$ep_list" | grep -v -q "^$ep_no$" && die "Invalid Episode Selection"
[ -z "$ep_no" ] && ep_no=$(printf "%s" "$ep_list" | nth "Select episode: " "$menu_multi_flag")
[ -z "$ep_no" ] && die "Invalid/No Episode Selection"
;;
esac
# moves the cursor up one line and clears that line
tput cuu1 && tput el
# stores the position of cursor
tput sc
# playback & loop
play
[ "$player_function" = "download" ] && exit 0
[ "$player_function" = "debug" ] && exit 0
while cmd=$(printf 'next\nreplay\nprevious\nselect%s\nchange_quality\nquit' "$seasons_option" | nth "Playing episode $ep_no of $title... "); do
# save playback position before acting on the menu choice
if [ "$menu_program" = "fzf" ]; then
case "$player_function" in
mpv*|flatpak_mpv) update_history "$(get_mpv_position)" ;;
esac
fi
case "$cmd" in
next) ep_no=$(printf "%s" "$ep_list" | sed -n "/^${ep_no}$/{n;p;}") 2>/dev/null ;;
replay) episode="$replay" ;;
previous) ep_no=$(printf "%s" "$ep_list" | sed -n "/^${ep_no}$/{g;1!p;};h") 2>/dev/null ;;
select) ep_no=$(printf "%s" "$ep_list" | nth "Select episode: " "$menu_multi_flag") ;;
seasons)
new_anime="$(printf "%s" "$seasons" | nl -w 2 | sed 's/^[[:space:]]//' | nth "Select anime: ")"
[ -z "$new_anime" ] && die "Invalid/No Anime Selection"
title="$(printf "%s" "$new_anime" | cut -f2)"
anime_title="$(printf "%s" "$title" | cut -d'(' -f1 | tr -d '[:punct:]')"
id="$(printf "%s" "$new_anime" | cut -f1)"
ep_list=$(episodes_list_anidb "$id")
ep_no=$(printf "%s" "$ep_list" | nth "Select episode: " "$menu_multi_flag")
[ -z "$ep_no" ] && die "Invalid/No Episode Selection"
;;
change_quality)
new_quality="$(printf "%s" "$links" | menu | cut -d">" -f1)"
[ -z "$new_quality" ] && die "No quality selected"
select_quality "$new_quality"
;;
*)
cleanup
exit 0
;;
esac
[ -z "$ep_no" ] && die "Out of range"
play
done
# ani-cli
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Project repository: https://github.com/VVAT3R/ani-cli