This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
forked from Dink4n/ani-cli
-
Notifications
You must be signed in to change notification settings - Fork 4
/
aniwrapper
executable file
·480 lines (455 loc) · 12.8 KB
/
aniwrapper
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
#!/usr/bin/env bash
set -Eeo pipefail
CMD="/usr/bin/ani-cli"
CFG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/aniwrapper"
CFG_FILE="$CFG_DIR/themes/aniwrapper.rasi"
DEFAULT_DOWNLOAD="$HOME/Videos/sauce"
ROFI_THEME="aniwrapper.rasi"
THEMES="alter|aniwrapper|dracula|doomone|fancy|material|monokai|nord|nord2|onedark"
QUALITIES="1. best|2. worst"
SUPPORTED_PLAYERS="mpv|vlc"
QUALITY=best
PLAYER_FN=mpv
DPI=96
IS_AUTOPLAY=0
GET_QUALITY=0
IS_CUSTOM_THEME=0
IS_DOWNLOAD=0
IS_PLAY_FROM_FILE=0
IS_ROFI=1
IS_SYNC=0
IS_ALTERNATE_PLAYER=0
IS_VERBOSE=0
IS_SELECT_EPISODE=1
SELECT_PROVIDER=0
SILENT=0
quit="8. Quit"
options="1. Stream|2. Download|3. Continue|4. Recently Updated|5. Play from File|6. Sync History|7. Choose Theme|$quit"
theme=default
help_text() {
while IFS= read -r line; do
printf "%s\n" "$line"
done <<< "
Usage:
aniwrapper [-adehpqSv] [-t <theme> | -T <config_path>] [<query>]
aniwrapper -f <directory_path> [-t <theme> | -T <config_path>] [-pSv] [<query>]\
aniwrapper -c [-dehpqSv] [<query>]
aniwrapper -Q <query>
aniwrapper -C
Options:
-a enable autoplay
-c enable command-line mode (rofi disabled)
-C connect to history database
-d download episode in command-line mode
-e [<episode> | <begin> - <end>] select episode(s) to stream/download
-f <path_to_directory> (no trailing slash) specify starting directory for play for file mode
-h show this help text
-p enable player selection menu
-P select provider to scrape first
-q enable quality selection
-Q <query> query the history database
-r start script in episode selection using the most recently watched anime
-v verbose output
-S silent mode (suppress output to stdout) [cannot be used with -v]
-t <alter|aniwrapper (default)|dracula|doomone|fancy|material|monokai|nord|nord2|onedark> change rofi theme
-T <config_path> specify custom rofi theme
"
}
lg() {
if ((IS_VERBOSE)); then
inf "$*"
fi
}
# 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"
}
# 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"
}
die() {
printf "%s\n" "$*"
exit 1
}
quit() {
((SILENT != 1)) && lg 'Quitting...'
exit 0
}
run() {
if ((IS_CUSTOM_THEME)); then
args+=(-T"$CFG_FILE")
elif ((!IS_CUSTOM_THEME)); then
args+=(-t"$theme")
fi
lg "Running $CMD ${args[*]} -D$DPI $*"
$CMD "${args[@]}" -D"$DPI" "$@"
}
get_quality() {
if ((IS_ROFI)); then
selection=$(rofi -dpi "$DPI" -dmenu -config "$CFG_FILE" \
-l 6 -selected-row 0 -a 0 -window-title 'aniwrapper' \
-theme-str 'listview {columns: 1;} window {width: 25%;}' \
-p "Choose video quality:" \
-sep '|' -no-custom <<< "$QUALITIES")
QUALITY=$(awk '{print $2}' <<< "$selection")
else
qualities="best|worst"
prompt "Choose quality " "[$qualities]"
read -r QUALITY
while [[ ! "$QUALITY" =~ ($qualities) ]]; do
lg "$QUALITY not a valid quality -> [$qualities]"
prompt "Choose quality " "[$qualities]"
read -r QUALITY
done
fi
lg "selected quality: $QUALITY"
}
# return the index in $THEMES of $ROFI_THEME
get_theme_idx() {
themeslist="aniwrapper-alter.rasi|aniwrapper.rasi|aniwrapper-dracula.rasi|aniwrapper-doomone.rasi|aniwrapper-fancy.rasi|aniwrapper-material.rasi|aniwrapper-monokai.rasi|aniwrapper-nord.rasi|aniwrapper-nord2.rasi|aniwrapper-onedark.rasi"
idx=0
while IFS='|' read -ra themes; do
for t in "${themes[@]}"; do
if [[ "$t" == "$ROFI_THEME" ]]; then
lg "theme -> $t | theme index -> $idx" > /dev/stderr
printf "%s\n" "$idx"
break
fi
((++idx))
done
done <<< "$themeslist"
}
# generates a span mesg for rofi given
# input: message: str
generate_span() {
msg="$*"
span="<span foreground='#ecbe7b' style='italic' size='small'>$msg</span>"
printf "%s\n" "$span"
}
set_theme() {
new_theme="$1"
case "$new_theme" in
aniwrapper)
ROFI_THEME=aniwrapper.rasi
;;
*)
ROFI_THEME="aniwrapper-$new_theme.rasi"
;;
esac
lg "Chosen theme -> $ROFI_THEME"
CFG_FILE="$CFG_DIR/themes/$ROFI_THEME"
}
parse_args() {
while getopts 'acCdD:e:f:hpPqQ:rSt:T:v' OPT; do
case "$OPT" in
a)
IS_AUTOPLAY=1
;;
c)
IS_ROFI=0
lg "Command-line (ani-cli) mode set"
;;
C)
lg "Connecting to history database -> $CFG_DIR/history.sqlite3"
sqlite3 "$CFG_DIR/history.sqlite3"
exit $?
;;
d)
IS_DOWNLOAD=1
lg "Download flag set..."
;;
D)
DPI="$OPTARG"
;;
e)
episodes="$OPTARG"
[[ "$episodes" =~ ^[0-9]+(-[0-9]+)? ]] || die "Invalid episode range: $episodes"
IS_SELECT_EPISODE=0
;;
f)
IS_PLAY_FROM_FILE=1
play_path="$OPTARG"
lg "Play from file flag set... skipping main menu"
lg "PLAY_PATH: $play_path"
;;
h)
help_text
exit 0
;;
p)
IS_ALTERNATE_PLAYER=1
;;
P)
SELECT_PROVIDER=1
;;
q)
GET_QUALITY=1
lg "Quality prompt enabled"
;;
Q)
query="$OPTARG"
lg "DATABASE QUERY: $query"
sqlite3 -line "$CFG_DIR/history.sqlite3" "$query"
exit $?
;;
r)
IS_RESUME=1
lg "Selecting previously watched anime"
;;
S)
SILENT=1
;;
t)
theme="$OPTARG"
set_theme "$theme"
;;
T)
CFG_FILE="$OPTARG"
[ ! -f "$CFG_FILE" ] && die "config file $CFG_FILE does not exist"
IS_CUSTOM_THEME=1
;;
v)
IS_VERBOSE=1
;;
*)
help_text
exit 1
;;
esac
done
}
get_player() {
msg1="Choose from the supported players, or supply your own player command (full functionality not guaranteed)"
msg2="if using an unsupported player, it must be able to play URLs for streaming from the internet"
if ((IS_ROFI)); then
PLAYER_FN=$(
awk '{print $NF}' < <(rofi -dmenu -config "$CFG_FILE" -DPI "$DPI" \
-l 4 -theme-str 'listview {columns: 1;} window {width: 40%;}' \
-p "Enter video player:" -window-title 'aniwrapper' \
-mesg "$(printf "%s\n%s\n" "$(generate_span "$msg1")" "$(generate_span "$msg2")")" \
-a 0 -sep '|' <<< "$SUPPORTED_PLAYERS")
)
else
printf "%s\n%s\n" "$msg1" "$msg2"
printf "%s\n" "SUPPORTED PLAYERS:"
while IFS='|' read -ra players; do
printf "%s\n" "${players[@]}"
done <<< "$SUPPORTED_PLAYERS"
prompt "Enter player"
read -r PLAYER_FN
fi
[ -z "$PLAYER_FN" ] && PLAYER_FN=mpv
if ! command -v "$PLAYER_FN" > /dev/null; then
die "ERROR: $PLAYER_FN does not exist"
fi
}
# prompts the user to select a provider and returns the provider name and index
get_provider() {
providers="1. Animixplay|2. Xstreamcdn|3. Gogoanime"
if ((IS_ROFI)); then
selection="$(rofi -dmenu -config "$CFG_FILE" -DPI "$DPI" \
-i -l 7 -theme-str 'listview {columns: 1;} window {width: 45%;}' \
-p "Select provider:" -window-title 'aniwrapper' \
-a 0 -sep '|' <<< "$providers")"
[ -z "$selection" ] && selection=1
provider_idx="${selection:0:1}"
else
provider_begin=1
provider_end=3
printf "%s%s\n" "Select provider:" "[$providers]"
prompt "Enter provider [$provider_begin-$provider_end]"
read -r provider_idx
[ -z "$provider_idx" ] && die "ERROR: provider index cannot be empty"
((provider_idx < provider_begin || provider_idx > provider_end)) && die "ERROR: provider index must be between $provider_begin and $provider_end"
fi
while IFS='|' read -ra providers_list; do
for p in "${providers_list[@]}"; do
if [[ "${p:0:1}" == "$provider_idx" ]]; then
lg "provider -> $p | provider index -> $idx" > /dev/stderr
provider="${p:3}"
break
fi
((++idx))
done
done <<< "$providers"
[ -z "$provider" ] && provider="Xstreamcdn"
inf "Selected provider: $provider"
}
# Check passed in flags and set cli arguments
check_flags() {
if ((IS_VERBOSE && SILENT)); then
die "verbose and silent options cannot be used together"
fi
if ((IS_DOWNLOAD && IS_AUTOPLAY)); then
die "autoplay and download options cannot be used together"
fi
if ((IS_DOWNLOAD && IS_PLAY_FROM_FILE)); then
die "download and play from file options cannot be used together"
fi
if ((!IS_DOWNLOAD && IS_ALTERNATE_PLAYER)); then
get_player
lg "SELECTED PLAYER FN -> $PLAYER_FN"
fi
args=()
if ((IS_AUTOPLAY)); then
args+=(-a)
fi
if ((!IS_ROFI)); then
args+=(-c)
prompt "Enter download directory"
read -r dl_dir
lg "Download dir: $dl_dir"
if [ ! -d "$dl_dir" ]; then
mkdir -p "$dl_dir" || die "Error creating directory: $dl_dir"
fi
args+=(-d"$dl_dir")
fi
if ((IS_DOWNLOAD)); then
args+=(-d "$DEFAULT_DOWNLOAD")
fi
if ((IS_PLAY_FROM_FILE)); then
args+=(-f"$play_path")
fi
if ((IS_ALTERNATE_PLAYER)); then
args+=(-p "$PLAYER_FN")
fi
if ((IS_RESUME)); then
args+=(-r)
fi
if ((SELECT_PROVIDER)); then
get_provider || provider_idx=1
lg "PROVIDER INDEX -> $provider_idx"
args+=(-P"$provider_idx")
fi
if ((SILENT)); then
args+=(-S)
fi
if ((IS_VERBOSE)); then
args+=(-v)
fi
if ((GET_QUALITY)); then
args+=(-q)
fi
if ((!IS_SELECT_EPISODE)); then
args+=(-e"$episodes")
fi
lg "ARGS: ${args[*]}"
if ((IS_AUTOPLAY || IS_DOWNLOAD || !IS_ROFI || IS_RESUME || IS_PLAY_FROM_FILE)); then
run "${args[@]}" "$@"
exit $?
fi
}
get_dl_dir() {
dl_dir=$(
rofi -dpi "$DPI" -dmenu -config "$CFG_FILE" \
-theme-str 'listview {columns: 1;} window {width: 45%;}' \
-mesg "$(generate_span "Enter the path to the download directory, or leave blank to go with the default: $HOME/Videos/sauce/")" \
-l 1 -p "Enter download dir:" -window-title 'aniwrapper'
)
# if dl_dir is none set to current directory
[ "$dl_dir" == "" ] && dl_dir="$DEFAULT_DOWNLOAD"
if [ ! -d "$dl_dir" ]; then
mkdir -p "$dl_dir" || die "Error creating directory: $dl_dir"
fi
}
main() {
((!SILENT)) && lg "CONFIG DIR: $CFG_DIR"
((!SILENT)) && lg "ROFI CFG: $CFG_FILE"
choice=$(echo "${options[@]}" | rofi -dpi "$DPI" -dmenu -no-custom -sep '|' \
-theme-str 'listview {columns: 2;} window {width: 45%;}' \
-config "$CFG_FILE" -l 4 -i -p "Aniwrapper" -window-title 'aniwrapper')
[ "$choice" == "$quit" ] && quit
selection=$(printf "%s\n" "$choice" | awk '{ print $1 }')
case "$selection" in
1.)
lg "Streaming mode"
run
;;
2.)
lg "Download anime"
get_dl_dir && run -d "$dl_dir"
;;
3.)
lg "Continue watching"
run -H
;;
4.)
lg "Showing recently updated anime"
run -R
;;
5.)
lg "Play from file selected"
IS_PLAY_FROM_FILE=1
span=$(printf '%s\n%s\n' "$(generate_span "Provide a path to a valid directory, or choose from the list below")" "$(generate_span "The program will begin searching for media files from the supplied directory")")
play_dir=$(
rofi -dpi "$DPI" -dmenu -config "$CFG_FILE" \
-i -l 12 -mesg "$span" -p "Enter path to starting directory:" \
-async-pre-read 24 -matching 'fuzzy' -window-title 'aniwrapper' \
-sort -sorting-method fzf \
< <(sqlite3 -noheader -list "$CFG_DIR/history.sqlite3" <<< "SELECT directory FROM file_history WHERE filename = 'DIRECTORY' ORDER BY WATCH_DATE DESC;")
)
# trim trailing whitespace
play_dir="${play_dir%% }"
lg "Play dir: $play_dir"
if [ -z "$play_dir" ]; then
mkdir -p "$DEFAULT_DOWNLOAD" || die "error creating default download directory"
run -f"$DEFAULT_DOWNLOAD"
else
run -f"$play_dir"
fi
exit $?
;;
6.)
lg "Sync history database"
IS_SYNC=1
roficmd="rofi -dpi $DPI -dmenu -config $CFG_FILE -l 0 -p"
username=$($roficmd "Enter the username of the remote user:" -theme-str 'window {width: 45%;}')
[ -z "$username" ] && die "No username provided... exiting"
host=$($roficmd "Enter the host for the remote machine (eg 192.168.1.99):" -theme-str 'window {width: 45%;}')
[ -z "$host" ] && die "No host provided... exiting"
port=$($roficmd "Enter in the ssh port for remote machine or leave blank for default [22]:" -theme-str 'window {width: 45%;}')
[ -z "$port" ] && port=22
keypath=$($roficmd "Enter path to private key (leave blank if not needed or if unsure):" -theme-str 'window {width: 45%;}')
if ! printf "%s\n%s\n%d\n%s\n" "$username" "$host" "$port" "$keypath" | run -s; then
lg "Aniwrapper was unable to sync the databases..."
exit 1
else
lg "Databases synced successfully"
quit
fi
;;
7.)
[ -z "$THEMES" ] && die "No themes provided... exiting"
theme_idx="$(get_theme_idx)"
lg "Theme index: $theme_idx"
lg "NUM THEMES: $NUM_THEMES"
choice=$(rofi -dmenu -config "$CFG_FILE" -dpi "$DPI" -window-title 'aniwrapper' \
-theme-str 'listview {columns: 2;} window {width: 45%;}' \
-no-custom -l 5 -i -p "Choose theme: " -sep '|' \
-a "$theme_idx" -selected-row "$theme_idx" <<< "$THEMES")
theme=$(awk '{ print $1 }' <<< "$choice")
set_theme "$theme"
main
;;
8.)
quit
;;
*)
help_text
;;
esac
}
parse_args "$@"
shift $((OPTIND - 1))
if [ $# -gt 0 ]; then
check_flags "$*"
if ((!IS_DOWNLOAD)); then
run "$*"
elif ((IS_DOWNLOAD)); then
get_dl_dir && run -d "$dl_dir" "$*"
fi
else
check_flags
main
fi