mirror of
https://github.com/ksyasuda/aniwrapper.git
synced 2024-10-28 04:44:11 -07:00
make sync watch history a lot better
This commit is contained in:
parent
70c141cf8e
commit
b1b263fb56
81
ani-cli
81
ani-cli
@ -140,16 +140,6 @@ dep_ch() {
|
||||
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() {
|
||||
printf "%s\n" "$1" | sqlite3 -noheader "$HISTORY_DB"
|
||||
}
|
||||
@ -163,7 +153,7 @@ check_db() {
|
||||
# args:
|
||||
# $1: anime name: str
|
||||
# $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
|
||||
stmt="SELECT DISTINCT COUNT(*) \
|
||||
FROM file_history \
|
||||
@ -184,10 +174,21 @@ check_db() {
|
||||
AND episode_number = '$2';"
|
||||
fi
|
||||
res=$(run_stmt "$stmt")
|
||||
logger "check_db result: $res"
|
||||
logger "END check_db... Result -> $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
|
||||
update_date() {
|
||||
datetime=$(date +'%Y-%m-%d %H:%M:%S')
|
||||
@ -201,9 +202,19 @@ update_date() {
|
||||
stmt="UPDATE search_history SET search_date = '$datetime' \
|
||||
WHERE anime_name = '$1';"
|
||||
elif [[ $# -ge 3 ]]; then
|
||||
[ -z "$3" ] && return 1
|
||||
logger "UPDATING watch_history from sync. watch_date -> $3"
|
||||
stmt="UPDATE watch_history SET watch_date = '$3' \
|
||||
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// /:}"
|
||||
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' \
|
||||
AND episode_number = $2;"
|
||||
else
|
||||
@ -218,14 +229,9 @@ update_date() {
|
||||
insert_history() {
|
||||
# inserts into search/watch history db
|
||||
# check the anime_name/id
|
||||
logger "BEGIN: insert_history function"
|
||||
if [[ ! "$1" == "file" ]]; then
|
||||
if ! check_anime_name "$1"; then
|
||||
logger "ERROR: Anime name is none... exiting"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
logger "BEGIN: function insert_history()"
|
||||
datetime=$(date +'%Y-%m-%d %H:%M:%S')
|
||||
logger "Checking if row exists in db"
|
||||
check_db "$@"
|
||||
res="$?"
|
||||
if [[ $res -gt 0 ]]; then
|
||||
@ -237,9 +243,10 @@ insert_history() {
|
||||
logger "Already in watch db... Updating watch_date"
|
||||
fi
|
||||
update_date "$@"
|
||||
res=$?
|
||||
else
|
||||
logger "Row not found in DB... inserting"
|
||||
if [[ "$1" == "file" ]]; then
|
||||
logger "inserting $2 into file_history..."
|
||||
stmt="INSERT INTO file_history(directory, filename, watch_date) \
|
||||
VALUES('$2', '$3', '$datetime');"
|
||||
elif [[ "$2" == "search" ]]; then
|
||||
@ -250,13 +257,17 @@ insert_history() {
|
||||
watch_history(anime_name, episode_number, watch_date) \
|
||||
VALUES('$1', '$2', '$datetime');"
|
||||
fi
|
||||
logger "INSERT STATEMENT -> $stmt"
|
||||
run_stmt "$stmt"
|
||||
res=$?
|
||||
fi
|
||||
logger "END: insert_history function"
|
||||
logger "END: function insert_history()"
|
||||
return $res
|
||||
}
|
||||
|
||||
sync_search_history() {
|
||||
cnt=0
|
||||
errs=0
|
||||
while read -r line; do
|
||||
anime_name=$(awk -F '|' '{print $2}' <<< "$line")
|
||||
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")
|
||||
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
|
||||
err "Error inserting row $line"
|
||||
log "Error inserting row $line... skipping"
|
||||
((++errs))
|
||||
continue
|
||||
fi
|
||||
((++cnt))
|
||||
fi
|
||||
done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT * FROM search_history")"
|
||||
logger "Inserted $cnt rows into search_history table"
|
||||
logger "$errs errors on insert"
|
||||
}
|
||||
|
||||
sync_watch_history() {
|
||||
cnt=0
|
||||
errs=0
|
||||
while read -r line; do
|
||||
# anime_name=$(awk -F '|' '{print $2}' <<<"$line")
|
||||
anime_name="${line/ //}"
|
||||
@ -283,21 +298,17 @@ sync_watch_history() {
|
||||
while read -r ep; do
|
||||
# logger "ROW: $ep"
|
||||
episode_num=$(awk -F '|' '{print $1}' <<< "$ep")
|
||||
check_db "$anime_name" "$episode_num"
|
||||
num=$?
|
||||
logger "COUNT for $anime_name - episode $episode_num: $num"
|
||||
if [[ "$num" -eq 0 ]]; then
|
||||
logger "$anime_name - E$episode_num NOT IN DB"
|
||||
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"
|
||||
if ! insert_history "$anime_name" "$episode_num" "$watch_date"; then
|
||||
logger "Error inserting row ($anime_name|$episode_num|$watch_date)... skipping"
|
||||
((++errs))
|
||||
continue
|
||||
fi
|
||||
((++cnt))
|
||||
done <<< "${episodes[@]}"
|
||||
done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT anime_name FROM watch_history")"
|
||||
logger "Inserted $cnt rows into watch_history table"
|
||||
logger "$errs errors on insert"
|
||||
}
|
||||
|
||||
#####################
|
||||
|
Loading…
Reference in New Issue
Block a user