Compare commits

..

2 Commits

Author SHA1 Message Date
ksyasuda
ebc8587e6f clean up code a bit 2022-02-03 22:18:51 -08:00
ksyasuda
7e71788604 restructure db code 2022-02-03 21:59:50 -08:00

141
ani-cli
View File

@ -15,7 +15,6 @@ SILENT=0
player_fn="mpv"
playable="\.mp4|\.mkv|\.ts|\.mp3|\.webm"
prog="ani-cli"
c_red="\033[1;31m"
c_green="\033[1;32m"
c_yellow="\033[1;33m"
@ -24,17 +23,6 @@ c_magenta="\033[1;35m"
c_cyan="\033[1;36m"
c_reset="\033[0m"
help_text() {
while IFS= read -r line; do
printf "%s\n" "$line"
done <<- EOF
USAGE: $prog <query>
-h show this help text
-d download episode
-H continue where you left off
EOF
}
die() {
((SILENT != 1)) && printf "$c_red%s$c_reset\n" "$*" >&2
exit 1
@ -59,8 +47,8 @@ check_input() {
fi
}
# get the download page url
get_dpage_link() {
# get the download page url
anime_id=$1
ep_no=$2
@ -155,17 +143,22 @@ run_stmt() {
# Return number of matches for anime/episode in db
check_db() {
if [[ "$1" == "directory" ]]; then
stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2';"
elif [[ "$1" == "file" ]]; then
stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2' AND filename = '$3';"
elif [[ "$2" == "search" ]]; then
stmt="SELECT DISTINCT COUNT(*) FROM search_history WHERE anime_name = '$1';"
else
stmt="SELECT DISTINCT COUNT(*) FROM watch_history WHERE anime_name = '$1' AND episode_number = '$2';"
fi
case "$1" in
directory)
stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2';"
;;
file)
stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2' AND filename = '$3';"
;;
search)
stmt="SELECT DISTINCT COUNT(*) FROM search_history WHERE anime_name = '$2';"
;;
watch | sync)
stmt="SELECT DISTINCT COUNT(*) FROM watch_history WHERE anime_name = '$2' AND episode_number = '$3';"
;;
esac
res=$(run_stmt "$stmt")
return $res
return "$res"
}
# return true (0) if $source_dt > $target_dt
@ -183,31 +176,37 @@ check_date() {
update_date() {
datetime=$(date +'%Y-%m-%d %H:%M:%S')
stmt=""
if [[ "$1" == "directory" ]]; then
lg "UPDATING FILE_HISTORY: directory='$2', filename='DIRECTORY', search_date='$datetime'"
stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';"
elif [[ "$1" == "file" ]]; then
lg "UPDATING FILE_HISTORY: directory='$2', filename='$3', search_date='$datetime'"
stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';"
elif [[ "$2" == "search" ]]; then
lg "UPDATING SEARCH_HISTORY: anime_name='$1', search_date='$datetime'"
stmt="UPDATE search_history SET search_date = '$datetime' WHERE anime_name = '$1';"
elif [[ $# -ge 3 ]]; then
temp_dt="${3// /:}"
[ -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="${hist_dt// /:}"
lg "Checking if update is needed..."
if ! check_date "$hist_dt" "$temp_dt"; then
lg "Passed in date is older or same than current date... doing nothing"
return 1
fi
lg "UPDATING watch_history from sync. watch_date -> $temp_dt"
stmt="UPDATE watch_history SET watch_date = '$temp_dt' WHERE anime_name = '$1' AND episode_number = $2;"
else
lg "UPDATING WATCH_HISTORY: anime_name='$1', episode_number='$2' search_date='$datetime'"
stmt="UPDATE watch_history SET watch_date = '$datetime' WHERE anime_name = '$1' AND episode_number = $2;"
fi
case "$1" in
directory)
lg "UPDATING FILE_HISTORY for with directory: directory='$2', filename='DIRECTORY', search_date='$datetime'"
stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';"
;;
file)
lg "UPDATING FILE_HISTORY: directory='$2', filename='$3', watch_date='$datetime'"
stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';"
;;
search)
lg "UPDATING SEARCH_HISTORY: anime_name='$2', search_date='$datetime'"
stmt="UPDATE search_history SET search_date = '$datetime' WHERE anime_name = '$2';"
;;
sync)
temp_dt="${3// /:}"
[ -z "$temp_dt" ] && return 1
hist_dt=$(run_stmt "SELECT watch_date FROM watch_history WHERE anime_name='$2' AND episode_number='$3';")
hist_dt="${hist_dt// /:}"
lg "Checking if update is needed..."
if ! check_date "$hist_dt" "$temp_dt"; then
lg "Passed in date is older or same than current date... doing nothing"
return 1
fi
lg "UPDATING WATCH_HISTORY from sync. watch_date -> $temp_dt"
stmt="UPDATE watch_history SET watch_date = '$temp_dt' WHERE anime_name = '$2' AND episode_number = $3;"
;;
watch)
lg "UPDATING WATCH_HISTORY: anime_name='$2', episode_number='$3' watch_date='$datetime'"
stmt="UPDATE watch_history SET watch_date = '$datetime' WHERE anime_name = '$2' AND episode_number = $3;"
;;
esac
wait # in case there's another insert/update still running in background?
run_stmt "$stmt"
}
@ -274,7 +273,6 @@ sync_watch_history() {
errs=0
while read -r line; do
anime_name="${line/ //}"
# for each episode of $anime_name on the remote machine, check local
while read -r ep; do
episode_num=$(awk -F '|' '{print $1}' <<< "$ep")
watch_date=$(awk -F '|' '{print $NF}' <<< "$ep")
@ -360,8 +358,7 @@ get_directory_data() {
find_media() {
inp="$1"
[ -z "$inp" ] && die "No directory"
# workaround to allow logging w/o affecting return output
lg "INPUT DIR: $inp" 1> /dev/stderr
lg "BEGIN find_media() on $inp" 1> /dev/stderr
# base case hit when a file is found
if [ -f "$inp" ]; then
@ -370,7 +367,6 @@ find_media() {
fi
get_directory_data "$inp"
[ -z "$inp" ] && return 1
selection="$(rofi -dpi "$DPI" -dmenu -only-match -async-pre-read 33 -config "$ROFI_CFG" \
-l 15 -i -sep '|' -mesg "$(generate_span "Current directory: $inp")" -a "$watched" \
-p "Enter selection" <<< "$inputlist")"
@ -408,13 +404,8 @@ find_media() {
## main code ##
#####################
# get query
get_search_query() {
# Get search history
stmt="SELECT DISTINCT id || '. ' || anime_name \
FROM search_history \
ORDER BY search_date DESC;"
stmt="SELECT DISTINCT id || '. ' || anime_name FROM search_history ORDER BY search_date DESC;"
msg="Choose from list of searched anime below, or enter a unique name of an anime to search for"
if [ "$IS_ROFI" -eq 1 ]; then
query=$(rofi -dpi "$DPI" -dmenu -l 15 -p "Search Anime:" \
@ -429,8 +420,8 @@ get_search_query() {
fi
}
# get anime name along with its id
search_anime() {
# get anime name along with its id
lg "NUM ARGS: $#"
if [[ $# -gt 1 ]]; then
# if multi-word query, concatenate into one string and replace spaces with '-'
@ -450,9 +441,7 @@ search_anime() {
}
search_eps() {
# get available episodes for anime_id
anime_id=$1
curl -s "$BASE_URL/category/$anime_id" |
sed -n -E '
/^[[:space:]]*<a href="#" class="active" ep_start/{
@ -462,8 +451,8 @@ search_eps() {
'
}
# Select anime from query results
anime_selection() {
# Select anime from query results
search_results=$*
if [ "$IS_ROFI" -eq 1 ]; then
menu=()
@ -475,7 +464,7 @@ anime_selection() {
menu+="$((cnt + 1)). $anime_id\n"
res["$cnt"]="$anime_id"
lg "ANIME: $anime_id"
if ! check_db "$anime_id" "search"; then
if ! check_db "search" "$anime_id"; then
lg "$anime_id HAS BEEN SEARCHED BEFORE"
[ -z "$searched" ] && searched="$cnt" || searched="$searched, $cnt"
fi
@ -510,7 +499,7 @@ anime_selection() {
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"
count=1
while read anime_id; do
while read -r anime_id; do
# alternating colors for menu
[ $((count % 2)) -eq 0 ] &&
menu_format_string=$menu_format_string_c1 ||
@ -558,9 +547,7 @@ episode_selection() {
if [ "$IS_ROFI" -eq 1 ]; then
# select episode number for anime
lg "Anime ID: $anime_id"
stmt="SELECT DISTINCT episode_number \
FROM watch_history \
WHERE anime_name = '$anime_id';"
stmt="SELECT DISTINCT episode_number FROM watch_history WHERE anime_name = '$anime_id';"
# Get Watch History for $anime_id as comma separated list
watch_history=""
@ -574,7 +561,9 @@ episode_selection() {
lg "Episode watch history -> $watch_history"
# get user choice and set the start and end
msg=$(printf "%s\n%s" "$(generate_span "Anime Name: $anime_id")" "$(generate_span "Range of episodes can be provided as: START_EPISODE - END_EPISODE")")
msg1="Anime Name: $anime_id"
msg2="Range of episodes can be provided as: START_EPISODE - END_EPISODE"
[ "$is_download" -eq 1 ] && msg=$(printf "%s\n%s" "$(generate_span "$msg1")" "$(generate_span "$msg2")") || msg=$(printf "%s\n" "$(generate_span "$msg1")")
choice=$(
seq 1 "$last_ep_number" |
rofi -dpi "$DPI" -dmenu -l 12 \
@ -684,7 +673,7 @@ stream() {
anime_id="${query// /}"
[ -z "$anime_id" ] && die "No anime selected or queried"
lg "Checking if anime: $anime_id has been searched before..."
check_db "$anime_id" "search"
check_db "search" "$anime_id"
searched="$?"
lg "Searched before: $searched"
if [ "$searched" -eq 0 ]; then
@ -705,7 +694,6 @@ stream() {
episode_selection
}
# option parsing
parse_args() {
# to clear the colors when exited using SIGINT
trap "printf '$c_reset'" INT HUP
@ -714,12 +702,8 @@ parse_args() {
is_download=0
download_dir="."
half_ep=0
while getopts 'hd:Hsvq:cf:t:T:CQ:D:S' OPT; do
while getopts 'd:Hsvq:cf:t:T:CQ:D:S' OPT; do
case "$OPT" in
h)
help_text
exit 0
;;
d)
is_download=1
download_dir="$OPTARG"
@ -834,11 +818,7 @@ main() {
fi
lg "SELECTION: $selection_id"
stmt="SELECT episode_number \
FROM watch_history \
WHERE anime_name = '$selection_id' \
ORDER BY watch_date DESC \
LIMIT 1"
stmt="SELECT episode_number FROM watch_history WHERE anime_name = '$selection_id' ORDER BY watch_date DESC LIMIT 1;"
ep_choice_start=$(run_stmt "$stmt")
lg "Most recently watched episode: $ep_choice_start"
;;
@ -893,7 +873,6 @@ main() {
check_input
# plays selected episode(s)
for ep in $episodes; do
open_episode "$selection_id" "$ep" "$download_dir"
done