restructure db code

This commit is contained in:
ksyasuda 2022-02-03 21:59:50 -08:00
parent 24f3c25142
commit 7e71788604

104
ani-cli
View File

@ -155,17 +155,22 @@ run_stmt() {
# Return number of matches for anime/episode in db # Return number of matches for anime/episode in db
check_db() { check_db() {
if [[ "$1" == "directory" ]]; then case "$1" in
stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2';" directory)
elif [[ "$1" == "file" ]]; then stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2';"
stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2' AND filename = '$3';" ;;
elif [[ "$2" == "search" ]]; then file)
stmt="SELECT DISTINCT COUNT(*) FROM search_history WHERE anime_name = '$1';" stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2' AND filename = '$3';"
else ;;
stmt="SELECT DISTINCT COUNT(*) FROM watch_history WHERE anime_name = '$1' AND episode_number = '$2';" search)
fi 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") res=$(run_stmt "$stmt")
return $res return "$res"
} }
# return true (0) if $source_dt > $target_dt # return true (0) if $source_dt > $target_dt
@ -183,31 +188,37 @@ check_date() {
update_date() { update_date() {
datetime=$(date +'%Y-%m-%d %H:%M:%S') datetime=$(date +'%Y-%m-%d %H:%M:%S')
stmt="" stmt=""
if [[ "$1" == "directory" ]]; then case "$1" in
lg "UPDATING FILE_HISTORY: directory='$2', filename='DIRECTORY', search_date='$datetime'" directory)
stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';" lg "UPDATING FILE_HISTORY: directory='$2', filename='DIRECTORY', search_date='$datetime'"
elif [[ "$1" == "file" ]]; then stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';"
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';" file)
elif [[ "$2" == "search" ]]; then lg "UPDATING FILE_HISTORY: directory='$2', filename='$3', watch_date='$datetime'"
lg "UPDATING SEARCH_HISTORY: anime_name='$1', search_date='$datetime'" stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';"
stmt="UPDATE search_history SET search_date = '$datetime' WHERE anime_name = '$1';" ;;
elif [[ $# -ge 3 ]]; then search)
temp_dt="${3// /:}" lg "UPDATING SEARCH_HISTORY: anime_name='$2', search_date='$datetime'"
[ -z "$temp_dt" ] && return 1 stmt="UPDATE search_history SET search_date = '$datetime' WHERE anime_name = '$2';"
hist_dt=$(run_stmt "SELECT watch_date FROM watch_history WHERE anime_name='$1' AND episode_number='$2';") ;;
hist_dt="${hist_dt// /:}" sync)
lg "Checking if update is needed..." temp_dt="${3// /:}"
if ! check_date "$hist_dt" "$temp_dt"; then [ -z "$temp_dt" ] && return 1
lg "Passed in date is older or same than current date... doing nothing" hist_dt=$(run_stmt "SELECT watch_date FROM watch_history WHERE anime_name='$2' AND episode_number='$3';")
return 1 hist_dt="${hist_dt// /:}"
fi lg "Checking if update is needed..."
lg "UPDATING watch_history from sync. watch_date -> $temp_dt" if ! check_date "$hist_dt" "$temp_dt"; then
stmt="UPDATE watch_history SET watch_date = '$temp_dt' WHERE anime_name = '$1' AND episode_number = $2;" lg "Passed in date is older or same than current date... doing nothing"
else return 1
lg "UPDATING WATCH_HISTORY: anime_name='$1', episode_number='$2' search_date='$datetime'" fi
stmt="UPDATE watch_history SET watch_date = '$datetime' WHERE anime_name = '$1' AND episode_number = $2;" lg "UPDATING watch_history from sync. watch_date -> $temp_dt"
fi 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? wait # in case there's another insert/update still running in background?
run_stmt "$stmt" run_stmt "$stmt"
} }
@ -274,7 +285,6 @@ sync_watch_history() {
errs=0 errs=0
while read -r line; do while read -r line; do
anime_name="${line/ //}" anime_name="${line/ //}"
# for each episode of $anime_name on the remote machine, check local
while read -r ep; do while read -r ep; do
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")
@ -370,7 +380,6 @@ find_media() {
fi fi
get_directory_data "$inp" get_directory_data "$inp"
[ -z "$inp" ] && return 1
selection="$(rofi -dpi "$DPI" -dmenu -only-match -async-pre-read 33 -config "$ROFI_CFG" \ 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" \ -l 15 -i -sep '|' -mesg "$(generate_span "Current directory: $inp")" -a "$watched" \
-p "Enter selection" <<< "$inputlist")" -p "Enter selection" <<< "$inputlist")"
@ -408,12 +417,9 @@ find_media() {
## main code ## ## main code ##
##################### #####################
# get query
get_search_query() { get_search_query() {
# Get search history # Get search history
stmt="SELECT DISTINCT id || '. ' || anime_name \ stmt="SELECT DISTINCT id || '. ' || anime_name FROM search_history ORDER BY search_date DESC;"
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" 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 if [ "$IS_ROFI" -eq 1 ]; then
@ -450,9 +456,7 @@ search_anime() {
} }
search_eps() { search_eps() {
# get available episodes for anime_id
anime_id=$1 anime_id=$1
curl -s "$BASE_URL/category/$anime_id" | curl -s "$BASE_URL/category/$anime_id" |
sed -n -E ' sed -n -E '
/^[[:space:]]*<a href="#" class="active" ep_start/{ /^[[:space:]]*<a href="#" class="active" ep_start/{
@ -475,7 +479,7 @@ anime_selection() {
menu+="$((cnt + 1)). $anime_id\n" menu+="$((cnt + 1)). $anime_id\n"
res["$cnt"]="$anime_id" res["$cnt"]="$anime_id"
lg "ANIME: $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" lg "$anime_id HAS BEEN SEARCHED BEFORE"
[ -z "$searched" ] && searched="$cnt" || searched="$searched, $cnt" [ -z "$searched" ] && searched="$cnt" || searched="$searched, $cnt"
fi fi
@ -558,9 +562,7 @@ episode_selection() {
if [ "$IS_ROFI" -eq 1 ]; then if [ "$IS_ROFI" -eq 1 ]; then
# select episode number for anime # select episode number for anime
lg "Anime ID: $anime_id" lg "Anime ID: $anime_id"
stmt="SELECT DISTINCT episode_number \ stmt="SELECT DISTINCT episode_number FROM watch_history WHERE anime_name = '$anime_id';"
FROM watch_history \
WHERE anime_name = '$anime_id';"
# Get Watch History for $anime_id as comma separated list # Get Watch History for $anime_id as comma separated list
watch_history="" watch_history=""
@ -684,7 +686,7 @@ stream() {
anime_id="${query// /}" anime_id="${query// /}"
[ -z "$anime_id" ] && die "No anime selected or queried" [ -z "$anime_id" ] && die "No anime selected or queried"
lg "Checking if anime: $anime_id has been searched before..." lg "Checking if anime: $anime_id has been searched before..."
check_db "$anime_id" "search" check_db "search" "$anime_id"
searched="$?" searched="$?"
lg "Searched before: $searched" lg "Searched before: $searched"
if [ "$searched" -eq 0 ]; then if [ "$searched" -eq 0 ]; then
@ -834,11 +836,7 @@ main() {
fi fi
lg "SELECTION: $selection_id" lg "SELECTION: $selection_id"
stmt="SELECT episode_number \ stmt="SELECT episode_number FROM watch_history WHERE anime_name = '$selection_id' ORDER BY watch_date DESC LIMIT 1;"
FROM watch_history \
WHERE anime_name = '$selection_id' \
ORDER BY watch_date DESC \
LIMIT 1"
ep_choice_start=$(run_stmt "$stmt") ep_choice_start=$(run_stmt "$stmt")
lg "Most recently watched episode: $ep_choice_start" lg "Most recently watched episode: $ep_choice_start"
;; ;;