forked from pystardust/ani-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathani-cli
executable file
·657 lines (598 loc) · 17 KB
/
ani-cli
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
#!/bin/sh
# 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/pystardust/ani-cli
# Version number
VERSION="1.9.2"
#######################
# AUXILIARY FUNCTIONS #
#######################
help_text () {
while IFS= read -r line; do
printf "%s\n" "$line"
done <<-EOF
Usage:
${0##*/} [-v] [-q <quality>] [-a <episode>] [-d | -p <download_dir>] [<query>]
${0##*/} [-v] [-q <quality>] -c
${0##*/} -h | -D | -U | -V
Options:
-c continue watching anime from history
-a specify episode to watch
-h show helptext
-d download episode
-p download episode to specified directory
-q set video quality (best|worst|360|480|720|1080)
-v use VLC as the media player
-D delete history
-U fetch update from github
-V print version number and exit
Episode selection:
Add 'h' on beginning for episodes like '6.5' -> 'h6'
Multiple episodes can be chosen given a range
Choose episode [1-13]: 1 6
This would choose episodes 1 2 3 4 5 6
To select the last episode use -1
When selecting non-interactively, the first result will be
selected, if anime is passed
EOF
}
version_text () {
inf "Version: $VERSION" >&2
}
die () {
err "$*"
exit 1
}
update_script () {
# get the newest version of this script from github and replace it
update="$(curl -s "https://raw.githubusercontent.com/pystardust/ani-cli/master/ani-cli" | diff -u "$0" -)"
if [ -z "$update" ]; then
inf "Script is up to date :)"
else
if printf '%s\n' "$update" | patch "$0" - ; then
inf "Script has been updated"
else
die "Can't update for some reason!"
fi
fi
}
dep_ch () {
# checks if dependencies are present
for dep; do
if ! command -v "$dep" >/dev/null ; then
err "Program \"$dep\" not found. Please install it."
#aria2c is in the package aria2
[ "$dep" = "aria2c" ] && err "To install aria2c, Type <your_package_manager> aria2"
die
fi
done
}
download () {
case $2 in
*mp4*)
aria2c --summary-interval=0 -x 16 -s 16 --referer="$1" "$2" --dir="$download_dir" -o "${3}-${4}.mp4" --download-result=hide;;
*)
ffmpeg -loglevel error -stats -referer "$1" -i "$2" -c copy "$download_dir/${3}-${4}.mp4";;
esac
}
#############
# SEARCHING #
#############
search_anime () {
# get anime name along with its id for search term
search=$(printf '%s' "$1" | tr ' ' '-' )
curl -s "$base_url//search.html" -G -d "keyword=$search" |
sed -n -E 's_^[[:space:]]*<a href="/category/([^"]*)" title="([^"]*)".*_\1_p'
}
search_eps () {
# get available episodes for anime_id
anime_id="$1"
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
}
'
}
search_for_unwatched () {
# compares history with gogoanime, only shows unfinished anime
search_results="$*"
while read -r anime_id; do
current_ep_number=$(search_eps "$anime_id")
history_ep_number=$(sed -n -E "s/${anime_id}\t//p" "$logfile")
[ "$current_ep_number" -ge "$history_ep_number" ] && printf '%s\n' "$anime_id"
done <<-EOF
$search_results
EOF
}
search_another_anime () {
tput clear
prompt "Search Anime"
query="$REPLY $REPLY2"
process_search
}
search_history () {
tput clear
search_results=$(sed -n -E 's/\t[0-9]*//p' "$logfile")
[ -z "$search_results" ] && die "History is empty"
search_results=$(search_for_unwatched "$search_results")
[ -z "$search_results" ] && die "No unwatched episodes"
one_hist=$(printf '%s\n' "$search_results" | grep -e "$" -c)
[ "$one_hist" = 1 ] && select_first=1
anime_selection "$search_results"
ep_choice_start=$(sed -n -E "s/${selection_id}\t//p" "$logfile")
}
##################
# URL PROCESSING #
##################
get_dpage_link() {
# get the download page url
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'
}
decrypt_link() {
secret_key='3235373136353338353232393338333936313634363632323738383333323838'
iv='31323835363732393835323338333933'
ajax_url="https://gogoplay4.com/encrypt-ajax.php"
id=$(printf "%s" "$1" | sed -nE 's/.*id=(.*)&title.*/\1/p')
if [ "${#id}" -lt "8" ]; then
#pad the id with character until it length(id) reaches to 8, no xor needed to be done on it
pad=$(printf "\010\016\003\010\t\003\004\t" | cut -c"$((8-(${#id} % 16)))"-)
new_id=$(printf "%s$pad" "$id")
else
# xor the last character of id with 0xA if length(id) is 8, make it the new last character
lastchar=$(printf %s "$id" | tail -c 1)
lastcharval=$(printf %d "'$lastchar")
xor=$((lastcharval ^ 10 ))
octalval=$(printf "\\$(printf %o "$xor")")
new_id=$(printf "%s" "$id" | sed "s/.$/$octalval/")
fi
#encrypt and create the final ajax
ajax=$(printf "%s\010\016\003\010\t\003\004\t" "$new_id" | openssl enc -aes256 -K "$secret_key" -iv "$iv" -a)
#send request and get the data(most lamest way)
data=$(curl -s -H "X-Requested-With:XMLHttpRequest" "$ajax_url" -d "id=$ajax" | sed -e 's/{"data":"//' -e 's/"}/\n/' -e 's/\\//g')
#decrypt the data to get final links
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@:%_\+.~#?&\/\/=]*'
}
get_video_quality() {
# chooses the link for the set quality
dpage_url="$1"
video_links=$(decrypt_link "$dpage_url")
case $quality in
best)
video_link=$(printf '%s' "$video_links" | head -n 4 | tail -n 1)
;;
worst)
video_link=$(printf '%s' "$video_links" | head -n 1)
;;
*)
video_link=$(printf '%s' "$video_links" | grep -i "${quality}p" | head -n 1)
if [ -z "$video_link" ]; then
err "Current video quality is not available (defaulting to best quality)"
quality=best
video_link=$(printf '%s' "$video_links" | head -n 4 | tail -n 1)
fi
;;
esac
printf '%s' "$video_link"
}
###############
# TEXT OUTPUT #
###############
err () {
# display an error message to stderr (in red)
printf "\033[1;31m%s\033[0m\n" "$*" >&2
}
inf () {
# display an informational message (first argument in green, second in magenta)
printf "\033[1;32m%s \033[1;35m%s\033[0m\n" "$1" "$2"
}
prompt () {
# 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
printf "\033[1;34m%s\033[1;35m%s\033[1;34m: \033[0m" "$1" "$2"
read -r REPLY REPLY2
}
menu_line_even () {
# displays an even (cyan) line of a menu line with $2 as an indicator in [] and $1 as the option
printf "\033[1;34m[\033[1;36m%s\033[1;34m] \033[1;36m%s\033[0m\n" "$2" "$1"
}
menu_line_odd() {
# displays an odd (yellow) line of a menu line with $2 as an indicator in [] and $1 as the option
printf "\033[1;34m[\033[1;33m%s\033[1;34m] \033[1;33m%s\033[0m\n" "$2" "$1"
}
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
else
menu_line_even "$1" "$2"
menu_line_parity=0
fi
}
menu_line_strong() {
# displays a warning (red) line of a menu line with $2 as an indicator in [] and $1 as the option
printf "\033[1;34m[\033[1;31m%s\033[1;34m] \033[1;31m%s\033[0m\n" "$2" "$1"
}
#################
# INPUT PARSING #
#################
is_number () {
[ "$1" -eq "$1" ] 2>/dev/null || die 'Invalid number entered'
}
process_search () {
search_results=$(search_anime "$query")
while [ -z "$search_results" ]; do
err 'No search results found'
prompt 'Search Anime'
query="$REPLY $REPLY2"
search_results=$(search_anime "$query")
done
anime_selection "$search_results"
episode_selection
}
anime_selection () {
count=1
while read -r anime_id; do
menu_line_alternate "$anime_id" "$count"
: $((count+=1))
done <<-EOF
$search_results
EOF
if [ -n "$select_first" ]; then
tput clear
choice=1
elif [ -z "$ep_choice_to_start" ] || { [ -n "$ep_choice_to_start" ] && [ -z "$select_first" ]; }; then
menu_line_strong "exit" "q"
prompt "Enter choice"
choice="$REPLY"
while ! [ "$choice" -eq "$choice" ] 2>/dev/null || [ "$choice" -lt 1 ] || [ "$choice" -ge "$count" ] || [ "$choice" = " " ]; do
[ "$choice" = "q" ] && exit 0
err "Invalid choice entered"
prompt "Enter choice"
choice="$REPLY"
done
fi
# Select respective anime_id
count=1
while read -r anime_id; do
if [ "$count" -eq "$choice" ]; then
selection_id="$anime_id"
break
fi
count=$((count+1))
done <<-EOF
$search_results
EOF
search_ep_result=$(search_eps "$selection_id")
read -r last_ep_number <<-EOF
$search_ep_result
EOF
}
episode_selection () {
# using get_dpage_link to get confirmation from episode 0 if it exists,else first_ep_number becomes "1"
first_ep_number=0
result=$(get_dpage_link "$anime_id" "$first_ep_number")
[ -z "$result" ] && first_ep_number=1
if [ "$last_ep_number" -gt "$first_ep_number" ]; then
inf "Range of episodes can be specified: start_number end_number"
if [ -z "$ep_choice_to_start" ]; then
prompt "Choose episode" "[$first_ep_number-$last_ep_number]"
ep_choice_start="$REPLY"
ep_choice_end="$REPLY2"
while [ "$ep_choice_start" -lt "$first_ep_number" ] 2> /dev/null || [ "$ep_choice_end" -gt "$last_ep_number" ] 2> /dev/null || [ "$ep_choice_start" -gt "$last_ep_number" ] 2> /dev/null || [ -z "$ep_choice_start" ] 2> /dev/null; do
if [ "$ep_choice_end" != -1 ]; then
err "Invalid number chosen"
prompt "Choose episode" "[$first_ep_number-$last_ep_number]"
ep_choice_start="$REPLY"
ep_choice_end="$REPLY2"
fi
done
else
ep_choice_start="$ep_choice_to_start" && unset ep_choice_to_start
fi
whether_half="$(printf '%s' "$ep_choice_start" | cut -c1-1)"
if [ "$whether_half" = "h" ]; then
half_ep=1
ep_choice_start="$(printf '%s' "$ep_choice_start" | cut -c2-)"
fi
else
# In case the anime contains only a single episode
ep_choice_start=1
fi
if [ -z "$ep_choice_end" ]; then
auto_play=0
else
auto_play=1
[ "$ep_choice_end" = "-1" ] && ep_choice_end="$last_ep_number"
fi
}
check_input() {
# checks if input is number, creates $episodes from $ep_choice_start and $ep_choice_end
is_number "$ep_choice_start"
episodes=$ep_choice_start
if [ -n "$ep_choice_end" ]; then
is_number "$ep_choice_end"
# create list of episodes to download/watch
episodes=$(seq "$ep_choice_start" "$ep_choice_end")
fi
}
##################
# VIDEO PLAYBACK #
##################
append_history () {
grep -q -w "${selection_id}" "$logfile" || printf "%s\t%s\n" "$selection_id" $((episode+1)) >> "$logfile"
}
open_selection() {
# opens selected episodes one-by-one
for ep in $episodes; do
open_episode "$selection_id" "$ep"
done
episode=${ep_choice_end:-$ep_choice_start}
}
open_episode () {
anime_id="$1"
episode="$2"
# cool way of clearing screen
tput clear
# checking if episode is in range
while [ "$episode" -gt "$last_ep_number" ] || [ -z "$episode" ]; do
is_number "$ep_choice_start"
if [ "$last_ep_number" -eq 0 ]; then
die "Episodes not released yet!"
else
err "Episode out of range"
fi
prompt "Choose episode" "[$first_ep_number-$last_ep_number]"
episode="$REPLY $REPLY2"
done
#processing half episodes
if [ "$half_ep" -eq 1 ]; then
temp_ep="$episode"
episode="${episode}-5"
fi
inf "Getting data for episode $episode"
# decrypting url
dpage_link=$(get_dpage_link "$anime_id" "$episode")
if [ -z "$dpage_link" ];then
err "Episode doesn't exist!!"
else
video_url=$(get_video_quality "$dpage_link")
fi
if [ "$half_ep" -eq 1 ]; then
episode="$temp_ep"
half_ep=0
fi
# Download or play episodes
if [ "$is_download" -eq 0 ]; then
# write anime and episode number and save to temporary history
sed -E "
s/^${selection_id}\t[0-9]+/${selection_id}\t$((episode+1))/
" "$logfile" > "${logfile}.new"
[ "$PID" -ne 0 ] && kill "$PID" >/dev/null 2>&1
[ -z "$video_url" ] && die "Video URL not found"
case "$player_fn" in
vlc)
if [ "$auto_play" -eq 0 ]; then
nohup "$player_fn" --http-referrer="$dpage_link" "$video_url" > /dev/null 2>&1 &
else
inf "Currently playing $selection_id episode" "$episode/$last_ep_number, Range: $ep_choice_start-$ep_choice_end"
"$player_fn" --play-and-exit --http-referrer="$dpage_link" "$video_url" > /dev/null 2>&1
sleep 2
fi
;;
*)
trackma_title=$(printf '%s' "$anime_id" | tr '-' ' ' )
if [ "$auto_play" -eq 0 ]; then
nohup "$player_fn" --referrer="$dpage_link" "$video_url" --force-media-title="$trackma_title $episode" > /dev/null 2>&1 &
else
inf "Currently playing $selection_id episode" "$episode/$last_ep_number, Range: $ep_choice_start-$ep_choice_end"
"$player_fn" --referrer="$dpage_link" "$video_url" --force-media-title="$trackma_title $episode" > /dev/null 2>&1
sleep 2
fi
;;
esac
PID=$!
mv "${logfile}.new" "$logfile"
else
mkdir -p "$download_dir"
inf "Downloading episode $episode ..."
# add 0 padding to the episode name
episode=$(printf "%03d" "$episode")
{
if download "$dpage_link" "$video_url" "$anime_id" "$episode" ; then
inf "Downloaded episode: $episode"
else
err "Download failed episode: $episode , please retry or check your internet connection"
fi
}
fi
}
############
# START UP #
############
# to clear the colors when exited using SIGINT
trap 'printf "\033[0m";[ -f "$logfile".new ] && rm "$logfile".new;exit 1' INT HUP
# default options
player_fn="mpv" #video player needs to be able to play urls
is_download=0
half_ep=0
PID=0
quality=best
scrape=query
download_dir="."
choice=
auto_play=0
# history file path
logfile="${XDG_CACHE_HOME:-$HOME/.cache}/ani-hsts"
logdir="${XDG_CACHE_HOME:-$HOME/.cache}"
# create history file and history dir if none found
[ -d "$logdir" ] || mkdir "$logdir"
[ -f "$logfile" ] || : > "$logfile"
while getopts 'vq:dp:chDUVa:' OPT; do
case $OPT in
h)
help_text
exit 0
;;
d)
is_download=1
;;
a)
ep_choice_to_start=$OPTARG
;;
D)
: > "$logfile"
exit 0
;;
p)
is_download=1
download_dir=$OPTARG
;;
q)
quality=$OPTARG
;;
c)
scrape=history
;;
v)
player_fn="vlc"
;;
U)
update_script
exit 0
;;
V)
version_text
exit 0
;;
*)
help_text
exit 1
;;
esac
done
shift $((OPTIND - 1))
# check for main dependencies
dep_ch "curl" "sed" "grep" "git" "openssl"
# check for optional dependencies
if [ "$is_download" -eq 0 ]; then
dep_ch "$player_fn"
else
dep_ch "aria2c" "ffmpeg"
fi
# gogoanime likes to change domains but keep the olds as redirects
base_url=$(curl -s -L -o /dev/null -w "%{url_effective}\n" https://gogoanime.cm)
case $scrape in
query)
if [ -z "$*" ]; then
prompt "Search Anime"
query="$REPLY $REPLY2"
else
if [ -n "$ep_choice_to_start" ]; then
REPLY=1
select_first=1
fi
query="$*"
fi
process_search
;;
history)
search_history
[ "$REPLY" = "q" ] && exit 0
first_ep_number=0
result=$(get_dpage_link "$anime_id" "$first_ep_number")
[ -z "$result" ] && first_ep_number=1
;;
*)
die "Unexpected scrape type"
esac
check_input
append_history
open_selection
########
# LOOP #
########
while :; do
if [ -z "$select_first" ]; then
if [ "$auto_play" -eq 0 ]; then
inf "Currently playing $selection_id episode" "$episode/$last_ep_number"
else
auto_play=0
fi
[ "$episode" -ne "$last_ep_number" ] && menu_line_alternate 'next episode' 'n'
[ "$episode" -ne "$first_ep_number" ] && menu_line_alternate 'previous episode' 'p'
[ "$last_ep_number" -ne "$first_ep_number" ] && menu_line_alternate 'select episode' 's'
menu_line_alternate "replay current episode" "r"
menu_line_alternate "search for another anime" "a"
menu_line_alternate "search history" "h"
menu_line_alternate "select quality (current: $quality)" "b"
menu_line_strong "exit" "q"
prompt "Enter choice"
# process user choice
choice="$REPLY"
case $choice in
n)
ep_choice_start=$((episode + 1))
ep_choice_end=
;;
b)
prompt "Select quality. Options (best|worst|360|480|720|1080)"
quality="$REPLY"
;;
p)
ep_choice_start=$((episode - 1))
ep_choice_end=
;;
s)
episode_selection
;;
r)
ep_choice_start=$((episode))
ep_choice_end=
;;
a)
search_another_anime
;;
h)
search_history
;;
q)
break
;;
*)
tput clear
err "Invalid choice"
continue
;;
esac
check_input
append_history
open_selection
else
wait $!
exit
fi
done