aniwrapper/aniwrapper

285 lines
7.2 KiB
Plaintext
Raw Normal View History

2021-10-30 16:30:40 -07:00
#!/usr/bin/env bash
set -Eeo pipefail
#############
# Globals #
#############
CMD="/usr/bin/ani-cli"
DEFAULT_DOWNLOAD="$HOME/Videos/sauce"
2022-01-02 23:18:27 -08:00
CFG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/aniwrapper"
ROFI_THEME="aniwrapper.rasi"
CFG_FILE="$CFG_DIR/themes/$ROFI_THEME"
2022-01-05 10:54:19 -08:00
QUALITIES="1. high|2. normal (default)|3. low"
QUALITY=normal
THEMES="aniwrapper (default)|dracula|fancy|flamingo|material|onedark"
GET_QUALITY=0
VERBOSE=0
IS_ROFI=1
IS_DOWNLOAD=0
IS_PLAY_FROM_FILE=0
IS_CUSTOM_THEME=0
quit="6. Quit"
options="1. Stream|2. Download|3. Continue|4. Play from File|5. Sync History|$quit"
theme=default
2021-10-30 16:30:40 -07:00
#############
# Functions #
#############
2022-01-07 09:24:13 -08:00
logger() {
2022-01-04 00:57:22 -08:00
if [[ "$VERBOSE" -eq 1 ]]; then
printf "%s\n" "$*"
2022-01-02 09:53:16 -08:00
fi
}
2021-10-30 16:30:40 -07:00
seppuku() {
printf "%s\n" "$*"
exit 1
}
quit() {
printf "%s\n" 'Quitting...'
2021-10-30 16:30:40 -07:00
exit 0
}
run() {
if [[ "$IS_PLAY_FROM_FILE" -eq 0 ]] && [[ "$GET_QUALITY" -eq 1 ]]; then
get_quality
2022-01-02 09:53:16 -08:00
fi
if [[ "$IS_CUSTOM_THEME" -eq 1 ]] && [[ "$VERBOSE" -eq 0 ]]; then
"$CMD" -q "$QUALITY" -T "$CFG_FILE" "$@"
elif [[ "$IS_CUSTOM_THEME" -eq 1 ]] && [[ "$VERBOSE" -eq 1 ]]; then
"$CMD" -vq "$QUALITY" -T "$CFG_FILE" "$@"
elif [[ "$VERBOSE" -eq 0 ]]; then
"$CMD" -q "$QUALITY" -t "$theme" "$@"
2021-11-11 15:17:55 -08:00
else
"$CMD" -vq "$QUALITY" -t "$theme" "$@"
2021-11-11 15:17:55 -08:00
fi
}
create_default_download() {
# make sure download directory exists
if [ ! -d "$DEFAULT_DOWNLOAD" ]; then
mkdir -p "$DEFAULT_DOWNLOAD"
fi
}
2022-01-04 00:57:22 -08:00
get_quality() {
if [ "$IS_ROFI" -eq 1 ]; then
selection=$(rofi -dmenu -config "$CFG_FILE" \
2022-01-05 10:54:19 -08:00
-l 3 -selected-row 1 \
-theme-str 'listview {columns: 1;}' -p "Choose video quality:" \
2022-01-04 00:57:22 -08:00
-sep '|' <<< "$QUALITIES")
QUALITY=$(awk '{print $2}' <<< "$selection")
else
2022-01-05 10:54:19 -08:00
printf "%s" "Enter quality [$QUALITIES]: "
2022-01-04 00:57:22 -08:00
read -r QUALITY
fi
2022-01-07 09:24:13 -08:00
logger "selected quality: $QUALITY"
}
2022-01-04 00:57:22 -08:00
# generates a span mesg for rofi given
# input: message: str
generate_span() {
msg="$*"
span="<span foreground='peachpuff' style='italic' size='small' weight='light'>$msg</span>"
printf "%s\n" "$span"
}
########
# Main #
########
while getopts 'vhqcdf:-:t:T:' OPT; do
case "$OPT" in
h)
help_text
;;
v)
VERBOSE=1
;;
2022-01-02 09:53:16 -08:00
q)
GET_QUALITY=1
2022-01-07 09:24:13 -08:00
logger "Quality prompt enabled"
2022-01-02 09:53:16 -08:00
;;
c)
IS_ROFI=0
2022-01-07 09:24:13 -08:00
logger "Command-line (ani-cli) mode set"
2022-01-02 09:53:16 -08:00
;;
d)
IS_DOWNLOAD=1
2022-01-07 09:24:13 -08:00
logger "Download flag set..."
2022-01-02 09:53:16 -08:00
;;
f)
IS_PLAY_FROM_FILE=1
play_path="$OPTARG"
2022-01-07 09:24:13 -08:00
logger "Play from file flag set... skipping main menu"
logger "PLAY_PATH: $play_path"
;;
t)
theme="$OPTARG"
2022-01-07 09:24:13 -08:00
logger "custom theme provided: $theme"
case "$theme" in
aniwrapper)
2022-01-07 09:24:13 -08:00
logger "Default theme chosen... doing nothing"
theme=default
;;
dracula)
ROFI_THEME=aniwrapper-dracula.rasi
;;
fancy)
ROFI_THEME=aniwrapper-fancy.rasi
;;
flamingo)
ROFI_THEME=aniwrapper-flamingo.rasi
;;
material)
ROFI_THEME=aniwrapper-material.rasi
;;
onedark)
ROFI_THEME=aniwrapper-onedark.rasi
;;
*)
seppuku "$theme not a valid theme file. Themes: [$THEMES]"
;;
esac
CFG_FILE="$CFG_DIR/themes/$ROFI_THEME"
;;
T)
CFG_FILE="$OPTARG"
[ ! -f "$CFG_FILE" ] && seppuku "config file $CFG_FILE does not exist"
IS_CUSTOM_THEME=1
;;
*)
2022-01-07 09:24:13 -08:00
logger "Invalid option"
exit 1
;;
esac
done
2022-01-07 09:24:13 -08:00
logger "CONFIG DIR: $CFG_DIR"
logger "ROFI CFG: $CFG_FILE"
# Check if command-line flag is set
if [[ "$IS_ROFI" -eq 0 ]] && [[ "$IS_DOWNLOAD" -eq 0 ]]; then
2022-01-02 09:53:16 -08:00
run -c
exit $?
elif [[ "$IS_ROFI" -eq 0 ]] && [[ "$IS_DOWNLOAD" -eq 1 ]]; then
2022-01-02 09:53:16 -08:00
printf "%s" "Enter download dir: "
read -r dl_dir
2022-01-07 09:24:13 -08:00
logger "Download dir: $dl_dir"
[ -d "$dl_dir" ] && mkdir -p "$dl_dir" || dir "Error creating directory: $dl_dir"
2022-01-02 09:53:16 -08:00
run "-cd $dl_dir"
exit $?
elif [[ "$IS_ROFI" -eq 1 ]] && [[ "$IS_PLAY_FROM_FILE" -eq 1 ]] && [[ "$IS_CUSTOM_THEME" -eq 1 ]]; then
run "-f$play_path" -T "$CFG_FILE"
exit $?
elif [[ "$IS_ROFI" -eq 1 ]] && [[ "$IS_PLAY_FROM_FILE" -eq 1 ]] && [[ "$IS_CUSTOM_THEME" -eq 0 ]]; then
run "-f$play_path"
exit $?
fi
2022-01-04 00:57:22 -08:00
# -------------------------------------------------------------------------------
# Main
# -------------------------------------------------------------------------------
choice=$(echo "${options[@]}" | rofi -dmenu -only-match -sep '|' \
-config "$CFG_FILE" -l 6 -i -p "Aniwrapper")
[ "$choice" == "$quit" ] && quit
selection=$(printf "%s\n" "$choice" | awk '{ print $1 }')
case "$selection" in
1.)
# ---------------------------------------------------------------------------
# streaming
# ---------------------------------------------------------------------------
2022-01-07 09:24:13 -08:00
logger "Streaming mode"
run
;;
2.)
# ---------------------------------------------------------------------------
# download
# ---------------------------------------------------------------------------
2022-01-07 09:24:13 -08:00
logger "Download anime"
dl_dir=$(rofi -dmenu -config "$CFG_FILE" \
-l 1 -p "Enter download dir:")
# if dl_dir is none set to current directory
[ "$dl_dir" == "" ] && dl_dir="$DEFAULT_DOWNLOAD"
create_default_download
run -d "$dl_dir"
;;
3.)
# ---------------------------------------------------------------------------
# continue
# ---------------------------------------------------------------------------
2022-01-07 09:24:13 -08:00
logger "Continue watching"
run -H
;;
4.)
# ---------------------------------------------------------------------------
# play
# ---------------------------------------------------------------------------
2022-01-07 09:24:13 -08:00
logger "Play from file selected"
IS_PLAY_FROM_FILE=1
msg="Provide a valid path to a directory or leave blank to go with the default: $HOME/Videos/sauce/"
msg="$(printf '%s\n%s\n' "$msg" "The program will begin searching for media files from the supplied directory")"
span=$(generate_span "$msg")
play_dir=$(rofi -dmenu -config "$CFG_FILE" \
-l 1 -mesg "$span" -p "Enter path to starting directory:")
if [ -z "$play_dir" ]; then
create_default_download
run -f"$DEFAULT_DOWNLOAD"
else
run -f"$play_dir"
fi
;;
5.)
2022-01-07 09:24:13 -08:00
logger "Sync history database"
username=$(rofi -dmenu -config "$CFG_FILE" \
-l 1 -p "Enter the username of the remote user:")
if [[ -z "$username" ]] || [[ "$username" == "" ]]; then
2022-01-07 09:24:13 -08:00
logger "No username provided... exiting"
exit 1
fi
host=$(rofi -dmenu -config "$CFG_FILE" \
-l 1 -p "Enter the host for the remote machine (eg 192.168.1.99):")
if [[ -z "$host" ]] || [[ "$host" == "" ]]; then
2022-01-07 09:24:13 -08:00
logger "No host provided... exiting"
exit 1
fi
port=$(rofi -dmenu -config "$CFG_FILE" \
-l 1 -p "Enter in the ssh port for remote machine or leave blank for default [22]:")
if [[ -z "$port" ]] || [[ "$port" == "" ]]; then
port=22
fi
keypath=$(rofi -dmenu -config "$CFG_FILE" \
-l 1 -p "Enter path to private key (leave blank if not needed or if unsure):")
2021-11-20 13:07:47 -08:00
if [[ -z "$keypath" ]]; then
printf "%s\n%s\n%d\n%s\n" "$username" "$host" "$port" "" | run -s
else
printf "%s\n%s\n%d\n%s\n" "$username" "$host" "$port" "$keypath" | run -s
fi
2021-11-20 13:07:47 -08:00
if [[ "$?" -ne 0 ]]; then
2022-01-07 09:24:13 -08:00
logger "Aniwrapper was unable to sync the databases..."
exit 1
else
2022-01-07 09:24:13 -08:00
logger "Databases synced successfully"
2022-01-04 00:57:22 -08:00
quit
fi
;;
6.)
# ---------------------------------------------------------------------------
# get out
# ---------------------------------------------------------------------------
2022-01-04 00:57:22 -08:00
quit
;;
*)
2022-01-07 09:24:13 -08:00
logger "Invalid choice..."
exit 1
;;
esac