move play from file function to ani-cli script

This commit is contained in:
ksyasuda 2022-01-04 18:07:41 -08:00
parent b48b849408
commit 090f0e84e7
2 changed files with 122 additions and 105 deletions

113
ani-cli
View File

@ -8,11 +8,12 @@ HISTORY_DB="$CFG_DIR/history.sqlite3"
ANIWRAPPER_ICON_PATH="$CFG_DIR/icons/icon-64.png"
MAISAN_ICON_PATH="$CFG_DIR/icons/MYsan.png"
ROFI_CFG="aniwrapper.rasi"
PLAYER_CMD="mpv -config-dir ${XDG_CONFIG_HOME:-$HOME/.config}/mpv"
# dependencies: grep, sed, curl, video_player, rofi, sqlite3
# video_player ( needs to be able to play urls )
player_fn="mpv"
playable="\.mp4|\.mkv|\.ts|\.mp3"
prog="ani-cli"
c_red="\033[1;31m"
@ -291,6 +292,91 @@ sync_watch_history() {
## END of db code ##
#####################
#####################
### Play from file###
#####################
# opens the passed in file with $PLAYER_CMD
play_file() {
log "Checking if file is playable"
if [[ "$1" =~ ($playable)$ ]]; then
log "File is playable..."
if [[ "$1" =~ .mp3 ]]; then
log ".mp3 file found... playing without video"
log "MPV COMMAND: $PLAYER_CMD --no-video $1"
$PLAYER_CMD --no-video "$1"
else
log "MPV COMMAND: $PLAYER_CMD $1"
$PLAYER_CMD "$1"
fi
exit $?
else
die "File: $1 is not playable... Quitting"
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)"
log "DIRS: $outstr" 1> /dev/stderr
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)"
log "DIRS + FILES: $outstr" 1> /dev/stderr
outstr="$outstr|Back|Quit"
printf "%s\n" "$outstr"
}
# recursive function for finding path to video file given a starting directory
find_media() {
inp="$1"
[ -z "$inp" ] && inp="/"
# workaround to allow logging w/o affecting return output
log "INPUT DIR: $inp" 1> /dev/stderr
# base case hit when a file is found
if [ -f "$inp" ]; then
printf "%s\n" "$inp"
return 0
fi
span=$(generate_span "Current directory: $inp")
inputlist=$(generate_inputlist "$inp")
selection=$(rofi -dmenu -only-match -config "$CFG_DIR/$ROFI_CFG" \
-l 13 -i -sep '|' -mesg "$span" -p "Enter selection" <<< "${inputlist[@]}")
if [ -z "$selection" ] || [ "$selection" = "Quit" ]; then
return 1
elif [ "$selection" = "Back" ]; then
# go up one directory
find_media "$(sed -E "s/(\/[^\/]*$)//" <<< "$inp")"
elif [ -d "$inp/$selection" ] || [ -f "$inp/$selection" ]; then
find_media "$inp/$selection"
return $?
fi
}
###########################
## END of Play from file ##
###########################
# get query
get_search_query() {
# Query the anime to stream/download
@ -307,7 +393,7 @@ get_search_query() {
if [ -z "$*" ] && [ "$is_rofi" -eq 1 ]; then
query=$(rofi -dmenu -l 12 -p "Search Anime:" \
-mesg "$span" \
-config "$CFG_DIR/${ROFI_CFG}" <<< "${hist[@]}")
-config "$CFG_DIR/$ROFI_CFG" <<< "${hist[@]}")
# Remove the id from the query
query="${query//[1-9]*\. /}"
# query="${query// /}"
@ -320,6 +406,12 @@ get_search_query() {
fi
}
generate_span() {
msg="$*"
span="<span foreground='peachpuff' style='italic' size='small' weight='light'>$msg</span>"
printf "%s\n" "$span"
}
#####################
## Anime selection ##
#####################
@ -596,7 +688,7 @@ is_rofi=1
is_download=0
download_dir="."
half_ep=0
while getopts 'hd:Hsvq:c-:' OPT; do
while getopts 'hd:Hsvq:c-:f:' OPT; do
case "$OPT" in
h)
help_text
@ -634,6 +726,11 @@ while getopts 'hd:Hsvq:c-:' OPT; do
;;
esac
;;
f)
scrape="file"
play_dir="$OPTARG"
;;
*)
printf "%s\n" "Invalid option"
exit 1
@ -701,6 +798,16 @@ case $scrape in
sync_search_history && sync_watch_history
exit 0
;;
file)
log "STARTING DIR: $play_dir"
[ ! -d "$play_dir" ] && die "$play_dir does not exist"
video_path=$(find_media "$play_dir") || die
log "VIDEO PATH: $video_path"
if [ -z "$video_path" ]; then
die "Something went wrong getting path... path is empty"
fi
play_file "$video_path"
;;
esac
check_input

