From 92549567759895dd4389d857633dc9e5439f5eda Mon Sep 17 00:00:00 2001 From: ksyasuda Date: Fri, 29 Oct 2021 22:49:52 -0700 Subject: [PATCH] change most menus to rofi and add history db all menus (except the last one) use rofi menu additionally all anime's selected in a search or episodes selected to watch will be inserted into a local sqlite3 database [history.sqlite3] --- .gitignore | 3 + ani-cli | 277 +++++++++++++++++++++++------------ db.sh | 33 +++++ sql/anime_search_history.sql | 6 + sql/watch_history.sql | 7 + 5 files changed, 234 insertions(+), 92 deletions(-) create mode 100644 .gitignore create mode 100755 db.sh create mode 100644 sql/anime_search_history.sql create mode 100644 sql/watch_history.sql diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3d20ebf --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.sqlite3 +.git +.vscode diff --git a/ani-cli b/ani-cli index 5529971..e45d665 100755 --- a/ani-cli +++ b/ani-cli @@ -6,6 +6,7 @@ player_fn="mpv" prog="ani-cli" logfile="${XDG_CACHE_HOME:-$HOME/.cache}/ani-hsts" +history_db="history.sqlite3" c_red="\033[1;31m" c_green="\033[1;32m" @@ -15,47 +16,47 @@ c_magenta="\033[1;35m" c_cyan="\033[1;36m" c_reset="\033[0m" +rofi_cmd="rofi -config ~/SudacodeRice/rofi/rofidmenu.rasi -dmenu -l 10 -i -p 'Enter Choice:'" -help_text () { +help_text() { while IFS= read line; do printf "%s\n" "$line" done <<-EOF - USAGE: $prog - -h show this help text - -d download episode - -H continue where you left off + USAGE: $prog + -h show this help text + -d download episode + -H continue where you left off EOF } - -die () { +die() { printf "$c_red%s$c_reset\n" "$*" >&2 exit 1 } -err () { +err() { printf "$c_red%s$c_reset\n" "$*" >&2 } -search_anime () { +search_anime() { # get anime name along with its id - search=$(printf '%s' "$1" | tr ' ' '-') + search=$(printf '%s' "$1" | tr ' ' '-') titlepattern='/{ s/.*href="([^"]*)".*/\1/p q }' } -get_links () { +get_links() { dpage_url="$1" curl -s "$dpage_url" | - sed -n -E ' + sed -n -E ' /href="([^"]*)" download>Download/{ s/href="([^"]*)" download>Download/\1/p q }' | tr -d ' ' } -dep_ch () { +dep_ch() { for dep; do - if ! command -v "$dep" >/dev/null ; then + if ! command -v "$dep" >/dev/null; then die "Program \"$dep\" not found. Please install it." fi done } +check_db() { + echo "$1 $2" + if [[ "$2" == "search" ]]; then + stmt="SELECT DISTINCT COUNT(*) FROM search_history WHERE name = '$1'" + res=$(printf "%s\n" "$stmt" | sqlite3 "$history_db") + echo "$res" + return "$res" + else + stmt="SELECT DISTINCT COUNT(*) FROM watch_history WHERE anime_name = '$1' AND episode_number = $2" + res=$(printf "%s\n" "$stmt" | sqlite3 "$history_db") + echo "$res" + return "$res" + fi +} + +update_date() { + if [[ "$2" == "search" ]]; then + curdate=$(date +'%Y-%m-%d') + stmt="UPDATE search_history SET search_date = '$curdate' WHERE name = '$1'" && return 0 || return 1 + else + curdate=$(date +'%Y-%m-%d') + stmt="UPDATE watch_history SET watch_date = '$curdate' WHERE anime_name = '$1' AND episode_number = $2" && return 0 || return 1 + fi +} + +insert_history() { + curdate=$(date +'%Y-%m-%d') + check_db "$@" + num=$? + # echo "$num" + if [[ "$num" -gt 0 ]]; then + if [[ "$2" == "search" ]]; then + printf "%s\n" "Already in search db... Updating search_date" + else + printf "%s\n" "Already in search db... Updating watch_date" + fi + update_date "$@" + else + if [[ "$2" == "search" ]]; then + stmt="INSERT INTO search_history(name, search_date) VALUES('$1', '$curdate')" + printf "%s\n" "$stmt" | sqlite3 "$history_db" + else + stmt="INSERT INTO watch_history(anime_name, episode_number, watch_date) VALUES('$1', '$2', '$curdate')" + printf "%s\n" "$stmt" | sqlite3 "$history_db" + fi + fi +} + # get query -get_search_query () { +get_search_query() { + stmt="SELECT DISTINCT name FROM search_history" + cnt_stmt="SELECT DISTINCT COUNT(*) FROM search_history" + hist=$(echo "$stmt" | sqlite3 "$history_db") + cnt=$(printf "%s\n" "$cnt_stmt" | sqlite3 "$history_db") if [ -z "$*" ]; then - printf "Search Anime: " - read -r query + query=$(printf "%s\n" "${hist[@]}" | rofi -dmenu -l "$cnt" -p "Search Anime:" -config ~/SudacodeRice/rofi/rofidmenu.rasi) + # if [[ "$query" != "" ]]; then + # insert_history "$query" "search" + # else + # echo 'Query empty... Skipping insert' + # fi + # printf "Search Anime: " + # read -r query else query=$* fi } # create history file -[ -f "$logfile" ] || : > "$logfile" +[ -f "$logfile" ] || : >"$logfile" ##################### ## Anime selection ## ##################### -anime_selection () { +anime_selection() { search_results=$* menu_format_string='[%d] %s\n' menu_format_string_c1="$c_blue[$c_cyan%d$c_blue] $c_reset%s\n" menu_format_string_c2="$c_blue[$c_cyan%d$c_blue] $c_yellow%s$c_reset\n" count=1 + menu=() while read anime_id; do # alternating colors for menu [ $((count % 2)) -eq 0 ] && menu_format_string=$menu_format_string_c1 || menu_format_string=$menu_format_string_c2 - printf "$menu_format_string" "$count" "$anime_id" - count=$((count+1)) + # printf "$menu_format_string" "$count" "$anime_id" + menu+="$count - $anime_id\n" + count=$((count + 1)) done <<-EOF - $search_results + $search_results EOF + user_input=$(printf "${menu[@]}" | rofi -dmenu -config ~/SudacodeRice/rofi/rofidmenu.rasi -l 10 -i -p "Enter number:") + + choice=$(printf '%s\n' "$user_input" | awk '{print $1}') + name=$(printf '%s\n' "$user_input" | awk '{print $NF}') + insert_history "$name" "search" + echo "Number Chosen: $choice" # User input - printf "$c_blue%s$c_green" "Enter number: " - read choice + # printf "$c_blue%s$c_green" "Enter number: " + # read choice printf "$c_reset" # Check if input is a number @@ -146,15 +213,15 @@ anime_selection () { selection_id=$anime_id break fi - count=$((count+1)) + count=$((count + 1)) done <<-EOF - $search_results + $search_results EOF [ -z "$selection_id" ] && die "Invalid number entered" read last_ep_number <<-EOF - $(search_eps "$selection_id") + $(search_eps "$selection_id") EOF } @@ -162,17 +229,21 @@ anime_selection () { ## Ep selection ## ################## -episode_selection () { +episode_selection() { [ $is_download -eq 1 ] && printf "Range of episodes can be specified: start_number end_number\n" - printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number - read ep_choice_start ep_choice_end + # printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number + choice=$(rofi -config ~/SudacodeRice/rofi/rofidmenu.rasi -dmenu -l 1 -i -p "Choose episode:") + ep_choice_start=$(printf '%s\n' "$choice" | awk '{print $1}') + ep_choice_end=$(printf '%s\n' "$choice" | awk '{print $2}') + # echo "$ep_choice_start $ep_choice_end" + # read ep_choice_start ep_choice_end printf "$c_reset" } -open_episode () { +open_episode() { anime_id=$1 episode=$2 @@ -180,34 +251,38 @@ open_episode () { printf '\x1B[2J\x1B[1;1H' if [ $episode -lt 1 ] || [ $episode -gt $last_ep_number ]; then err "Episode out of range" - printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number - read episode + # printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number + # read episode + episode=$(rofi -dmenu -l 1 -p "${c_blue}Choose episode") printf "$c_reset" fi printf "Getting data for episode %d\n" $episode + insert_history "$anime_id" "$episode" + dpage_url=$(get_dpage_link "$anime_id" "$episode") video_url=$(get_links "$dpage_url") case $video_url in - *streamtape*) - # If direct download not available then scrape streamtape.com - BROWSER=${BROWSER:-firefox} - printf "scraping streamtape.com\n" - video_url=$(curl -s "$video_url" | sed -n -E ' + *streamtape*) + # If direct download not available then scrape streamtape.com + BROWSER=${BROWSER:-firefox} + printf "scraping streamtape.com\n" + video_url=$(curl -s "$video_url" | sed -n -E ' /^