make sync watch history a lot better

This commit is contained in:
ksyasuda 2022-01-07 10:50:52 -08:00
parent 70c141cf8e
commit b1b263fb56

83
ani-cli
View File

@ -140,16 +140,6 @@ dep_ch() {
done done
} }
check_anime_name() {
# Check to make sure passed in name is not empty
logger "VAR: $1"
if [[ "$1" == "" ]] || [[ "$1" == " " ]] || [[ "$1" == "\n" ]]; then
logger "Passed in name is nothing"
return 1
fi
return 0
}
run_stmt() { run_stmt() {
printf "%s\n" "$1" | sqlite3 -noheader "$HISTORY_DB" printf "%s\n" "$1" | sqlite3 -noheader "$HISTORY_DB"
} }
@ -163,7 +153,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 "check_db $*" 1> /dev/stderr logger "BEGIN check_db()" 1> /dev/stderr
if [[ "$1" == "directory" ]]; then if [[ "$1" == "directory" ]]; then
stmt="SELECT DISTINCT COUNT(*) \ stmt="SELECT DISTINCT COUNT(*) \
FROM file_history \ FROM file_history \
@ -184,10 +174,21 @@ check_db() {
AND episode_number = '$2';" AND episode_number = '$2';"
fi fi
res=$(run_stmt "$stmt") res=$(run_stmt "$stmt")
logger "check_db result: $res" logger "END check_db... Result -> $res"
return "$res" return "$res"
} }
# return true (0) if $source_dt > $target_dt
check_date() {
source_dt="$1"
target_dt="$2"
if [[ "$i" < "$j" ]] || [[ "$i" == "$j" ]]; then
return 1
else
return 0
fi
}
# updates search/watch date for passed in anime # updates search/watch date for passed in anime
update_date() { update_date() {
datetime=$(date +'%Y-%m-%d %H:%M:%S') datetime=$(date +'%Y-%m-%d %H:%M:%S')
@ -201,9 +202,19 @@ update_date() {
stmt="UPDATE search_history SET search_date = '$datetime' \ stmt="UPDATE search_history SET search_date = '$datetime' \
WHERE anime_name = '$1';" WHERE anime_name = '$1';"
elif [[ $# -ge 3 ]]; then elif [[ $# -ge 3 ]]; then
[ -z "$3" ] && return 1 temp_dt="${3// /:}"
logger "UPDATING watch_history from sync. watch_date -> $3" [ -z "$temp_dt" ] && return 1
stmt="UPDATE watch_history SET watch_date = '$3' \ hist_dt=$(run_stmt "SELECT watch_date FROM watch_history WHERE anime_name='$1' AND episode_number='$2';")
hist_dt="${hist_dt// /:}"
logger "PASSED IN DATE: $temp_dt"
logger "DB DATE: $hist_dt"
logger "Checking if update is needed..."
if ! check_date "$hist_dt" "$temp_dt"; then
logger "Passed in date is older or same than current date... doing nothing"
return 1
fi
logger "UPDATING watch_history from sync. watch_date -> $temp_dt"
stmt="UPDATE watch_history SET watch_date = '$temp_dt' \
WHERE anime_name = '$1' \ WHERE anime_name = '$1' \
AND episode_number = $2;" AND episode_number = $2;"
else else
@ -218,14 +229,9 @@ 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: insert_history function" logger "BEGIN: function insert_history()"
if [[ ! "$1" == "file" ]]; then
if ! check_anime_name "$1"; then
logger "ERROR: Anime name is none... exiting"
return 1
fi
fi
datetime=$(date +'%Y-%m-%d %H:%M:%S') datetime=$(date +'%Y-%m-%d %H:%M:%S')
logger "Checking if row exists in db"
check_db "$@" check_db "$@"
res="$?" res="$?"
if [[ $res -gt 0 ]]; then if [[ $res -gt 0 ]]; then
@ -237,9 +243,10 @@ insert_history() {
logger "Already in watch db... Updating watch_date" logger "Already in watch db... Updating watch_date"
fi fi
update_date "$@" update_date "$@"
res=$?
else else
logger "Row not found in DB... inserting"
if [[ "$1" == "file" ]]; then if [[ "$1" == "file" ]]; then
logger "inserting $2 into file_history..."
stmt="INSERT INTO file_history(directory, filename, watch_date) \ stmt="INSERT INTO file_history(directory, filename, watch_date) \
VALUES('$2', '$3', '$datetime');" VALUES('$2', '$3', '$datetime');"
elif [[ "$2" == "search" ]]; then elif [[ "$2" == "search" ]]; then
@ -250,13 +257,17 @@ insert_history() {
watch_history(anime_name, episode_number, watch_date) \ watch_history(anime_name, episode_number, watch_date) \
VALUES('$1', '$2', '$datetime');" VALUES('$1', '$2', '$datetime');"
fi fi
logger "INSERT STATEMENT -> $stmt"
run_stmt "$stmt" run_stmt "$stmt"
res=$?
fi fi
logger "END: insert_history function" logger "END: function insert_history()"
return $res
} }
sync_search_history() { sync_search_history() {
cnt=0 cnt=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")
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'")
@ -264,16 +275,20 @@ sync_search_history() {
search_date=$(awk -F '|' '{print $3}' <<< "$line") search_date=$(awk -F '|' '{print $3}' <<< "$line")
logger "Adding ($anime_name|$search_date) to search history..." logger "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
err "Error inserting row $line" log "Error inserting row $line... skipping"
((++errs))
continue
fi fi
((++cnt)) ((++cnt))
fi fi
done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT * FROM search_history")" done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT * FROM search_history")"
logger "Inserted $cnt rows into search_history table" logger "Inserted $cnt rows into search_history table"
logger "$errs errors on insert"
} }
sync_watch_history() { sync_watch_history() {
cnt=0 cnt=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")
anime_name="${line/ //}" anime_name="${line/ //}"
@ -283,21 +298,17 @@ sync_watch_history() {
while read -r ep; do while read -r ep; do
# logger "ROW: $ep" # logger "ROW: $ep"
episode_num=$(awk -F '|' '{print $1}' <<< "$ep") episode_num=$(awk -F '|' '{print $1}' <<< "$ep")
check_db "$anime_name" "$episode_num" watch_date=$(awk -F '|' '{print $NF}' <<< "$ep")
num=$? if ! insert_history "$anime_name" "$episode_num" "$watch_date"; then
logger "COUNT for $anime_name - episode $episode_num: $num" logger "Error inserting row ($anime_name|$episode_num|$watch_date)... skipping"
if [[ "$num" -eq 0 ]]; then ((++errs))
logger "$anime_name - E$episode_num NOT IN DB" continue
watch_date=$(awk -F '|' '{print $NF}' <<< "$ep")
logger "Adding ($anime_name|$episode_num|$watch_date) to watch history..."
insert_history "$anime_name" "$episode_num" "$watch_date"
((++cnt))
else
logger "$anime_name - Episode: $episode_num found in the db... skipping"
fi fi
((++cnt))
done <<< "${episodes[@]}" done <<< "${episodes[@]}"
done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT anime_name FROM watch_history")" done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT anime_name FROM watch_history")"
logger "Inserted $cnt rows into watch_history table" logger "Inserted $cnt rows into watch_history table"
logger "$errs errors on insert"
} }
##################### #####################