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]
This commit is contained in:
ksyasuda 2021-10-29 22:49:52 -07:00
parent e5c2a1796e
commit 9254956775
5 changed files with 234 additions and 92 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.sqlite3
.git
.vscode

169
ani-cli
View File

@ -6,6 +6,7 @@ player_fn="mpv"
prog="ani-cli" prog="ani-cli"
logfile="${XDG_CACHE_HOME:-$HOME/.cache}/ani-hsts" logfile="${XDG_CACHE_HOME:-$HOME/.cache}/ani-hsts"
history_db="history.sqlite3"
c_red="\033[1;31m" c_red="\033[1;31m"
c_green="\033[1;32m" c_green="\033[1;32m"
@ -15,8 +16,9 @@ c_magenta="\033[1;35m"
c_cyan="\033[1;36m" c_cyan="\033[1;36m"
c_reset="\033[0m" 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 while IFS= read line; do
printf "%s\n" "$line" printf "%s\n" "$line"
done <<-EOF done <<-EOF
@ -27,17 +29,16 @@ help_text () {
EOF EOF
} }
die() {
die () {
printf "$c_red%s$c_reset\n" "$*" >&2 printf "$c_red%s$c_reset\n" "$*" >&2
exit 1 exit 1
} }
err () { err() {
printf "$c_red%s$c_reset\n" "$*" >&2 printf "$c_red%s$c_reset\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 ' ' '-')
titlepattern='<a href="/category/' titlepattern='<a href="/category/'
@ -50,7 +51,7 @@ search_anime () {
' '
} }
search_eps () { search_eps() {
# get available episodes for anime_id # get available episodes for anime_id
anime_id=$1 anime_id=$1
@ -76,7 +77,7 @@ get_dpage_link() {
}' }'
} }
get_links () { get_links() {
dpage_url="$1" dpage_url="$1"
curl -s "$dpage_url" | curl -s "$dpage_url" |
@ -87,53 +88,119 @@ get_links () {
}' | tr -d ' ' }' | tr -d ' '
} }
dep_ch () { dep_ch() {
for dep; do 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." die "Program \"$dep\" not found. Please install it."
fi fi
done 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 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 if [ -z "$*" ]; then
printf "Search Anime: " query=$(printf "%s\n" "${hist[@]}" | rofi -dmenu -l "$cnt" -p "Search Anime:" -config ~/SudacodeRice/rofi/rofidmenu.rasi)
read -r query # if [[ "$query" != "" ]]; then
# insert_history "$query" "search"
# else
# echo 'Query empty... Skipping insert'
# fi
# printf "Search Anime: "
# read -r query
else else
query=$* query=$*
fi fi
} }
# create history file # create history file
[ -f "$logfile" ] || : > "$logfile" [ -f "$logfile" ] || : >"$logfile"
##################### #####################
## Anime selection ## ## Anime selection ##
##################### #####################
anime_selection () { anime_selection() {
search_results=$* search_results=$*
menu_format_string='[%d] %s\n' menu_format_string='[%d] %s\n'
menu_format_string_c1="$c_blue[$c_cyan%d$c_blue] $c_reset%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" menu_format_string_c2="$c_blue[$c_cyan%d$c_blue] $c_yellow%s$c_reset\n"
count=1 count=1
menu=()
while read anime_id; do while read anime_id; do
# alternating colors for menu # alternating colors for menu
[ $((count % 2)) -eq 0 ] && [ $((count % 2)) -eq 0 ] &&
menu_format_string=$menu_format_string_c1 || menu_format_string=$menu_format_string_c1 ||
menu_format_string=$menu_format_string_c2 menu_format_string=$menu_format_string_c2
printf "$menu_format_string" "$count" "$anime_id" # printf "$menu_format_string" "$count" "$anime_id"
count=$((count+1)) menu+="$count - $anime_id\n"
count=$((count + 1))
done <<-EOF done <<-EOF
$search_results $search_results
EOF 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 # User input
printf "$c_blue%s$c_green" "Enter number: " # printf "$c_blue%s$c_green" "Enter number: "
read choice # read choice
printf "$c_reset" printf "$c_reset"
# Check if input is a number # Check if input is a number
@ -146,7 +213,7 @@ anime_selection () {
selection_id=$anime_id selection_id=$anime_id
break break
fi fi
count=$((count+1)) count=$((count + 1))
done <<-EOF done <<-EOF
$search_results $search_results
EOF EOF
@ -162,17 +229,21 @@ anime_selection () {
## Ep selection ## ## Ep selection ##
################## ##################
episode_selection () { episode_selection() {
[ $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"
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 ep_choice_start ep_choice_end 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" printf "$c_reset"
} }
open_episode () { open_episode() {
anime_id=$1 anime_id=$1
episode=$2 episode=$2
@ -180,13 +251,16 @@ open_episode () {
printf '\x1B[2J\x1B[1;1H' printf '\x1B[2J\x1B[1;1H'
if [ $episode -lt 1 ] || [ $episode -gt $last_ep_number ]; then if [ $episode -lt 1 ] || [ $episode -gt $last_ep_number ]; then
err "Episode out of range" err "Episode out of range"
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
episode=$(rofi -dmenu -l 1 -p "${c_blue}Choose episode")
printf "$c_reset" printf "$c_reset"
fi fi
printf "Getting data for episode %d\n" $episode printf "Getting data for episode %d\n" $episode
insert_history "$anime_id" "$episode"
dpage_url=$(get_dpage_link "$anime_id" "$episode") dpage_url=$(get_dpage_link "$anime_id" "$episode")
video_url=$(get_links "$dpage_url") video_url=$(get_links "$dpage_url")
@ -200,14 +274,15 @@ open_episode () {
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
# write anime and episode number # write anime and episode number
sed -E " sed -E "
s/^${selection_id}\t[0-9]+/${selection_id}\t$((episode+1))/ s/^${selection_id}\t[0-9]+/${selection_id}\t$((episode + 1))/
" "$logfile" > "${logfile}.new" && mv "${logfile}.new" "$logfile" " "$logfile" >"${logfile}.new" && mv "${logfile}.new" "$logfile"
setsid -f $player_fn --http-header-fields="Referer: $dpage_url" "$video_url" >/dev/null 2>&1 setsid -f $player_fn --http-header-fields="Referer: $dpage_url" "$video_url" >/dev/null 2>&1
else else
@ -235,8 +310,9 @@ dep_ch "$player_fn" "curl" "sed" "grep"
# option parsing # option parsing
is_download=0 is_download=0
list_history=0
scrape=query scrape=query
while getopts 'hdH' OPT; do while getopts 'hdHl' OPT; do
case $OPT in case $OPT in
h) h)
help_text help_text
@ -248,6 +324,9 @@ while getopts 'hdH' OPT; do
H) H)
scrape=history scrape=history
;; ;;
l)
list_history=1
;;
esac esac
done done
shift $((OPTIND - 1)) shift $((OPTIND - 1))
@ -256,15 +335,25 @@ shift $((OPTIND - 1))
# main # # main #
######## ########
if [[ "$list_history" -eq 1 ]]; then
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")
printf "%s\n" "${hist[@]}" | rofi -config ~/SudacodeRice/rofi/rofidmenu.rasi -dmenu -l "$cnt" -i -p "Search History"
exit 0
fi
case $scrape in case $scrape in
query) query)
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"
episode_selection episode_selection
;; ;;
history) history)
search_results=$(sed -n -E 's/\t[0-9]*//p' "$logfile") search_results=$(sed -n -E 's/\t[0-9]*//p' "$logfile")
[ -z "$search_results" ] && die "History is empty" [ -z "$search_results" ] && die "History is empty"
anime_selection "$search_results" anime_selection "$search_results"
@ -272,7 +361,6 @@ case $scrape in
;; ;;
esac esac
{ # checking input { # checking input
[ "$ep_choice_start" -eq "$ep_choice_start" ] 2>/dev/null || die "Invalid number entered" [ "$ep_choice_start" -eq "$ep_choice_start" ] 2>/dev/null || die "Invalid number entered"
episodes=$ep_choice_start episodes=$ep_choice_start
@ -286,14 +374,14 @@ esac
# add anime to history file # add anime to history file
grep -q -w "${selection_id}" "$logfile" || grep -q -w "${selection_id}" "$logfile" ||
printf "%s\t%d\n" "$selection_id" $((episode+1)) >> "$logfile" printf "%s\t%d\n" "$selection_id" $((episode + 1)) >>"$logfile"
for ep in $episodes for ep in $episodes; do
do
open_episode "$selection_id" "$ep" open_episode "$selection_id" "$ep"
done done
episode=${ep_choice_end:-$ep_choice_start} episode=${ep_choice_end:-$ep_choice_start}
choice=''
while :; do while :; do
printf "\n${c_green}Currently playing %s episode ${c_cyan}%d/%d\n" "$selection_id" $episode $last_ep_number printf "\n${c_green}Currently playing %s episode ${c_cyan}%d/%d\n" "$selection_id" $episode $last_ep_number
printf "$c_blue[${c_cyan}%s$c_blue] $c_yellow%s$c_reset\n" "n" "next episode" printf "$c_blue[${c_cyan}%s$c_blue] $c_yellow%s$c_reset\n" "n" "next episode"
@ -302,7 +390,11 @@ while :; do
printf "$c_blue[${c_cyan}%s$c_blue] $c_magenta%s$c_reset\n" "r" "replay current episode" printf "$c_blue[${c_cyan}%s$c_blue] $c_magenta%s$c_reset\n" "r" "replay current episode"
printf "$c_blue[${c_cyan}%s$c_blue] $c_red%s$c_reset\n" "q" "exit" printf "$c_blue[${c_cyan}%s$c_blue] $c_red%s$c_reset\n" "q" "exit"
printf "${c_blue}Enter choice:${c_green} " printf "${c_blue}Enter choice:${c_green} "
printf "$c_reset"
read choice read choice
# choice=$(printf '%s\n' "${args[@]}" | rofi -dmenu -l 8 -i -p "Enter choice:")
# choice=$(printf '%s\n' "$choice" | awk '{print $1}')
printf "$c_reset" printf "$c_reset"
case $choice in case $choice in
n) n)
@ -312,7 +404,8 @@ while :; do
episode=$((episode - 1)) episode=$((episode - 1))
;; ;;
s) printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number s)
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"
@ -321,12 +414,12 @@ while :; do
r) ;; r) ;;
q) q)
break;; break
;;
*) *)
die "invalid choice" die "invalid choice"
;; ;;
esac esac
open_episode "$selection_id" "$episode" open_episode "$selection_id" "$episode"
done done

