fix colorscheme for active items

update history function to use database instead of history file
This commit is contained in:
ksyasuda 2021-11-01 17:05:43 -07:00
parent e42629d406
commit 2912950366
2 changed files with 98 additions and 94 deletions

191
ani-cli
View File

@ -117,6 +117,10 @@ check_anime_name() {
return 0 return 0
} }
run_stmt() {
printf "%s\n" "$1" | sqlite3 -noheader "$history_db"
}
check_db() { check_db() {
# Return number of matches for anime/episode in db # Return number of matches for anime/episode in db
# echo "$1 $2" # echo "$1 $2"
@ -124,7 +128,7 @@ check_db() {
stmt="SELECT DISTINCT COUNT(*) \ stmt="SELECT DISTINCT COUNT(*) \
FROM search_history \ FROM search_history \
WHERE anime_name = '$1';" WHERE anime_name = '$1';"
res="$(printf '%s\n' $stmt | sqlite3 -noheader $history_db | tail -1)" res=$(run_stmt "$stmt")
# echo "QUERY RESULT $res" # echo "QUERY RESULT $res"
return "$res" return "$res"
else else
@ -132,7 +136,7 @@ check_db() {
FROM watch_history \ FROM watch_history \
WHERE anime_name = '$1' \ WHERE anime_name = '$1' \
AND episode_number = $2;" AND episode_number = $2;"
res="$(printf '%s\n' $stmt | sqlite3 -noheader $history_db | tail -1)" res=$(run_stmt "$stmt")
# echo "QUERY RESULT $res" # echo "QUERY RESULT $res"
return "$res" return "$res"
fi fi
@ -141,16 +145,16 @@ check_db() {
update_date() { update_date() {
# updates search/watch date for passed in anime # updates search/watch date for passed in anime
datetime=$(date +'%Y-%m-%d %H:%M:%S') datetime=$(date +'%Y-%m-%d %H:%M:%S')
stmt=""
if [[ "$2" == "search" ]]; then if [[ "$2" == "search" ]]; then
stmt="UPDATE search_history SET search_date = '$datetime' \ stmt="UPDATE search_history SET search_date = '$datetime' \
WHERE anime_name = '$1';" && WHERE anime_name = '$1';"
return 0 || return 1
else else
stmt="UPDATE watch_history SET watch_date = '$datetime' \ stmt="UPDATE watch_history SET watch_date = '$datetime' \
WHERE anime_name = '$1' \ WHERE anime_name = '$1' \
AND episode_number = $2;" && AND episode_number = $2;"
return 0 || return 1
fi fi
run_stmt "$stmt"
} }
insert_history() { insert_history() {
@ -162,9 +166,8 @@ insert_history() {
fi fi
datetime=$(date +'%Y-%m-%d %H:%M:%S') datetime=$(date +'%Y-%m-%d %H:%M:%S')
check_db "$@" check_db "$@"
num=$?
# echo "CHECKDB RETURNED: $num" # echo "CHECKDB RETURNED: $num"
if [[ "$num" -gt 0 ]]; then if [[ "$?" -gt 0 ]]; then
if [[ "$2" == "search" ]]; then if [[ "$2" == "search" ]]; then
printf "%s\n" "Already in search db... Updating search_date" printf "%s\n" "Already in search db... Updating search_date"
else else
@ -175,12 +178,12 @@ insert_history() {
if [[ "$2" == "search" ]]; then if [[ "$2" == "search" ]]; then
stmt="INSERT INTO search_history(anime_name, search_date) \ stmt="INSERT INTO search_history(anime_name, search_date) \
VALUES('$1', '$datetime');" VALUES('$1', '$datetime');"
printf "%s\n" "$stmt" | sqlite3 -noheader "$history_db" run_stmt "$stmt"
else else
stmt="INSERT INTO \ stmt="INSERT INTO \
watch_history(anime_name, episode_number, watch_date) \ watch_history(anime_name, episode_number, watch_date) \
VALUES('$1', '$2', '$datetime');" VALUES('$1', '$2', '$datetime');"
printf "%s\n" "$stmt" | sqlite3 -noheader "$history_db" run_stmt "$stmt"
fi fi
fi fi
} }
@ -193,16 +196,9 @@ get_search_query() {
stmt="SELECT DISTINCT anime_name \ stmt="SELECT DISTINCT anime_name \
FROM search_history \ FROM search_history \
ORDER BY search_date DESC;" ORDER BY search_date DESC;"
# cnt_stmt="SELECT DISTINCT COUNT(*) FROM search_history" # hist=$(echo "$stmt" | sqlite3 -noheader "$history_db")
hist=$(echo "$stmt" | sqlite3 -noheader "$history_db") hist=$(run_stmt "$stmt")
# hist=$(echo "$stmt" | sqlite3 -noheader "$history_db" | awk '{ if ( NR > 2 ) { print } }')
# cnt=$(printf "%s\n" "$cnt_stmt" | sqlite3 -noheader "$history_db" | tail -1)
if [[ "$VERBOSE" -eq 1 ]]; then
echo "HISTORY:" "${hist[@]}"
fi
# echo "HISTORY: $hist"
# echo "COUNT: $cnt"
if [ -z "$*" ]; then if [ -z "$*" ]; then
query=$(printf "%s\n" "${hist[@]}" | query=$(printf "%s\n" "${hist[@]}" |
rofi -dmenu -l 12 -p "Search Anime:" \ rofi -dmenu -l 12 -p "Search Anime:" \
@ -246,19 +242,23 @@ anime_selection() {
$search_results $search_results
EOF EOF
searched=() searched=""
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
# printf "ANIME: $anime" [ "$VERBOSE" -eq 1 ] && printf "%s\n" "ANIME: $anime"
check_db "$anime" "search" check_db "$anime" "search"
if [[ $? -gt 0 ]]; then if [[ $? -gt 0 ]]; then
# printf "%s\n" "SEARCHED BEFORE" [ "$VERBOSE" -eq 1 ] && printf "%s\n" "SEARCHED BEFORE"
searched+=("$((cnt++)), ") if [[ "$searched" == "" ]]; then
searched="$((cnt++))"
else
searched="$searched, $((cnt++))"
fi
fi fi
done done
# printf "%s\n" "SEARCHED: $searched" printf "%s\n" "SEARCHED: $searched"
# get the anime from indexed list # get the anime from indexed list
user_input=$(printf "${menu[@]}" | user_input=$(printf "${menu[@]}" |
@ -312,10 +312,9 @@ episode_selection() {
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';"
hist=$(echo "$stmt" | sqlite3 -noheader "$history_db") hist=$(run_stmt "$stmt")
# hist=$(echo "$stmt" | sqlite3 "$history_db" | awk '{ if ( NR > 2 ) { print } }')
if [[ "$VERBOSE" -eq 1 ]]; then if [[ "$VERBOSE" -eq 1 ]]; then
echo "HISTORY: ${hist[@]}" printf "%s\n" "HISTORY: ${hist[*]}"
fi fi
# Get Watch History for $anime_id as comma separated list # Get Watch History for $anime_id as comma separated list
@ -329,19 +328,25 @@ episode_selection() {
done done
[ "$VERBOSE" -eq 1 ] && printf "WATCH HISTORY: %s\n" "$watch_history" [ "$VERBOSE" -eq 1 ] && printf "WATCH HISTORY: %s\n" "$watch_history"
# cnt=$(printf "%s\n" "$cnt_stmt" | sqlite3 "$history_db" | tail -1)
# printf "NUM EPISODES: $last_ep_number" # printf "NUM EPISODES: $last_ep_number"
# 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>'
choice=$( choice=$(
seq 1 "$last_ep_number" | seq 1 "$last_ep_number" |
rofi -dmenu -l 12 \ rofi -dmenu -l 12 \
-a "$watch_history" \ -a "$watch_history" \
-p "Select Episode (1, $last_ep_number):" \ -p "Select Episode [1, $last_ep_number]:" \
-mesg "$msg" \
-config "$config_dir"/meh.rasi -config "$config_dir"/meh.rasi
) )
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 $2}') ep_choice_end=$(printf '%s\n' "${choice}" | awk '{print $NF}')
# echo "$ep_choice_start $ep_choice_end"
# if only one episode was entered, set ep_choice_end to empty string so only selected episode plays
# otherwise plays from ep 1 - ep_choice_start
[ "$ep_choice_start" -eq "$ep_choice_end" ] && ep_choice_end=""
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "START: $ep_choice_start | END: $ep_choice_end"
# read ep_choice_start ep_choice_end # read ep_choice_start ep_choice_end
printf "$c_reset" printf "$c_reset"
@ -360,9 +365,9 @@ open_episode() {
FROM watch_history \ FROM watch_history \
WHERE anime_name = '$anime_id';" WHERE anime_name = '$anime_id';"
# hist=$(echo "$stmt" | sqlite3 "$history_db" | awk '{ if ( NR > 2 ) { print } }') # hist=$(echo "$stmt" | sqlite3 "$history_db" | awk '{ if ( NR > 2 ) { print } }')
hist=$(echo "$stmt" | sqlite3 -noheader "$history_db") hist=$(run_stmt "$stmt")
if [[ "$VERBOSE" -eq 1 ]]; then if [[ "$VERBOSE" -eq 1 ]]; then
echo "HISTORY: ${hist[@]}" echo "HISTORY: ${hist[*]}"
fi fi
episode=$(printf "%s\n" "${hist[@]}" | episode=$(printf "%s\n" "${hist[@]}" |
rofi -dmenu -l 12 -p "Choose Episode:" \ rofi -dmenu -l 12 -p "Choose Episode:" \
@ -378,17 +383,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
@ -434,24 +439,24 @@ scrape=query
download_dir="." download_dir="."
while getopts 'hd:Hl' OPT; do while getopts 'hd:Hl' 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
echo "DOWNLOAD DIR: $download_dir" echo "DOWNLOAD DIR: $download_dir"
fi fi
;; ;;
H) H)
scrape=history scrape=history
;; ;;
l) l)
list_history=1 list_history=1
;; ;;
esac esac
done done
shift $((OPTIND - 1)) shift $((OPTIND - 1))
@ -464,11 +469,7 @@ if [[ "$list_history" -eq 1 ]]; then
stmt="SELECT DISTINCT anime_name \ stmt="SELECT DISTINCT anime_name \
FROM search_history \ FROM search_history \
ORDER BY search_date DESC" ORDER BY search_date DESC"
# cnt_stmt="SELECT DISTINCT COUNT(*) FROM search_history" hist=$(run_stmt "$stmt")
# hist=$(echo "$stmt" | sqlite3 "$history_db")
# hist=$(echo "$stmt" | sqlite3 -noheader "$history_db" | awk '{ if ( NR > 2 ) { print } }')
hist=$(echo "$stmt" | sqlite3 -noheader "$history_db")
# cnt=$(printf "%s\n" "$cnt_stmt" | sqlite3 -noheader "$history_db")
printf "%s\n" "${hist[@]}" | printf "%s\n" "${hist[@]}" |
rofi -config "$config_dir"/meh.rasi \ rofi -config "$config_dir"/meh.rasi \
-dmenu -l 12 -i -p "Search History" -dmenu -l 12 -i -p "Search History"
@ -476,20 +477,22 @@ if [[ "$list_history" -eq 1 ]]; then
fi 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" stmt="SELECT DISTINCT anime_name FROM watch_history ORDER BY watch_date DESC"
anime_selection "$search_results" search_results=$(printf "%s\n" "$stmt" | sqlite3 -noheader "$history_db")
ep_choice_start=$(sed -n -E "s/${selection_id}\t//p" "$logfile") [ -z "$search_results" ] && die "History is empty"
;; anime_selection "${search_results[@]}"
ep_choice_start=$(sed -n -E "s/${selection_id}\t//p" "$logfile")
;;
esac esac
{ # checking input { # checking input
@ -528,29 +531,29 @@ while :; do
# choice=$(printf '%s\n' "$choice" | awk '{print $1}') # choice=$(printf '%s\n' "$choice" | awk '{print $1}')
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

View File

@ -31,6 +31,7 @@ message {
border: 2px 0px 0px ; border: 2px 0px 0px ;
border-color: @separatorcolor; border-color: @separatorcolor;
padding: 1px ; padding: 1px ;
text-color: @active-foreground;
} }
textbox { textbox {
text-color: @foreground; text-color: @foreground;