aniwrapper/aniwrapper
2022-01-02 15:15:16 -08:00

267 lines
6.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeo pipefail
#############
# Globals #
#############
[ -z "$XDG_CONFIG_HOME" ] && XDG_CONFIG_HOME="$HOME/.config"
CMD="/usr/bin/ani-cli"
DEFAULT_DOWNLOAD="$HOME/Videos/sauce"
CFG_DIR="$XDG_CONFIG_HOME/aniwrapper"
DEFAULT_PLAYLIST="$CFG_DIR/playlists/playlist.txt"
CFG_FILE="meh.rasi"
QUALITIES="1. best (default)|2. 1080p|3. 720p|4. 480p|5. 360p|6. worst"
QUALITY="best"
PLAYER_CMD="mpv"
GET_QUALITY=0
VERBOSE=0
IS_ROFI=1
IS_DOWNLOAD=0
quit="6. Quit"
options="1. Stream|2. Download|3. Continue|4. Play from File|5. Sync History|$quit"
#############
# Functions #
#############
get_quality() {
if [[ "$IS_ROFI" -eq 1 ]]; then
selection=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \
-l 6 -theme-str 'listview {columns: 1;}' -p "Choose video quality:" \
-sep '|' <<< "$QUALITIES")
QUALITY=$(awk '{print $2}' <<< "$selection")
else
printf "%s" "Enter quality [ best|1080|720|480|360|worst ]: "
read -r QUALITY
fi
log "selected quality: $QUALITY"
# if [[ "$QUALITY" != 'best' ]] && [[ "$QUALITY" != 'worst' ]]; then
# seppuku "Something went wrong getting the quality: $QUALITY... exiting"
# fi
}
seppuku() {
printf "%s\n" "$*"
exit 1
}
quit() {
printf "%s\n" 'Quitting...'
exit 0
}
run() {
if [[ "$GET_QUALITY" -eq 1 ]]; then
get_quality
else
log "QUALITY flag not set... using default -> $QUALITY"
fi
if [[ "$VERBOSE" -eq 0 ]]; then
"$CMD" -q "$QUALITY" "$*"
else
"$CMD" -q "$QUALITY" -v "$*"
fi
}
log() {
if [[ "$VERBOSE" -eq 1 ]]; then
printf "%s\n" "$*"
fi
}
play_file() {
# opens the passed in file with $PLAYER_CMD
log "Checking if file is playable"
if [[ "$1" =~ (\.mp4|\.mkv|\.ts)$ ]]; then
log "File is playable..."
log "Playing file: $1"
if [[ "$VERBOSE" -eq 1 ]]; then
$PLAYER_CMD "$1"
else
$PLAYER_CMD "$1" &> /dev/null &
fi
exit $?
else
seppuku "File: $1 is not playable... Quitting"
fi
}
check_path() {
pth="$1"
if ! [ -d "$pth" ]; then
seppuku "ERROR: Passed in directory is not valid: $inp"
elif [[ $(find "$pth" -type f | wc -l) -eq 0 ]]; then
seppuku "ERROR: Passed in directory is empty: $inp"
fi
}
find_videos() {
# recursive function for finding path to video file given a starting directory
inp="$1"
# log "INPUT: $inp"
if [ -f "$inp" ]; then
echo "$inp"
return 0
fi
check_path "$inp"
selection=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \
-l 12 -i -p "Enter selection" <<< "$(ls "$inp")")
if [ -z "$selection" ]; then
seppuku "selection is empty... exiting"
elif [ -d "$inp/$selection" ] || [ -f "$inp/$selection" ]; then
find_videos "$inp/$selection"
return $?
fi
}
########
# Main #
########
while getopts 'vhqcd' 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..."
;;
*)
log "Invalid option"
exit 1
;;
esac
done
log "CONFIG DIR: $CFG_DIR"
# 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"
run "-cd $dl_dir"
exit $?
fi
choice=$(echo "${options[@]}" | rofi -dmenu -sep '|' \
-config "$CFG_DIR/$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_DIR/$CFG_FILE" \
-l 1 -p "Enter download dir:")
# if dl_dir is none set to current directory
[ "$dl_dir" == "" ] && dl_dir="$DEFAULT_DOWNLOAD"
# make sure download directory exists
if [ ! -d "$DEFAULT_DOWNLOAD" ]; then
mkdir -p "$DEFAULT_DOWNLOAD"
fi
run -d "$dl_dir"
;;
3.)
# ---------------------------------------------------------------------------
# continue
# ---------------------------------------------------------------------------
log "Continue watching"
run -H
;;
4.)
# ---------------------------------------------------------------------------
# play
# ---------------------------------------------------------------------------
msg="Enter path to the videos directory or leave blank to go with the default: $HOME/Videos/sauce/"
span="<span foreground='peachpuff' style='italic' size='small' weight='light'>$msg</span>"
play_dir=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \
-l 1 -mesg "$span" -p "Enter path to play dir:")
[ -z "$play_dir" ] && play_dir="$DEFAULT_DOWNLOAD"
log "PLAY DIR: $play_dir"
[ ! -d "$play_dir" ] && seppuku "$play_dir does not exist"
video_path=$(find_videos "$play_dir")
log "VIDEO PATH: $video_path"
if [ -z "$video_path" ]; then
seppuku "Something went wrong getting path"
fi
play_file "$video_path"
;;
5.)
log "Sync history database"
username=$(rofi -dmenu -config "$CFG_DIR/$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_DIR/$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_DIR/$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_DIR/$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"
exit 0
fi
;;
6.)
# ---------------------------------------------------------------------------
# get out
# ---------------------------------------------------------------------------
printf "%s\n" "Quitting..."
exit 0
;;
*)
log "Invalid choice..."
exit 1
;;
esac