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

95
ani-cli
View File

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

View File

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