update sync history function

This commit is contained in:
ksyasuda 2021-11-18 15:52:37 -08:00
parent c5a1919dc3
commit a2eedef977
2 changed files with 90 additions and 63 deletions

87
ani-cli
View File

@ -48,6 +48,10 @@ err() {
printf "$c_red%s$c_reset\n" "$*" >&2 printf "$c_red%s$c_reset\n" "$*" >&2
} }
log() {
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "$*" >&2
}
search_anime() { search_anime() {
# get anime name along with its id # get anime name along with its id
search=$(printf '%s' "$1" | tr ' ' '-') search=$(printf '%s' "$1" | tr ' ' '-')
@ -105,10 +109,9 @@ dep_ch() {
check_anime_name() { check_anime_name() {
# Maybe change to query the db for a similar name # Maybe change to query the db for a similar name
# Check to make sure passed in name is not empty # Check to make sure passed in name is not empty
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "VAR: $1" log "VAR: $1"
if [[ "$1" == "" ]] || [[ "$1" == " " ]] || [[ "$1" == "\n" ]]; then if [[ "$1" == "" ]] || [[ "$1" == " " ]] || [[ "$1" == "\n" ]]; then
[ "$VERBOSE" -eq 1 ] && log "Passed in name is nothing"
printf "%s\n" "Passed in name is nothing"
return 1 return 1
fi fi
return 0 return 0
@ -161,19 +164,16 @@ insert_history() {
# check the anime_name/id # check the anime_name/id
check_anime_name "$1" check_anime_name "$1"
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
[ "$VERBOSE" -eq 1 ] && log "ERROR: Anime name is none... exiting"
printf "%s\n" "ERROR: Anime name is none... exiting"
return 1 return 1
fi fi
datetime=$(date +'%Y-%m-%d %H:%M:%S') datetime=$(date +'%Y-%m-%d %H:%M:%S')
check_db "$@" check_db "$@"
if [[ $? -gt 0 ]]; then if [[ $? -gt 0 ]]; then
if [[ "$2" == "search" ]]; then if [[ "$2" == "search" ]]; then
[ "$VERBOSE" -eq 1 ] && log "Already in search db... Updating search_date"
printf "%s\n" "Already in search db... Updating search_date"
else else
[ "$VERBOSE" -eq 1 ] && log "Already in search db... Updating watch_date"
printf "%s\n" "Already in search db... Updating watch_date"
fi fi
update_date "$@" update_date "$@"
else else
@ -211,20 +211,47 @@ sync_search_history() {
sync_watch_history() { sync_watch_history() {
cnt=0 cnt=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 anime_name FROM watch_history WHERE anime_name = '$anime_name'") anime_name="${line/ //}"
if [[ "${res/ //}" == "" ]]; then log "ANIME: $anime_name"
episode_num=$(awk -F '|' '{print $3}' <<<"$line") episodes=$(sqlite3 -noheader "$temp_db" <<<"SELECT episode_number, watch_date FROM watch_history WHERE anime_name = '$anime_name'")
watch_date=$(awk -F '|' '{print $NF}' <<<"$line") # for each episode of $anime_name on the remote machine, check local
printf "%s\n" "Adding ($anime_name|$episode_num|$watch_date) to watch history..." while read -r ep; do
episode_num=$(awk -F '|' '{print $1}' <<<"$ep")
# TODO: Fix inserting duplicate rows
run_stmt "SELECT COUNT(*) FROM watch_history WHERE anime_name = '$anime_name' AND episode_number = $episode_num"
num=$?
log "EP: $ep"
if [[ "$num" -eq 0 ]]; then
log "NOT IN DB"
watch_date=$(awk -F '|' '{print $NF}' <<<"$ep")
log "Adding ($anime_name|$episode_num|$watch_date) to watch history..."
sqlite3 "$HISTORY_DB" <<<"INSERT INTO watch_history(anime_name, episode_number, watch_date) VALUES('$anime_name', '$episode_num', '$watch_date')" sqlite3 "$HISTORY_DB" <<<"INSERT INTO watch_history(anime_name, episode_number, watch_date) VALUES('$anime_name', '$episode_num', '$watch_date')"
if [[ "$?" -ne 0 ]]; then if [[ "$?" -ne 0 ]]; then
err "Error inserting row $line" err "Error inserting row $ep"
fi fi
((++cnt)) ((++cnt))
else
log "Episode: $episode_num found in the db... skipping"
fi fi
done <<<"$(sqlite3 -noheader "$temp_db" <<<"SELECT DISTINCT * FROM watch_history")" done <<<"${episodes[@]}"
printf "%s\n" "Inserted $cnt rows into watch_history table" # for episode in "${eps[@]}"; do
# printf "\t%s\n" "ep: $episode"
# done
# echo "EPISODES: ${episodes[*]}"
# res=$(sqlite3 -noheader "$HISTORY_DB" <<<"SELECT anime_name FROM watch_history WHERE anime_name = '$anime_name'")
# if [[ "${res/ //}" == "" ]]; then
# episode_num=$(awk -F '|' '{print $3}' <<<"$line")
# watch_date=$(awk -F '|' '{print $NF}' <<<"$line")
# printf "%s\n" "Adding ($anime_name|$episode_num|$watch_date) to watch history..."
# sqlite3 "$HISTORY_DB" <<<"INSERT INTO watch_history(anime_name, episode_number, watch_date) VALUES('$anime_name', '$episode_num', '$watch_date')"
# if [[ "$?" -ne 0 ]]; then
# err "Error inserting row $line"
# fi
# ((++cnt))
# fi
done <<<"$(sqlite3 -noheader "$temp_db" <<<"SELECT DISTINCT anime_name FROM watch_history")"
log "Inserted $cnt rows into watch_history table"
} }
##################### #####################
@ -252,7 +279,7 @@ get_search_query() {
# Strip the list entry number from string # Strip the list entry number from string
query="$(awk '{print $NF}' <<<${query//[1-9]+\. /})" query="$(awk '{print $NF}' <<<${query//[1-9]+\. /})"
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "Query: $query" log "Query: $query"
else else
query=$* query=$*
fi fi
@ -294,10 +321,10 @@ anime_selection() {
cnt=0 cnt=0
# Get the comma separated list of indexes of anime that has been searched before # Get the comma separated list of indexes of anime that has been searched before
for anime in "${res[@]}"; do for anime in "${res[@]}"; do
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "ANIME: $anime" log "ANIME: $anime"
check_db "$anime" "search" check_db "$anime" "search"
if [[ $? -gt 0 ]]; then if [[ $? -gt 0 ]]; then
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "SEARCHED BEFORE" log "SEARCHED BEFORE"
if [[ "$searched" == "" ]]; then if [[ "$searched" == "" ]]; then
searched="$((cnt++))" searched="$((cnt++))"
else else
@ -306,7 +333,7 @@ anime_selection() {
fi fi
done done
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "SEARCHED: $searched" log "SEARCHED: $searched"
# get the anime from indexed list # get the anime from indexed list
user_input=$(printf "${menu[@]}" | user_input=$(printf "${menu[@]}" |
@ -363,7 +390,7 @@ episode_selection() {
# select episode number for anime # select episode number for anime
[ "$is_download" -eq 1 ] && [ "$is_download" -eq 1 ] &&
printf "Range of episodes can be specified: start_number end_number\n" printf "Range of episodes can be specified: start_number end_number\n"
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "Anime ID: $anime_id" log "Anime ID: $anime_id"
stmt="SELECT DISTINCT episode_number \ stmt="SELECT DISTINCT episode_number \
FROM watch_history \ FROM watch_history \
WHERE anime_name = '$anime_id';" WHERE anime_name = '$anime_id';"
@ -382,7 +409,7 @@ episode_selection() {
fi fi
done done
[ "$VERBOSE" -eq 1 ] && printf "WATCH HISTORY: %s\n" "$watch_history" log "WATCH HISTORY: %s\n" "$watch_history"
# get user choice and set the start and end # get user choice and set the start and end
msg='<span foreground="peachpuff" style="italic" size="small" weight="light">Range of episodes can be provided as: START_EPISODE - END_EPISODE</span>' msg='<span foreground="peachpuff" style="italic" size="small" weight="light">Range of episodes can be provided as: START_EPISODE - END_EPISODE</span>'
choice=$( choice=$(
@ -395,7 +422,7 @@ episode_selection() {
) )
ep_choice_start=$(printf '%s\n' "${choice}" | awk '{print $1}') ep_choice_start=$(printf '%s\n' "${choice}" | awk '{print $1}')
ep_choice_end=$(printf '%s\n' "${choice}" | awk '{print $NF}') ep_choice_end=$(printf '%s\n' "${choice}" | awk '{print $NF}')
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "START: $ep_choice_start | END: $ep_choice_end" log "START: $ep_choice_start | END: $ep_choice_end"
if [[ -z "$ep_choice_start" ]] && [[ -z "$ep_choice_end" ]]; then if [[ -z "$ep_choice_start" ]] && [[ -z "$ep_choice_end" ]]; then
die "No episode range entered" die "No episode range entered"
fi fi
@ -436,7 +463,7 @@ open_episode() {
fi fi
fi fi
[ "$VERBOSE" -eq 1 ] && printf "Getting data for episode %d\n" $episode log "Getting data for episode %d\n" $episode
insert_history "$anime_id" "$episode" insert_history "$anime_id" "$episode"
@ -532,7 +559,7 @@ while getopts 'hd:Hlpa:P:sv' OPT; do
# remove spaces from $OPTARG # remove spaces from $OPTARG
playlist_file="${OPTARG/ //}" playlist_file="${OPTARG/ //}"
[ -z "$playlist_file" ] && die "Enter in path to playlist" [ -z "$playlist_file" ] && die "Enter in path to playlist"
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "$playlist_file" log "$playlist_file"
$player_fn "$playlist_file" $player_fn "$playlist_file"
exit 0 exit 0
;; ;;
@ -581,7 +608,7 @@ case $scrape in
[ -z "$search_results" ] && die "History is empty" [ -z "$search_results" ] && die "History is empty"
anime_selection "${search_results[@]}" anime_selection "${search_results[@]}"
[ $? -ne 0 ] && die "No anime selection found" [ $? -ne 0 ] && die "No anime selection found"
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "SELECTION: $selection_id" log "SELECTION: $selection_id"
stmt="SELECT episode_number \ stmt="SELECT episode_number \
FROM watch_history \ FROM watch_history \
@ -589,7 +616,7 @@ case $scrape in
ORDER BY watch_date DESC \ ORDER BY watch_date DESC \
LIMIT 1" LIMIT 1"
ep_choice_start=$(run_stmt "$stmt") ep_choice_start=$(run_stmt "$stmt")
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "Most recently watched episode: $ep_choice_start" log "Most recently watched episode: $ep_choice_start"
# search_results=$(sed -n -E 's/\t[0-9]*//p' "$logfile") # search_results=$(sed -n -E 's/\t[0-9]*//p' "$logfile")
# ep_choice_start=$(sed -n -E "s/${selection_id}\t//p" "$logfile") # ep_choice_start=$(sed -n -E "s/${selection_id}\t//p" "$logfile")
;; ;;
@ -686,7 +713,7 @@ for ep in $episodes; do
printf "%s\n" "EPISODES: $episodes" printf "%s\n" "EPISODES: $episodes"
fi fi
printf "%s\n" "$selection_id $ep" >>"$playlist_file" printf "%s\n" "$selection_id $ep" >>"$playlist_file"
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "Added to playlist file" log "Added to playlist file"
else else
open_episode "$selection_id" "$ep" "$download_dir" open_episode "$selection_id" "$ep" "$download_dir"
if [[ "$is_playlist" -eq 1 ]]; then if [[ "$is_playlist" -eq 1 ]]; then

View File

@ -171,21 +171,20 @@ case "$selection" in
[ "$VERBOSE" -eq 1 ] && log "playlist file: $PLAYLIST_FILE" [ "$VERBOSE" -eq 1 ] && log "playlist file: $PLAYLIST_FILE"
lines="" lines=""
cnt=0 cnt=0
while read -r line while read -r line; do
do
if [[ "$cnt" -eq 0 ]]; then if [[ "$cnt" -eq 0 ]]; then
lines="$((cnt + 1)). $line" lines="$((cnt + 1)). $line"
else else
lines="$lines|$((cnt + 1)). $line" lines="$lines|$((cnt + 1)). $line"
fi fi
((cnt++)) ((cnt++))
done < "$PLAYLIST_FILE" done <"$PLAYLIST_FILE"
choice=$(rofi -dmenu -l 12 \ choice=$(rofi -dmenu -l 12 \
-sep '|' \ -sep '|' \
-config "$CFG_DIR/$CFG_FILE" -p "Choose episode to delete:" \ -config "$CFG_DIR/$CFG_FILE" -p "Choose episode to delete:" \
<<< "${lines[*]}") <<<"${lines[*]}")
log "choice: $choice" log "choice: $choice"
selection=$(awk '{print $1}' <<< "$choice") selection=$(awk '{print $1}' <<<"$choice")
idx=${selection//\./} idx=${selection//\./}
log "index selected: $idx" log "index selected: $idx"
log "deleting entry: $idx" log "deleting entry: $idx"
@ -205,31 +204,32 @@ case "$selection" in
;; ;;
5.) 5.)
log "Sync history database" log "Sync history database"
username=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \ # username=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \
-l 1 -p "Enter the username of the remote user:") # -l 1 -p "Enter the username of the remote user:")
if [[ -z "$username" ]] || [[ "$username" == "" ]]; then # if [[ -z "$username" ]] || [[ "$username" == "" ]]; then
log "No username provided... exiting" # log "No username provided... exiting"
exit 1 # exit 1
fi # fi
host=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \ # host=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \
-l 1 -p "Enter the host for the remote machine (eg 192.168.1.99):") # -l 1 -p "Enter the host for the remote machine (eg 192.168.1.99):")
if [[ -z "$host" ]] || [[ "$host" == "" ]]; then # if [[ -z "$host" ]] || [[ "$host" == "" ]]; then
log "No host provided... exiting" # log "No host provided... exiting"
exit 1 # exit 1
fi # fi
port=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \ # port=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \
-l 1 -p "Enter in the ssh port for remote machine or leave blank for default [22]:") # -l 1 -p "Enter in the ssh port for remote machine or leave blank for default [22]:")
if [[ -z "$port" ]] || [[ "$port" == "" ]]; then # if [[ -z "$port" ]] || [[ "$port" == "" ]]; then
port=22 # port=22
fi # fi
keypath=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \ # keypath=$(rofi -dmenu -config "$CFG_DIR/$CFG_FILE" \
-l 1 -p "Enter path to private key (leave blank if not needed or if unsure):") # -l 1 -p "Enter path to private key (leave blank if not needed or if unsure):")
if [[ -z "$keypath" ]]; then printf "%s\n%s\n%d\n%s\n" "sudacode" "sudacode" 3005 "" | ani-cli -s
printf "%s\n%s\n%d\n%s\n" "$username" "$host" "$port" "" | ani-cli -s # if [[ -z "$keypath" ]]; then
else # printf "%s\n%s\n%d\n%s\n" "$username" "$host" "$port" "" | ani-cli -s
printf "%s\n%s\n%d\n%s\n" "$username" "$host" "$port" "$keypath" | ani-cli -s # else
fi # printf "%s\n%s\n%d\n%s\n" "$username" "$host" "$port" "$keypath" | ani-cli -s
# fi
if [[ "$?" -ne 0 ]]; then if [[ "$?" -ne 0 ]]; then
log "Aniwrapper was unable to sync the databases..." log "Aniwrapper was unable to sync the databases..."
exit 1 exit 1