make play from file safer

This commit is contained in:
ksyasuda 2022-01-04 00:57:22 -08:00
parent f814497f44
commit f9d4f65f22
2 changed files with 70 additions and 36 deletions

View File

@ -365,7 +365,7 @@ anime_selection() {
rofi -dmenu -config "$CFG_DIR/${ROFI_CFG}" \ rofi -dmenu -config "$CFG_DIR/${ROFI_CFG}" \
-a "$searched" \ -a "$searched" \
-l 12 -i -p "Enter selection:" \ -l 12 -i -p "Enter selection:" \
-mesg "$msg") -mesg "$msg" -only-match)
[ -z "$user_input" ] && return 1 [ -z "$user_input" ] && return 1
choice=$(printf '%s\n' "$user_input" | awk '{print $1}') choice=$(printf '%s\n' "$user_input" | awk '{print $1}')

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -Eeo pipefail # set -Eeo pipefail
############# #############
# Globals # # Globals #
@ -19,21 +19,15 @@ IS_DOWNLOAD=0
quit="6. Quit" quit="6. Quit"
options="1. Stream|2. Download|3. Continue|4. Play from File|5. Sync History|$quit" options="1. Stream|2. Download|3. Continue|4. Play from File|5. Sync History|$quit"
playable="\.mp4|\.mkv|\.ts"
############# #############
# Functions # # Functions #
############# #############
get_quality() { log() {
if [[ "$IS_ROFI" -eq 1 ]]; then if [[ "$VERBOSE" -eq 1 ]]; then
selection=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \ printf "%s\n" "$*"
-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 fi
log "selected quality: $QUALITY"
} }
seppuku() { seppuku() {
@ -59,16 +53,23 @@ run() {
fi fi
} }
log() { get_quality() {
if [[ "$VERBOSE" -eq 1 ]]; then if [ "$IS_ROFI" -eq 1 ]; then
printf "%s\n" "$*" 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 fi
log "selected quality: $QUALITY"
} }
# opens the passed in file with $PLAYER_CMD # opens the passed in file with $PLAYER_CMD
play_file() { play_file() {
log "Checking if file is playable" log "Checking if file is playable"
if [[ "$1" =~ (\.mp4|\.mkv|\.ts)$ ]]; then if [[ "$1" =~ ($playable)$ ]]; then
log "File is playable..." log "File is playable..."
log "Playing file: $1" log "Playing file: $1"
if [[ "$VERBOSE" -eq 1 ]]; then if [[ "$VERBOSE" -eq 1 ]]; then
@ -82,27 +83,59 @@ play_file() {
fi fi
} }
check_path() { # generates a span mesg for rofi given
pth="$1" # input: message: str
if ! [ -d "$pth" ]; then generate_span() {
seppuku "ERROR: Passed in directory is not valid: $inp" msg="$*"
elif [[ $(find "$pth" -type f | wc -l) -eq 0 ]]; then span="<span foreground='peachpuff' style='italic' size='small' weight='light'>$msg</span>"
seppuku "ERROR: Passed in directory is empty: $inp" printf "%s\n" "$span"
fi }
# attempt to generate list of valid files and directories
generate_inputlist() {
# start at 2nd line, because first line out output from find is $1
outstr=""
while read -r directory; do
if [[ "${directory// /}" == "" ]]; then
continue
fi
if [[ "$outstr" == "" ]]; then
outstr="$directory"
else
outstr="$outstr|$directory"
fi
done <<< "$(find "$1" -maxdepth 1 -type d | sed "s|$1/||" | tail -n +2 | sort -V)"
while read -r filename; do
if [[ "${filename// /}" == "" ]]; then
continue
fi
if [[ "$outstr" == "" ]]; then
outstr="$filename"
else
outstr="$outstr|$filename"
fi
done <<< "$(find "$1" -maxdepth 1 -type f | sed "s|$1/||" | grep -E "$playable$" | sort -V)"
outstr="$outstr|Quit"
printf "%s\n" "$outstr"
} }
# recursive function for finding path to video file given a starting directory # recursive function for finding path to video file given a starting directory
find_videos() { find_videos() {
inp="$1" inp="$1"
# base case hit when a file is found
if [ -f "$inp" ]; then if [ -f "$inp" ]; then
echo "$inp" printf "%s\n" "$inp"
return 0 return 0
fi fi
check_path "$inp"
selection=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \ span=$(generate_span "Current directory: $inp")
-l 12 -i -p "Enter selection" <<< "$(ls "$inp")") inputlist=$(generate_inputlist "$inp")
if [ -z "$selection" ]; then selection=$(rofi -dmenu -only-match -config "$CFG_DIR/$CFG_FILE" \
seppuku "selection is empty... exiting" -l 13 -i -sep '|' -mesg "$span" -p "Enter selection" <<< "${inputlist[@]}")
if [ -z "$selection" ] || [ "$selection" = "Quit" ]; then
return 1
elif [ -d "$inp/$selection" ] || [ -f "$inp/$selection" ]; then elif [ -d "$inp/$selection" ] || [ -f "$inp/$selection" ]; then
find_videos "$inp/$selection" find_videos "$inp/$selection"
return $? return $?
@ -154,7 +187,10 @@ elif [[ "$IS_ROFI" -eq 0 ]] && [[ "$IS_DOWNLOAD" -eq 1 ]]; then
exit $? exit $?
fi fi
choice=$(echo "${options[@]}" | rofi -dmenu -sep '|' \ # -------------------------------------------------------------------------------
# Main
# -------------------------------------------------------------------------------
choice=$(echo "${options[@]}" | rofi -dmenu -only-match -sep '|' \
-config "$CFG_DIR/$CFG_FILE" -l 6 -i -p "Aniwrapper") -config "$CFG_DIR/$CFG_FILE" -l 6 -i -p "Aniwrapper")
[ "$choice" == "$quit" ] && quit [ "$choice" == "$quit" ] && quit
@ -197,13 +233,13 @@ case "$selection" in
# play # play
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
msg="Enter path to the videos directory or leave blank to go with the default: $HOME/Videos/sauce/" 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>" span=$(generate_span "$msg")
play_dir=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \ play_dir=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \
-l 1 -mesg "$span" -p "Enter path to play dir:") -l 1 -mesg "$span" -p "Enter path to play dir:")
[ -z "$play_dir" ] && play_dir="$DEFAULT_DOWNLOAD" [ -z "$play_dir" ] && play_dir="$DEFAULT_DOWNLOAD"
log "PLAY DIR: $play_dir" log "PLAY DIR: $play_dir"
[ ! -d "$play_dir" ] && seppuku "$play_dir does not exist" [ ! -d "$play_dir" ] && seppuku "$play_dir does not exist"
video_path=$(find_videos "$play_dir") video_path=$(find_videos "$play_dir") || quit
log "VIDEO PATH: $video_path" log "VIDEO PATH: $video_path"
if [ -z "$video_path" ]; then if [ -z "$video_path" ]; then
seppuku "Something went wrong getting path" seppuku "Something went wrong getting path"
@ -243,19 +279,17 @@ case "$selection" in
exit 1 exit 1
else else
log "Databases synced successfully" log "Databases synced successfully"
exit 0 quit
fi fi
;; ;;
6.) 6.)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# get out # get out
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
printf "%s\n" "Quitting..." quit
exit 0
;; ;;
*) *)
log "Invalid choice..." log "Invalid choice..."
exit 1 exit 1
;; ;;
esac esac