aniwrapper/aniwrapper
Kyle Yasuda bd949bb7c7
update to v3 functions (#8)
* update to v3 functions

* fix progress prompt appearing in rofi menus

* modularize UI in ani-cli for rofi vs cli
2022-06-26 21:38:28 -07:00

428 lines
11 KiB
Bash
Executable File

#!/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. 1080p|3. 720p|4. 480p|5. 360p|6. 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
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 [-adhpqSv] [-t <theme> | -T <config_path>] [<query>]
aniwrapper -f <directory_path> [-t <theme> | -T <config_path>] [-pSv] [<query>]\
aniwrapper -c [-dhpqSv] [<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
-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
-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_PLAY_FROM_FILE && !IS_SYNC && GET_QUALITY)); then
get_quality
fi
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|1080p|720p|480p|360p|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:f:hpqQ: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"
;;
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
;;
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
}
# 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 ((SILENT)); then
args+=(-S)
fi
if ((IS_VERBOSE)); then
args+=(-v)
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