update logging in search_history function

This commit is contained in:
ksyasuda 2021-11-21 21:43:48 -08:00
parent 7ee540601e
commit 8a531d1818

327
ani-cli
View File

@ -197,7 +197,7 @@ sync_search_history() {
res=$(sqlite3 -noheader "$HISTORY_DB" <<<"SELECT anime_name FROM search_history WHERE anime_name = '$anime_name'") res=$(sqlite3 -noheader "$HISTORY_DB" <<<"SELECT anime_name FROM search_history WHERE anime_name = '$anime_name'")
if [[ "${res/ //}" == "" ]]; then if [[ "${res/ //}" == "" ]]; then
search_date=$(awk -F '|' '{print $3}' <<<"$line") search_date=$(awk -F '|' '{print $3}' <<<"$line")
printf "%s\n" "Adding ($anime_name|$search_date) to watch history..." log "Adding ($anime_name|$search_date) to search history..."
sqlite3 "$HISTORY_DB" <<<"INSERT INTO search_history(anime_name, search_date) VALUES('$anime_name', '$search_date')" sqlite3 "$HISTORY_DB" <<<"INSERT INTO search_history(anime_name, search_date) VALUES('$anime_name', '$search_date')"
if [[ "$?" -ne 0 ]]; then if [[ "$?" -ne 0 ]]; then
err "Error inserting row $line" err "Error inserting row $line"
@ -205,7 +205,7 @@ sync_search_history() {
((++cnt)) ((++cnt))
fi fi
done <<<"$(sqlite3 -noheader "$temp_db" <<<"SELECT DISTINCT * FROM search_history")" done <<<"$(sqlite3 -noheader "$temp_db" <<<"SELECT DISTINCT * FROM search_history")"
printf "%s\n" "Inserted $cnt rows into search_history table" log "Inserted $cnt rows into search_history table"
} }
sync_watch_history() { sync_watch_history() {
@ -235,21 +235,6 @@ sync_watch_history() {
log "Episode: $episode_num found in the db... skipping" log "Episode: $episode_num found in the db... skipping"
fi fi
done <<<"${episodes[@]}" 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")" done <<<"$(sqlite3 -noheader "$temp_db" <<<"SELECT DISTINCT anime_name FROM watch_history")"
log "Inserted $cnt rows into watch_history table" log "Inserted $cnt rows into watch_history table"
} }
@ -473,17 +458,17 @@ open_episode() {
video_url=$(get_links "$dpage_url") video_url=$(get_links "$dpage_url")
case $video_url in case $video_url in
*streamtape*) *streamtape*)
# If direct download not available then scrape streamtape.com # If direct download not available then scrape streamtape.com
BROWSER=${BROWSER:-firefox} BROWSER=${BROWSER:-firefox}
printf "scraping streamtape.com\n" printf "scraping streamtape.com\n"
video_url=$(curl -s "$video_url" | sed -n -E ' video_url=$(curl -s "$video_url" | sed -n -E '
/^<script>document/{ /^<script>document/{
s/^[^"]*"([^"]*)" \+ '\''([^'\'']*).*/https:\1\2\&dl=1/p s/^[^"]*"([^"]*)" \+ '\''([^'\'']*).*/https:\1\2\&dl=1/p
q q
} }
') ')
;; ;;
esac esac
if [ $is_download -eq 0 ]; then if [ $is_download -eq 0 ]; then
@ -529,52 +514,52 @@ playlist_remove=0
playlist_add=0 playlist_add=0
while getopts 'hd:Hlpa:P:sv' OPT; do while getopts 'hd:Hlpa:P:sv' OPT; do
case "$OPT" in case "$OPT" in
h) h)
help_text help_text
exit 0 exit 0
;; ;;
d) d)
is_download=1 is_download=1
download_dir="$OPTARG" download_dir="$OPTARG"
if [ "$VERBOSE" -eq 1 ]; then if [ "$VERBOSE" -eq 1 ]; then
printf "%s\n" "DOWNLOAD DIR: $download_dir" printf "%s\n" "DOWNLOAD DIR: $download_dir"
fi fi
;; ;;
H) H)
scrape=history scrape=history
;; ;;
l) l)
list_history=1 list_history=1
;; ;;
p) p)
scrape=playlist scrape=playlist
is_playlist=1 is_playlist=1
;; ;;
a) a)
is_add=1 is_add=1
scrape=add scrape=add
playlist_file="${OPTARG/ //}" playlist_file="${OPTARG/ //}"
;; ;;
P) P)
is_playlist=1 is_playlist=1
# 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"
log "$playlist_file" log "$playlist_file"
$player_fn "$playlist_file" $player_fn "$playlist_file"
exit 0 exit 0
;; ;;
s) s)
scrape=sync scrape=sync
;; ;;
v) v)
VERBOSE=1 VERBOSE=1
;; ;;
*) *)
printf "%s\n" "Invalid option" printf "%s\n" "Invalid option"
exit 1 exit 1
;; ;;
esac esac
done done
shift $((OPTIND - 1)) shift $((OPTIND - 1))
@ -595,102 +580,102 @@ if [[ "$list_history" -eq 1 ]]; then
fi fi
case $scrape in case $scrape in
query) query)
get_search_query "$*"
search_results=$(search_anime "$query")
[ -z "$search_results" ] && die "No search results found"
anime_selection "$search_results"
[ $? -ne 0 ] && die "No anime selection found"
episode_selection
;;
history)
stmt="SELECT DISTINCT anime_name FROM watch_history ORDER BY watch_date DESC"
search_results=$(printf "%s\n" "$stmt" | sqlite3 -noheader "$HISTORY_DB")
[ -z "$search_results" ] && die "History is empty"
anime_selection "${search_results[@]}"
[ $? -ne 0 ] && die "No anime selection found"
log "SELECTION: $selection_id"
stmt="SELECT episode_number \
FROM watch_history \
WHERE anime_name = '$selection_id' \
ORDER BY watch_date DESC \
LIMIT 1"
ep_choice_start=$(run_stmt "$stmt")
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")
;;
playlist)
lines=$(cat "$playlist_file" | wc -l)
[ "$VERBOSE" -eq 1 ] && printf "%s%d\n" "Num lines in playlist: " "$lines"
if [[ "$lines" -eq 0 ]]; then
get_search_query "$*" get_search_query "$*"
search_results=$(search_anime "$query") search_results=$(search_anime "$query")
[ -z "$search_results" ] && die "No search results found" [ -z "$search_results" ] && die "No search results found"
anime_selection "$search_results" anime_selection "$search_results"
[ $? -ne 0 ] && die "No anime selection found" [ $? -ne 0 ] && die "No anime selection found"
episode_selection episode_selection
else ;;
line=($(sed '1q;d' "$playlist_file")) history)
if [[ "${#line[@]}" -ne 2 ]]; then stmt="SELECT DISTINCT anime_name FROM watch_history ORDER BY watch_date DESC"
die "Something went wrong with the playlist file... exiting" search_results=$(printf "%s\n" "$stmt" | sqlite3 -noheader "$HISTORY_DB")
[ -z "$search_results" ] && die "History is empty"
anime_selection "${search_results[@]}"
[ $? -ne 0 ] && die "No anime selection found"
log "SELECTION: $selection_id"
stmt="SELECT episode_number \
FROM watch_history \
WHERE anime_name = '$selection_id' \
ORDER BY watch_date DESC \
LIMIT 1"
ep_choice_start=$(run_stmt "$stmt")
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")
;;
playlist)
lines=$(cat "$playlist_file" | wc -l)
[ "$VERBOSE" -eq 1 ] && printf "%s%d\n" "Num lines in playlist: " "$lines"
if [[ "$lines" -eq 0 ]]; then
get_search_query "$*"
search_results=$(search_anime "$query")
[ -z "$search_results" ] && die "No search results found"
anime_selection "$search_results"
[ $? -ne 0 ] && die "No anime selection found"
episode_selection
else
line=($(sed '1q;d' "$playlist_file"))
if [[ "${#line[@]}" -ne 2 ]]; then
die "Something went wrong with the playlist file... exiting"
fi
selection_id="${line[0]}"
episodes=($selection_id)
ep_choice_start="${line[1]}"
ep_choice_end=""
read last_ep_number <<-EOF
$(search_eps "$selection_id")
EOF
[ "$VERBOSE" -eq 1 ] && printf "Anime: %s Episode: %d\n" "$episodes" "$ep_choice_start"
[ "$VERBOSE" -eq 1 ] && printf "Episodes: %s\n" "${episodes[@]}"
fi fi
selection_id="${line[0]}" ;;
episodes=($selection_id) add)
ep_choice_start="${line[1]}" get_search_query "$*"
ep_choice_end="" search_results=$(search_anime "$query")
read last_ep_number <<-EOF [ -z "$search_results" ] && die "No search results found"
$(search_eps "$selection_id") anime_selection "$search_results"
EOF [ $? -ne 0 ] && die "No anime selection found"
[ "$VERBOSE" -eq 1 ] && printf "Anime: %s Episode: %d\n" "$episodes" "$ep_choice_start" episode_selection
[ "$VERBOSE" -eq 1 ] && printf "Episodes: %s\n" "${episodes[@]}" ;;
fi sync)
;; printf "%s" "Enter username for remote user: "
add) read -r username
get_search_query "$*" printf "%s" "Enter host for remote user: "
search_results=$(search_anime "$query") read -r host
[ -z "$search_results" ] && die "No search results found"
anime_selection "$search_results"
[ $? -ne 0 ] && die "No anime selection found"
episode_selection
;;
sync)
printf "%s" "Enter username for remote user: "
read -r username
printf "%s" "Enter host for remote user: "
read -r host
# printf "%s" "Enter connection string for remote user in the form user@host: " # printf "%s" "Enter connection string for remote user in the form user@host: "
# read -r connection_str # read -r connection_str
connection_str="$username@$host" connection_str="$username@$host"
printf "%s" "Enter port to connect to remote host with or leave blank for default (22): " printf "%s" "Enter port to connect to remote host with or leave blank for default (22): "
read -r port read -r port
if [[ "${port/ //}" == "" ]]; then if [[ "${port/ //}" == "" ]]; then
PORT=22 PORT=22
else else
PORT="$port" PORT="$port"
fi fi
printf "%s" "Enter path to private key (leave blank if unsure or not needed): " printf "%s" "Enter path to private key (leave blank if unsure or not needed): "
read -r key_path read -r key_path
printf "%s\n" "Syncing database with: $connection_str on port $PORT" printf "%s\n" "Syncing database with: $connection_str on port $PORT"
temp_db="/tmp/aniwrapper_tmp_history.sqlite3" temp_db="/tmp/aniwrapper_tmp_history.sqlite3"
if [[ -z "$key_path" ]]; then if [[ -z "$key_path" ]]; then
scp -P "$PORT" "$connection_str:$XDG_CONFIG_HOME/aniwrapper/history.sqlite3" "$temp_db" scp -P "$PORT" "$connection_str:$XDG_CONFIG_HOME/aniwrapper/history.sqlite3" "$temp_db"
else else
scp -P "$PORT" -i "$key_path" "$connection_str:$XDG_CONFIG_HOME/aniwrapper/history.sqlite3" "$temp_db" scp -P "$PORT" -i "$key_path" "$connection_str:$XDG_CONFIG_HOME/aniwrapper/history.sqlite3" "$temp_db"
fi fi
if [[ "$?" -ne 0 ]]; then if [[ "$?" -ne 0 ]]; then
printf "%s\n" "Error getting database file from remote host" printf "%s\n" "Error getting database file from remote host"
exit 1 exit 1
fi fi
sync_search_history && sync_watch_history sync_search_history && sync_watch_history
exit 0 exit 0
;; ;;
esac esac
{ # checking input { # checking input
@ -745,29 +730,29 @@ while :; do
printf "$c_reset" printf "$c_reset"
case $choice in case $choice in
n) n)
episode=$((episode + 1)) episode=$((episode + 1))
;; ;;
p) p)
episode=$((episode - 1)) episode=$((episode - 1))
;; ;;
s) s)
printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number
read episode read episode
printf "$c_reset" printf "$c_reset"
[ "$episode" -eq "$episode" ] 2>/dev/null || die "Invalid number entered" [ "$episode" -eq "$episode" ] 2>/dev/null || die "Invalid number entered"
;; ;;
r) ;; r) ;;
q) q)
break break
;; ;;
*) *)
die "invalid choice" die "invalid choice"
;; ;;
esac esac
open_episode "$selection_id" "$episode" "$download_dir" open_episode "$selection_id" "$episode" "$download_dir"
done done