aniwrapper/aniwrapper

269 lines
6.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeo pipefail
#############
# Globals #
#############
CMD="/usr/bin/ani-cli"
DEFAULT_DOWNLOAD="$HOME/Videos/sauce"
CFG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/aniwrapper"
ROFI_THEME="aniwrapper.rasi"
CFG_FILE="$CFG_DIR/themes/$ROFI_THEME"
QUALITIES="1. high|2. normal (default)|3. low"
QUALITY=normal
THEMES="aniwrapper (default)|dracula|hidpi"
GET_QUALITY=0
VERBOSE=0
IS_ROFI=1
IS_DOWNLOAD=0
IS_PLAY_FROM_FILE=0
quit="6. Quit"
options="1. Stream|2. Download|3. Continue|4. Play from File|5. Sync History|$quit"
theme=default
#############
# Functions #
#############
log() {
if [[ "$VERBOSE" -eq 1 ]]; then
printf "%s\n" "$*"
fi
}
seppuku() {
printf "%s\n" "$*"
exit 1
}
quit() {
printf "%s\n" 'Quitting...'
exit 0
}
run() {
if [[ "$IS_PLAY_FROM_FILE" -eq 0 ]]; then
if [[ "$GET_QUALITY" -eq 1 ]]; then
get_quality
else
log "QUALITY flag not set... using default -> $QUALITY"
fi
fi
if [[ "$VERBOSE" -eq 0 ]]; then
"$CMD" -q "$QUALITY" -t "$theme" "$*"
else
"$CMD" -q "$QUALITY" -t "$theme" -v "$*"
fi
}
create_default_download() {
# make sure download directory exists
if [ ! -d "$DEFAULT_DOWNLOAD" ]; then
mkdir -p "$DEFAULT_DOWNLOAD"
fi
}
get_quality() {
if [ "$IS_ROFI" -eq 1 ]; then
selection=$(rofi -dmenu -config "$CFG_FILE" \
-l 3 -selected-row 1 \
-theme-str 'listview {columns: 1;}' -p "Choose video quality:" \
-sep '|' <<< "$QUALITIES")
QUALITY=$(awk '{print $2}' <<< "$selection")
else
printf "%s" "Enter quality [$QUALITIES]: "
read -r QUALITY
fi
log "selected quality: $QUALITY"
}
# 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:' OPT; do
case "$OPT" in
h)
help_text
;;
v)
VERBOSE=1
;;
q)
GET_QUALITY=1
log "Quality prompt enabled"
;;
c)
IS_ROFI=0
log "Command-line (ani-cli) mode set"
;;
d)
IS_DOWNLOAD=1
log "Download flag set..."
;;
f)
IS_PLAY_FROM_FILE=1
play_path="$OPTARG"
log "Play from file flag set... skipping main menu"
log "PLAY_PATH: $play_path"
;;
t)
theme="$OPTARG"
log "custom theme provided: $theme"
case "$theme" in
aniwrapper)
log "Default theme chosen... doing nothing"
theme=default
;;
dracula)
log "Chaning theme to dracula..."
ROFI_THEME=aniwrapper-dracula.rasi
;;
hidpi)
ROFI_THEME=aniwrapper-hidpi.rasi
;;
*)
log "$1 not a valid theme file. Themes: [$THEMES]"
theme=default
;;
esac
CFG_FILE="$CFG_DIR/themes/$ROFI_THEME"
;;
*)
log "Invalid option"
exit 1
;;
esac
done
log "CONFIG DIR: $CFG_DIR"
log "ROFI CFG: $CFG_FILE"
# Check if command-line flag is set
if [[ "$IS_ROFI" -eq 0 ]] && [[ "$IS_DOWNLOAD" -eq 0 ]]; then
run -c
exit $?
elif [[ "$IS_ROFI" -eq 0 ]] && [[ "$IS_DOWNLOAD" -eq 1 ]]; then
printf "%s" "Enter download dir: "
read -r dl_dir
log "Download dir: $dl_dir"
[ -d "$dl_dir" ] && mkdir -p "$dl_dir" || dir "Error creating directory: $dl_dir"
run "-cd $dl_dir"
exit $?
elif [[ "$IS_ROFI" -eq 1 ]] && [[ "$IS_PLAY_FROM_FILE" -eq 1 ]]; then
run -f"$play_path"
exit $?
fi
# -------------------------------------------------------------------------------
# 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
# ---------------------------------------------------------------------------
log "Streaming mode"
run
;;
2.)
# ---------------------------------------------------------------------------
# download
# ---------------------------------------------------------------------------
log "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
# ---------------------------------------------------------------------------
log "Continue watching"
run -H
;;
4.)
# ---------------------------------------------------------------------------
# play
# ---------------------------------------------------------------------------
log "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.)
log "Sync history database"
username=$(rofi -dmenu -config "$CFG_FILE" \
-l 1 -p "Enter the username of the remote user:")
if [[ -z "$username" ]] || [[ "$username" == "" ]]; then
log "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
log "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):")
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
if [[ "$?" -ne 0 ]]; then
log "Aniwrapper was unable to sync the databases..."
exit 1
else
log "Databases synced successfully"
quit
fi
;;
6.)
# ---------------------------------------------------------------------------
# get out
# ---------------------------------------------------------------------------
quit
;;
*)
log "Invalid choice..."
exit 1
;;
esac