update code to insert into file history table

This commit is contained in:
ksyasuda 2022-01-04 23:06:12 -08:00
parent ae6e20ae1c
commit 7ca1907998
2 changed files with 39 additions and 16 deletions

46
ani-cli
View File

@ -177,27 +177,36 @@ 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
if [[ "$2" == "search" ]]; then log "check_db $*" 1> /dev/stderr
if [[ "$1" == "file" ]]; then
stmt="SELECT DISTINCT COUNT(*) \
FROM file_history \
WHERE directory = '$2' \
AND filename = '$3';"
elif [[ "$2" == "search" ]]; then
stmt="SELECT DISTINCT COUNT(*) \ stmt="SELECT DISTINCT COUNT(*) \
FROM search_history \ FROM search_history \
WHERE anime_name = '$1';" WHERE anime_name = '$1';"
res=$(run_stmt "$stmt")
return "$res"
else else
stmt="SELECT DISTINCT COUNT(*) \ stmt="SELECT DISTINCT COUNT(*) \
FROM watch_history \ FROM watch_history \
WHERE anime_name = '$1' \ WHERE anime_name = '$1' \
AND episode_number = '$2';" AND episode_number = '$2';"
res=$(run_stmt "$stmt")
return "$res"
fi fi
res=$(run_stmt "$stmt")
log "check_db result: $res"
return "$res"
} }
# 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')
stmt="" stmt=""
if [[ "$2" == "search" ]]; then if [[ "$1" == "file" ]]; then
log "UPDATING FILE_HISTORY: anime_name='$1', search_date='$datetime'"
stmt="UPDATE file_history SET watch_date = '$datetime' \
WHERE directory = '$2' and filename = '$3';"
elif [[ "$2" == "search" ]]; then
log "UPDATING SEARCH_HISTORY: anime_name='$1', search_date='$datetime'" log "UPDATING SEARCH_HISTORY: anime_name='$1', search_date='$datetime'"
stmt="UPDATE search_history SET search_date = '$datetime' \ stmt="UPDATE search_history SET search_date = '$datetime' \
WHERE anime_name = '$1';" WHERE anime_name = '$1';"
@ -213,32 +222,41 @@ 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
log "BEGIN: insert_history function"
if [[ ! "$1" == "file" ]]; then
if ! check_anime_name "$1"; then if ! check_anime_name "$1"; then
log "ERROR: Anime name is none... exiting" log "ERROR: Anime name is none... exiting"
return 1 return 1
fi fi
fi
datetime=$(date +'%Y-%m-%d %H:%M:%S') datetime=$(date +'%Y-%m-%d %H:%M:%S')
check_db "$@" check_db "$@"
res="$?" res="$?"
if [[ "$res" -gt 0 ]]; then if [[ $res -gt 0 ]]; then
if [[ "$2" == "search" ]]; then if [[ "$1" == "file" ]]; then
log "Already in file history db... updaing watch_date"
elif [[ "$2" == "search" ]]; then
log "Already in search db... Updating search_date" log "Already in search db... Updating search_date"
else else
log "Already in watch db... Updating watch_date" log "Already in watch db... Updating watch_date"
fi fi
update_date "$@" update_date "$@"
else else
if [[ "$2" == "search" ]]; then if [[ "$1" == "file" ]]; then
log "inserting $2/$3 into file_history..."
stmt="INSERT INTO file_history(directory, filename, watch_date) \
VALUES('$2', '$3', '$datetime');"
elif [[ "$2" == "search" ]]; then
stmt="INSERT INTO search_history(anime_name, search_date) \ stmt="INSERT INTO search_history(anime_name, search_date) \
VALUES('$1', '$datetime');" VALUES('$1', '$datetime');"
run_stmt "$stmt"
else else
stmt="INSERT INTO \ stmt="INSERT INTO \
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
run_stmt "$stmt" run_stmt "$stmt"
fi fi
fi log "END: insert_history function"
} }
sync_search_history() { sync_search_history() {
@ -300,6 +318,9 @@ play_file() {
log "Checking if file is playable" log "Checking if file is playable"
if [[ "$1" =~ ($playable)$ ]]; then if [[ "$1" =~ ($playable)$ ]]; then
log "File is playable..." log "File is playable..."
filename=$(grep -oE '[^/]*$' <<< "$1")
log "FILENAME: $filename"
insert_history "file" "$1" "$filename"
if [[ "$1" =~ .mp3 ]]; then if [[ "$1" =~ .mp3 ]]; then
log ".mp3 file found... playing without video" log ".mp3 file found... playing without video"
log "MPV COMMAND: $PLAYER_CMD --no-video $1" log "MPV COMMAND: $PLAYER_CMD --no-video $1"
@ -796,7 +817,7 @@ case $scrape in
die "Error getting database file from remote host" die "Error getting database file from remote host"
fi fi
sync_search_history && sync_watch_history sync_search_history && sync_watch_history
exit 0 exit $?
;; ;;
file) file)
log "STARTING DIR: $play_dir" log "STARTING DIR: $play_dir"
@ -807,6 +828,7 @@ case $scrape in
die "Something went wrong getting path... path is empty" die "Something went wrong getting path... path is empty"
fi fi
play_file "$video_path" play_file "$video_path"
exit $?
;; ;;
esac esac

View File

@ -1,5 +1,6 @@
CREATE TABLE file_history ( CREATE TABLE file_history (
id integer PRIMARY KEY AUTOINCREMENT, id integer PRIMARY KEY AUTOINCREMENT,
directory varchar(200) NOT NULL, directory varchar(200) NOT NULL,
filename varchar(200) NOT NULL filename varchar(200) NOT NULL,
watch_date datetime NOT NULL
); );