improvements

This commit is contained in:
ksyasuda 2022-01-08 14:55:17 -08:00
parent aaaad7d986
commit 99691746c7

135
ani-cli
View File

@ -144,6 +144,15 @@ run_stmt() {
printf "%s\n" "$1" | sqlite3 -noheader "$HISTORY_DB" printf "%s\n" "$1" | sqlite3 -noheader "$HISTORY_DB"
} }
notification() {
msg="$*"
if command -v "notify-send" > /dev/null; then
notify-send -i "$ANIWRAPPER_ICON_PATH" "$msg"
else
logger "$msg"
fi
}
##################### #####################
## Database Code ## ## Database Code ##
##################### #####################
@ -153,7 +162,7 @@ check_db() {
# args: # args:
# $1: anime name: str # $1: anime name: str
# $2: either 'search' or 'watch' for which db to query # $2: either 'search' or 'watch' for which db to query
logger "BEGIN check_db()" 1> /dev/stderr # logger "BEGIN check_db()"
if [[ "$1" == "directory" ]]; then if [[ "$1" == "directory" ]]; then
stmt="SELECT DISTINCT COUNT(*) \ stmt="SELECT DISTINCT COUNT(*) \
FROM file_history \ FROM file_history \
@ -174,7 +183,7 @@ check_db() {
AND episode_number = '$2';" AND episode_number = '$2';"
fi fi
res=$(run_stmt "$stmt") res=$(run_stmt "$stmt")
logger "END check_db... Result -> $res" # logger "END check_db... Result -> $res"
return "$res" return "$res"
} }
@ -206,8 +215,6 @@ update_date() {
[ -z "$temp_dt" ] && return 1 [ -z "$temp_dt" ] && return 1
hist_dt=$(run_stmt "SELECT watch_date FROM watch_history WHERE anime_name='$1' AND episode_number='$2';") hist_dt=$(run_stmt "SELECT watch_date FROM watch_history WHERE anime_name='$1' AND episode_number='$2';")
hist_dt="${hist_dt// /:}" hist_dt="${hist_dt// /:}"
logger "PASSED IN DATE: $temp_dt"
logger "DB DATE: $hist_dt"
logger "Checking if update is needed..." logger "Checking if update is needed..."
if ! check_date "$hist_dt" "$temp_dt"; then if ! check_date "$hist_dt" "$temp_dt"; then
logger "Passed in date is older or same than current date... doing nothing" logger "Passed in date is older or same than current date... doing nothing"
@ -229,19 +236,12 @@ update_date() {
insert_history() { insert_history() {
# inserts into search/watch history db # inserts into search/watch history db
# check the anime_name/id # check the anime_name/id
logger "BEGIN: function insert_history()"
datetime=$(date +'%Y-%m-%d %H:%M:%S') datetime=$(date +'%Y-%m-%d %H:%M:%S')
logger "Checking if row exists in db" logger "Checking if ($*) exists in db"
check_db "$@" check_db "$@"
res="$?" res="$?"
if [[ $res -gt 0 ]]; then if [[ $res -gt 0 ]]; then
if [[ "$1" == "file" ]]; then logger "Match found... Updating row in history db..."
logger "Already in file history db... updaing watch_date"
elif [[ "$2" == "search" ]]; then
logger "Already in search db... Updating search_date"
else
logger "Already in watch db... Updating watch_date"
fi
update_date "$@" update_date "$@"
res=$? res=$?
else else
@ -261,7 +261,6 @@ insert_history() {
run_stmt "$stmt" run_stmt "$stmt"
res=$? res=$?
fi fi
logger "END: function insert_history()"
return $res return $res
} }
@ -270,19 +269,20 @@ sync_search_history() {
errs=0 errs=0
while read -r line; do while read -r line; do
anime_name=$(awk -F '|' '{print $2}' <<< "$line") anime_name=$(awk -F '|' '{print $2}' <<< "$line")
logger "Checking if $anime_name has been searched..."
res=$(sqlite3 -noheader "$HISTORY_DB" "SELECT COUNT(*) FROM search_history WHERE anime_name = '$anime_name'") res=$(sqlite3 -noheader "$HISTORY_DB" "SELECT COUNT(*) FROM search_history WHERE anime_name = '$anime_name'")
if [[ "$res" -eq 0 ]]; then if [[ "$res" -eq 0 ]]; then
search_date=$(awk -F '|' '{print $3}' <<< "$line") search_date=$(awk -F '|' '{print $3}' <<< "$line")
logger "Adding ($anime_name|$search_date) to search history..." logger "Not found in db... Adding ($anime_name|$search_date) to search history..."
if ! sqlite3 "$HISTORY_DB" "INSERT INTO search_history(anime_name, search_date) VALUES('$anime_name', '$search_date')"; then if ! sqlite3 "$HISTORY_DB" "INSERT INTO search_history(anime_name, search_date) VALUES('$anime_name', '$search_date')"; then
log "Error inserting row $line... skipping"
((++errs)) ((++errs))
continue continue
fi fi
((++cnt)) ((++cnt))
fi fi
done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT * FROM search_history")" logger
logger "Inserted $cnt rows into search_history table" done <<< "$(sqlite3 -list -noheader "$temp_db" "SELECT DISTINCT * FROM search_history")"
logger "$cnt rows inserted into search_history table"
logger "$errs errors on insert" logger "$errs errors on insert"
} }
@ -290,25 +290,24 @@ sync_watch_history() {
cnt=0 cnt=0
errs=0 errs=0
while read -r line; do while read -r line; do
# anime_name=$(awk -F '|' '{print $2}' <<<"$line")
anime_name="${line/ //}" anime_name="${line/ //}"
logger "ANIME: $anime_name" # some spacing for log messages
logger && logger "ANIME: $anime_name"
episodes=$(sqlite3 -list -noheader "$temp_db" "SELECT episode_number, watch_date FROM watch_history WHERE anime_name = '$anime_name'") episodes=$(sqlite3 -list -noheader "$temp_db" "SELECT episode_number, watch_date FROM watch_history WHERE anime_name = '$anime_name'")
# for each episode of $anime_name on the remote machine, check local # for each episode of $anime_name on the remote machine, check local
while read -r ep; do while read -r ep; do
# logger "ROW: $ep" logger
episode_num=$(awk -F '|' '{print $1}' <<< "$ep") episode_num=$(awk -F '|' '{print $1}' <<< "$ep")
watch_date=$(awk -F '|' '{print $NF}' <<< "$ep") watch_date=$(awk -F '|' '{print $NF}' <<< "$ep")
if ! insert_history "$anime_name" "$episode_num" "$watch_date"; then if ! insert_history "$anime_name" "$episode_num" "$watch_date"; then
logger "Error inserting row ($anime_name|$episode_num|$watch_date)... skipping"
((++errs)) ((++errs))
continue continue
fi fi
((++cnt)) ((++cnt))
done <<< "${episodes[@]}" done <<< "${episodes[@]}"
done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT anime_name FROM watch_history")" done <<< "$(sqlite3 -list -noheader "$temp_db" "SELECT DISTINCT anime_name FROM watch_history")"
logger "Inserted $cnt rows into watch_history table" logger "$cnt rows inserted into watch_history table"
logger "$errs errors on insert" logger "$errs rows skipped on insert"
} }
##################### #####################
@ -328,12 +327,14 @@ play_file() {
if [[ "$1" =~ .mp3 ]]; then if [[ "$1" =~ .mp3 ]]; then
logger ".mp3 file found... playing without video" logger ".mp3 file found... playing without video"
logger "MPV COMMAND: $PLAYER_CMD --no-video $1" logger "MPV COMMAND: $PLAYER_CMD --no-video $1"
notification "Playing $1"
$PLAYER_CMD --no-video "$1" $PLAYER_CMD --no-video "$1"
else else
notification "Playing $1"
logger "MPV COMMAND: $PLAYER_CMD $1" logger "MPV COMMAND: $PLAYER_CMD $1"
$PLAYER_CMD "$1" $PLAYER_CMD "$1"
fi fi
exit $? return $?
else else
die "File: $1 is not playable... Quitting" die "File: $1 is not playable... Quitting"
fi fi
@ -349,14 +350,14 @@ generate_inputlist() {
fi fi
[ "$outstr" = "" ] && outstr="$directory" || outstr="$outstr|$directory" [ "$outstr" = "" ] && outstr="$directory" || outstr="$outstr|$directory"
done <<< "$(find "$1" -maxdepth 1 -type d | sed "s|$1/||" | tail -n +2 | sort -V)" done <<< "$(find "$1" -maxdepth 1 -type d | sed "s|$1/||" | tail -n +2 | sort -V)"
logger "DIRS: $outstr" 1> /dev/stderr # logger "DIRS: $outstr" 1> /dev/stderr
while read -r filename; do while read -r filename; do
if [[ "${filename// /}" == "" ]]; then if [[ "${filename// /}" == "" ]]; then
continue continue
fi fi
[ "$outstr" = "" ] && outstr="$filename" || outstr="$outstr|$filename" [ "$outstr" = "" ] && outstr="$filename" || outstr="$outstr|$filename"
done <<< "$(find "$1" -maxdepth 1 -type f | sed "s|$1/||" | grep -E "$playable$" | sort -V)" done <<< "$(find "$1" -maxdepth 1 -type f | sed "s|$1/||" | grep -E "$playable$" | sort -V)"
logger "DIRS + FILES: $outstr" 1> /dev/stderr # logger "DIRS + FILES: $outstr" 1> /dev/stderr
outstr="$outstr|Back|Quit" outstr="$outstr|Back|Quit"
printf "%s\n" "$outstr" printf "%s\n" "$outstr"
} }
@ -394,7 +395,7 @@ generate_file_watchedlist() {
# 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_media() { find_media() {
inp="$1" inp="$1"
[ -z "$inp" ] && inp="/" [ -z "$inp" ] && die "No directory"
# workaround to allow logging w/o affecting return output # workaround to allow logging w/o affecting return output
logger "INPUT DIR: $inp" 1> /dev/stderr logger "INPUT DIR: $inp" 1> /dev/stderr
@ -408,27 +409,41 @@ find_media() {
inputlist=$(generate_inputlist "$inp") inputlist=$(generate_inputlist "$inp")
watched_files=$(generate_file_watchedlist "$inp") watched_files=$(generate_file_watchedlist "$inp")
logger "watched files -> $watched_files" logger "watched files -> $watched_files"
selection=$(rofi -dmenu -only-match -config "$ROFI_CFG" \ selection=$(rofi -dmenu -only-match -async-pre-read 33 -config "$ROFI_CFG" \
-l 13 -i -sep '|' -mesg "$span" -a "$watched_files" \ -l 12 -i -sep '|' -mesg "$span" -a "$watched_files" \
-p "Enter selection" <<< "${inputlist[@]}") -p "Enter selection" <<< "${inputlist[@]}")
if [ -z "$selection" ] || [ "$selection" = "Quit" ]; then case "$selection" in
return 1 Back)
elif [ "$selection" = "Back" ]; then # go up one directory
# go up one directory # dotdotslash=$(sed -E "s/(\/[^\/]*$)//" <<< "$inp")
find_media "$(sed -E "s/(\/[^\/]*$)//" <<< "$inp")" if [ -z "$(sed -E "s/(\/[^\/]*$)//" <<< "$inp")" ]; then
elif [ -d "$inp/$selection" ] || [ -f "$inp/$selection" ]; then find_media "/"
find_media "$inp/$selection" else
return $? find_media "$(sed -E "s/(\/[^\/]*$)//" <<< "$inp")"
fi fi
;;
Quit)
return 1
;;
*)
if [ -d "$inp/$selection" ] || [ -f "$inp/$selection" ]; then
if [ "$inp" = "/" ]; then
find_media "$selection"
else
find_media "$inp/$selection"
fi
return $?
else
return 1
fi
;;
esac
} }
# get query # get query
get_search_query() { get_search_query() {
# Query the anime to stream/download
# Get search history # Get search history
# Construct string "<id>. <anime_name>"
stmt="SELECT DISTINCT id || '. ' || anime_name \ stmt="SELECT DISTINCT id || '. ' || anime_name \
FROM search_history \ FROM search_history \
ORDER BY id DESC;" ORDER BY id DESC;"
@ -477,7 +492,6 @@ anime_selection() {
done <<- EOF done <<- EOF
$search_results $search_results
EOF EOF
menu+="$count. Quit" menu+="$count. Quit"
searched="" searched=""
@ -487,7 +501,7 @@ anime_selection() {
logger "ANIME: $anime" logger "ANIME: $anime"
check_db "$anime" "search" check_db "$anime" "search"
if [[ $? -gt 0 ]]; then if [[ $? -gt 0 ]]; then
logger "SEARCHED BEFORE" logger "$anime HAS BEEN SEARCHED BEFORE"
if [ -z "$searched" ]; then if [ -z "$searched" ]; then
searched="$cnt" searched="$cnt"
else else
@ -496,15 +510,15 @@ anime_selection() {
fi fi
((++cnt)) ((++cnt))
done done
logger "SEARCHED: $searched" logger "SEARCHED: $searched"
# get the anime from indexed list # get the anime from indexed list
msg="<span foreground='peachpuff' style='italic' size='small' weight='normal'>Query: $query</span>" msg="<span foreground='peachpuff' style='italic' size='small' weight='normal'><b>Query: $query</b></span>"
user_input=$(printf "${menu[@]}" | user_input=$(printf "${menu[@]}" |
rofi -dmenu -config "$ROFI_CFG" \ rofi -dmenu -config "$ROFI_CFG" \
-a "$searched" \ -a "$searched" \
-l 12 -i -p "Enter selection:" \ -l 12 -i -p "Enter selection:" \
-async-pre-read 33 \
-mesg "$msg" -only-match) -mesg "$msg" -only-match)
[ -z "$user_input" ] && return 1 [ -z "$user_input" ] && return 1
if [ $(awk '{ print $NF }' <<< "$user_input") = "Quit" ]; then if [ $(awk '{ print $NF }' <<< "$user_input") = "Quit" ]; then
@ -520,7 +534,6 @@ anime_selection() {
menu_format_string='[%d] %s\n' menu_format_string='[%d] %s\n'
menu_format_string_c1="$c_blue[$c_cyan%d$c_blue] $c_reset%s\n" menu_format_string_c1="$c_blue[$c_cyan%d$c_blue] $c_reset%s\n"
menu_format_string_c2="$c_blue[$c_cyan%d$c_blue] $c_yellow%s$c_reset\n" menu_format_string_c2="$c_blue[$c_cyan%d$c_blue] $c_yellow%s$c_reset\n"
count=1 count=1
while read anime_id; do while read anime_id; do
# alternating colors for menu # alternating colors for menu
@ -531,8 +544,6 @@ anime_selection() {
printf "$menu_format_string" "$count" "$anime_id" printf "$menu_format_string" "$count" "$anime_id"
count=$((count + 1)) count=$((count + 1))
done <<< "$search_results" done <<< "$search_results"
# User input
printf "$c_blue%s$c_green" "Enter number: " printf "$c_blue%s$c_green" "Enter number: "
read choice read choice
printf "$c_reset" printf "$c_reset"
@ -563,7 +574,6 @@ anime_selection() {
EOF EOF
[ -z "$name" ] && name="$anime_id" [ -z "$name" ] && name="$anime_id"
logger "NAME: $name"
insert_history "$name" "search" & insert_history "$name" "search" &
printf "$c_reset" printf "$c_reset"
@ -601,7 +611,8 @@ episode_selection() {
done done
# get user choice and set the start and end # get user choice and set the start and end
msg='<span foreground="peachpuff" style="italic" size="small" weight="light">Range of episodes can be provided as: START_EPISODE - END_EPISODE</span>' # msg='<span foreground="peachpuff" style="italic" size="small" weight="light"></span>'
msg=$(printf "%s\n%s\n" "$(generate_span "<b>Anime Name: $anime_id</b>")" "$(generate_span "Range of episodes can be provided as: START_EPISODE - END_EPISODE")")
choice=$( choice=$(
seq 1 "$last_ep_number" | seq 1 "$last_ep_number" |
rofi -dmenu -l 12 \ rofi -dmenu -l 12 \
@ -622,7 +633,6 @@ episode_selection() {
printf "$c_reset" printf "$c_reset"
fi fi
# check for half episode # check for half episode
logger "Checking if selected a half episode"
if [ "$(echo "$ep_choice_start" | awk '{ printf substr($0, 1, 1) }')" = "h" ]; then if [ "$(echo "$ep_choice_start" | awk '{ printf substr($0, 1, 1) }')" = "h" ]; then
logger "IS A HALF EPISODE" logger "IS A HALF EPISODE"
half_ep=1 half_ep=1
@ -637,7 +647,6 @@ episode_selection() {
if [[ "$ep_choice_start" -eq "$ep_choice_end" ]]; then if [[ "$ep_choice_start" -eq "$ep_choice_end" ]]; then
ep_choice_end="" ep_choice_end=""
fi fi
printf "$c_reset" printf "$c_reset"
} }
@ -674,7 +683,11 @@ open_episode() {
logger "REFERRER: $referer_link" logger "REFERRER: $referer_link"
nohup "$player_fn" --http-header-fields="Referer:$referer_link" "$video_url" > /dev/null 2>&1 & nohup "$player_fn" --http-header-fields="Referer:$referer_link" "$video_url" > /dev/null 2>&1 &
PID=$! PID=$!
printf "${c_green}\nVideo playing" if command -v "notify-send" > /dev/null; then
notify-send -i "$ANIWRAPPER_ICON_PATH" "Playing $anime_id - Episode $episode"
else
printf "${c_green}\nVideo playing"
fi
else else
logger "Downloading episode $episode ..." logger "Downloading episode $episode ..."
logger "$video_url" logger "$video_url"
@ -689,9 +702,9 @@ open_episode() {
notify-send -i "$MAISAN_ICON_PATH" "Download failed for ${anime_id//-/ } - Episode: $episode. Please retry or check your internet connection" notify-send -i "$MAISAN_ICON_PATH" "Download failed for ${anime_id//-/ } - Episode: $episode. Please retry or check your internet connection"
else else
aria2c -x 16 -s 16 --referer="$referer_link" "$video_url" --dir="$dl_dir" -o "$episode.mp4" --download-result=hide && aria2c -x 16 -s 16 --referer="$referer_link" "$video_url" --dir="$dl_dir" -o "$episode.mp4" --download-result=hide &&
notify-send -i "$ANIWRAPPER_ICON_PATH" "Download complete for ${anime_id//-/ } - Episode: $episode" ||
printf "${c_green}Downloaded complete for %s - Episode: %s${c_reset}\n" "${anime_id//-/ }" "$episode" || printf "${c_green}Downloaded complete for %s - Episode: %s${c_reset}\n" "${anime_id//-/ }" "$episode" ||
printf "${c_red}Download failed for %s - Episode: %s, please retry or check your internet connection${c_reset}\n" "${anime_id//-/ }" "$episode" printf "${c_red}Download failed for %s - Episode: %s, please retry or check your internet connection${c_reset}\n" "${anime_id//-/ }" "$episode"
fi fi
} }
fi fi
@ -903,11 +916,15 @@ case $scrape in
file) file)
logger "STARTING DIR: $play_dir" logger "STARTING DIR: $play_dir"
[ ! -d "$play_dir" ] && die "$play_dir does not exist" [ ! -d "$play_dir" ] && die "$play_dir does not exist"
video_path=$(find_media "$play_dir") || die video_path=$(find_media "$play_dir")
logger "VIDEO PATH: $video_path" retcode="$?"
if [ -z "$video_path" ]; then echo "RETURN FROM find_media() -> $retcode"
if [ "$retcode" -ne 0 ]; then
die "QUITTING"
elif [ -z "$video_path" ]; then
die "Something went wrong getting path... path is empty" die "Something went wrong getting path... path is empty"
fi fi
logger "VIDEO PATH: $video_path"
play_file "$video_path" play_file "$video_path"
exit $? exit $?
;; ;;