# runs sql command on the history database run_stmt() { printf "%s\n" "$1" | sqlite3 -noheader -list "$HISTORY_DB" } # Return number of matches for anime/episode in db check_db() { case "$1" in directory) stmt="SELECT COUNT(*) FROM file_history WHERE directory = '$2';" ;; file) stmt="SELECT COUNT(*) FROM file_history WHERE directory = '$2' AND filename = '$3';" ;; search) stmt="SELECT COUNT(*) FROM search_history WHERE anime_name = '$2';" ;; watch | sync) stmt="SELECT COUNT(*) FROM watch_history WHERE anime_name = '$2' AND episode_number = '$3';" ;; anime) stmt="SELECT COUNT(*) FROM anime WHERE anime_name = '$2';" ;; esac res=$(run_stmt "$stmt") return "$res" } # return true (0) if $source_dt > $target_dt check_date() { source_dt="$1" target_dt="$2" if [[ "$source_dt" < "$target_dt" ]] || [[ "$source_dt" == "$target_dt" ]]; then return 1 else return 0 fi } # updates search/watch date for passed in anime update_date() { datetime=$(date +'%Y-%m-%d %H:%M:%S') stmt="" case "$1" in directory) stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = 'DIRECTORY';" ;; file) stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';" ;; search) 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// /:}" if ! check_date "$hist_dt" "$temp_dt"; then lg "Passed in date is older or same than current date... doing nothing" return 1 fi stmt="UPDATE watch_history SET watch_date = '$temp_dt' WHERE anime_name = '$2' AND episode_number = $3;" ;; watch) stmt="UPDATE watch_history SET watch_date = '$datetime' WHERE anime_name = '$2' AND episode_number = $3;" ;; anime) return ;; esac lg "UPDATE STMT -> $stmt" run_stmt "$stmt" } # inserts into search/watch history db # check the anime_name/id insert_history() { datetime=$(date +'%Y-%m-%d %H:%M:%S') lg "Checking if ($*) exists in db" if ! check_db "$@"; then lg "Match found... Updating row in history db..." update_date "$@" res=$? else lg "Row not found in DB... inserting" case "$1" in directory) stmt="INSERT INTO file_history(directory, filename, watch_date) VALUES('$2', 'DIRECTORY', '$datetime');" ;; file) stmt="INSERT INTO file_history(directory, filename, watch_date) VALUES('$2', '$3', '$datetime');" ;; search) stmt="INSERT INTO search_history(anime_name, search_date) VALUES('$2', '$datetime');" ;; watch) stmt="INSERT INTO watch_history(anime_name, episode_number, watch_date) VALUES('$2', '$3', '$datetime');" ;; sync) stmt="INSERT INTO watch_history(anime_name, episode_number, watch_date) VALUES('$2', '$3', '$4');" ;; anime) stmt="INSERT INTO anime(anime_name, start_episode, end_episode, data_date) VALUES('$2', $3, $4, '$datetime');" ;; esac lg "INSERT STATEMENT -> $stmt" run_stmt "$stmt" res=$? fi return $res } sync_search_history() { cnt=0 errs=0 while read -r line; do anime_name=$(awk -F '|' '{print $2}' <<< "$line") if sqlite3 -noheader "$HISTORY_DB" "SELECT COUNT(*) FROM search_history WHERE anime_name = '$anime_name'"; then search_date=$(awk -F '|' '{print $3}' <<< "$line") if ! sqlite3 "$HISTORY_DB" "INSERT INTO search_history(anime_name, search_date) VALUES('$anime_name', '$search_date')"; then ((++errs)) continue fi ((++cnt)) fi done < <(sqlite3 -list -noheader "$temp_db" "SELECT * FROM search_history") lg "$cnt rows inserted into search_history table" lg "$errs errors on insert" } sync_watch_history() { cnt=0 errs=0 while read -r line; do anime_name="${line/ //}" while read -r ep; do episode_num=$(awk -F '|' '{print $1}' <<< "$ep") watch_date=$(awk -F '|' '{print $NF}' <<< "$ep") if ! insert_history "sync" "$anime_name" "$episode_num" "$watch_date"; then ((++errs)) continue fi ((++cnt)) done < <(sqlite3 -list -noheader "$temp_db" "SELECT episode_number, watch_date FROM watch_history WHERE anime_name = '$anime_name'") done < <(sqlite3 -list -noheader "$temp_db" "SELECT anime_name FROM watch_history") lg "$cnt rows inserted into watch_history table" lg "$errs rows skipped on insert" } # vim: ft=sh