View File

@ -74,25 +74,6 @@ get_quality() {
log "selected quality: $QUALITY"
}
# opens the passed in file with $PLAYER_CMD
play_file() {
log "Checking if file is playable"
if [[ "$1" =~ ($playable)$ ]]; then
log "File is playable..."
if [[ "$1" =~ .mp3 ]]; then
log ".mp3 file found... playing without video"
log "MPV COMMAND: $PLAYER_CMD --no-video $1"
$PLAYER_CMD --no-video "$1"
else
log "MPV COMMAND: $PLAYER_CMD $1"
$PLAYER_CMD "$1"
fi
exit $?
else
seppuku "File: $1 is not playable... Quitting"
fi
}
# generates a span mesg for rofi given
# input: message: str
generate_span() {
@ -101,87 +82,6 @@ generate_span() {
printf "%s\n" "$span"
}
# 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)"
log "DIRS: $outstr" 1> /dev/stderr
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)"
log "DIRS + FILES: $outstr" 1> /dev/stderr
outstr="$outstr|Back|Quit"
printf "%s\n" "$outstr"
}
# recursive function for finding path to video file given a starting directory
find_media() {
inp="$1"
[ -z "$inp" ] && inp="/"
# workaround to allow logging w/o affecting return output
log "INPUT DIR: $inp" 1> /dev/stderr
# base case hit when a file is found
if [ -f "$inp" ]; then
printf "%s\n" "$inp"
return 0
fi
span=$(generate_span "Current directory: $inp")
inputlist=$(generate_inputlist "$inp")
selection=$(rofi -dmenu -only-match -config "$CFG_DIR/$CFG_FILE" \
-l 13 -i -sep '|' -mesg "$span" -p "Enter selection" <<< "${inputlist[@]}")
if [ -z "$selection" ] || [ "$selection" = "Quit" ]; then
return 1
elif [ "$selection" = "Back" ]; then
# go up one directory
find_media "$(sed -E "s/(\/[^\/]*$)//" <<< "$inp")"
elif [ -d "$inp/$selection" ] || [ -f "$inp/$selection" ]; then
find_media "$inp/$selection"
return $?
fi
}
play_from_file() {
if [[ $# -eq 0 ]]; then
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_DIR/$CFG_FILE" \
-l 1 -mesg "$span" -p "Enter path to starting directory:")
else
play_dir="$1"
fi
[ -z "$play_dir" ] && play_dir="$DEFAULT_DOWNLOAD"
[ "$play_dir" = "$DEFAULT_DOWNLOAD" ] && create_default_download
log "STARTING DIR: $play_dir"
[ ! -d "$play_dir" ] && seppuku "$play_dir does not exist"
video_path=$(find_media "$play_dir") || quit
log "VIDEO PATH: $video_path"
if [ -z "$video_path" ]; then
seppuku "Something went wrong getting path... path is empty"
fi
play_file "$video_path"
}
########
# Main #
########
@ -232,7 +132,7 @@ elif [[ "$IS_ROFI" -eq 0 ]] && [[ "$IS_DOWNLOAD" -eq 1 ]]; then
run "-cd $dl_dir"
exit $?
elif [[ "$IS_ROFI" -eq 1 ]] && [[ "$IS_PLAY_FROM_FILE" -eq 1 ]]; then
play_from_file "$play_path"
run -f"$play_path"
exit $?
fi
@ -279,7 +179,17 @@ case "$selection" in
# play
# ---------------------------------------------------------------------------
log "Play from file selected"
play_from_file
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_DIR/$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"