33
db.sh Executable file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env bash
DB='history.sqlite3'
while getopts 'cdr' OPT; do
case "$OPT" in
c)
printf "%s\n" "Creating database..."
sqlite3 "$DB" <sql/anime_search_history.sql
sqlite3 "$DB" <sql/watch_history.sql
printf "%s\n" "Created database..."
;;
d)
printf "%s\n" "Deleting database..."
rm -rf "$DB"
printf "%s\n" "Database deleted..."
;;
r)
printf "%s\n" "Deleting database..."
rm -rf "$DB"
printf "%s\n" "Database deleted..."
printf "%s\n" "Creating database..."
sqlite3 "$DB" <sql/anime_search_history.sql
sqlite3 "$DB" <sql/watch_history.sql
printf "%s\n" "Created database..."
;;
*)
printf "%s\n" "Don't get here"
exit 1
;;
esac
done

View File

@ -0,0 +1,6 @@
CREATE TABLE search_history (
id integer PRIMARY KEY autoincrement,
name varchar(200) NOT NULL,
search_date date NOT NULL
);

7
sql/watch_history.sql Normal file
View File

@ -0,0 +1,7 @@
CREATE TABLE watch_history (
id integer PRIMARY KEY AUTOINCREMENT,
anime_name varchar(200) NOT NULL,
episode_number integer NOT NULL,
watch_date date
);