Compare commits

..

No commits in common. "ebc8587e6fbe86fc6e6be8b00081cd2a4458a73f" and "24f3c251422a05d93d62adeaf21882a132d33ff5" have entirely different histories.

117
ani-cli
View File

@ -15,6 +15,7 @@ SILENT=0
player_fn="mpv" player_fn="mpv"
playable="\.mp4|\.mkv|\.ts|\.mp3|\.webm" playable="\.mp4|\.mkv|\.ts|\.mp3|\.webm"
prog="ani-cli"
c_red="\033[1;31m" c_red="\033[1;31m"
c_green="\033[1;32m" c_green="\033[1;32m"
c_yellow="\033[1;33m" c_yellow="\033[1;33m"
@ -23,6 +24,17 @@ c_magenta="\033[1;35m"
c_cyan="\033[1;36m" c_cyan="\033[1;36m"
c_reset="\033[0m" c_reset="\033[0m"
help_text() {
while IFS= read -r line; do
printf "%s\n" "$line"
done <<- EOF
USAGE: $prog <query>
-h show this help text
-d download episode
-H continue where you left off
EOF
}
die() { die() {
((SILENT != 1)) && printf "$c_red%s$c_reset\n" "$*" >&2 ((SILENT != 1)) && printf "$c_red%s$c_reset\n" "$*" >&2
exit 1 exit 1
@ -47,8 +59,8 @@ check_input() {
fi fi
} }
# get the download page url
get_dpage_link() { get_dpage_link() {
# get the download page url
anime_id=$1 anime_id=$1
ep_no=$2 ep_no=$2
@ -143,22 +155,17 @@ run_stmt() {
# Return number of matches for anime/episode in db # Return number of matches for anime/episode in db
check_db() { check_db() {
case "$1" in if [[ "$1" == "directory" ]]; then
directory)
stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2';" stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2';"
;; elif [[ "$1" == "file" ]]; then
file)
stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2' AND filename = '$3';" stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2' AND filename = '$3';"
;; elif [[ "$2" == "search" ]]; then
search) stmt="SELECT DISTINCT COUNT(*) FROM search_history WHERE anime_name = '$1';"
stmt="SELECT DISTINCT COUNT(*) FROM search_history WHERE anime_name = '$2';" else
;; stmt="SELECT DISTINCT COUNT(*) FROM watch_history WHERE anime_name = '$1' AND episode_number = '$2';"
watch | sync) fi
stmt="SELECT DISTINCT COUNT(*) FROM watch_history WHERE anime_name = '$2' AND episode_number = '$3';"
;;
esac
res=$(run_stmt "$stmt") res=$(run_stmt "$stmt")
return "$res" return $res
} }
# return true (0) if $source_dt > $target_dt # return true (0) if $source_dt > $target_dt
@ -176,37 +183,31 @@ check_date() {
update_date() { update_date() {
datetime=$(date +'%Y-%m-%d %H:%M:%S') datetime=$(date +'%Y-%m-%d %H:%M:%S')
stmt="" stmt=""
case "$1" in if [[ "$1" == "directory" ]]; then
directory) lg "UPDATING FILE_HISTORY: directory='$2', filename='DIRECTORY', search_date='$datetime'"
lg "UPDATING FILE_HISTORY for with directory: directory='$2', filename='DIRECTORY', search_date='$datetime'"
stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';" stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';"
;; elif [[ "$1" == "file" ]]; then
file) lg "UPDATING FILE_HISTORY: directory='$2', filename='$3', search_date='$datetime'"
lg "UPDATING FILE_HISTORY: directory='$2', filename='$3', watch_date='$datetime'"
stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';" stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';"
;; elif [[ "$2" == "search" ]]; then
search) lg "UPDATING SEARCH_HISTORY: anime_name='$1', search_date='$datetime'"
lg "UPDATING SEARCH_HISTORY: anime_name='$2', search_date='$datetime'" stmt="UPDATE search_history SET search_date = '$datetime' WHERE anime_name = '$1';"
stmt="UPDATE search_history SET search_date = '$datetime' WHERE anime_name = '$2';" elif [[ $# -ge 3 ]]; then
;;
sync)
temp_dt="${3// /:}" temp_dt="${3// /:}"
[ -z "$temp_dt" ] && return 1 [ -z "$temp_dt" ] && return 1
hist_dt=$(run_stmt "SELECT watch_date FROM watch_history WHERE anime_name='$2' AND episode_number='$3';") hist_dt=$(run_stmt "SELECT watch_date FROM watch_history WHERE anime_name='$1' AND episode_number='$2';")
hist_dt="${hist_dt// /:}" hist_dt="${hist_dt// /:}"
lg "Checking if update is needed..." lg "Checking if update is needed..."
if ! check_date "$hist_dt" "$temp_dt"; then if ! check_date "$hist_dt" "$temp_dt"; then
lg "Passed in date is older or same than current date... doing nothing" lg "Passed in date is older or same than current date... doing nothing"
return 1 return 1
fi fi
lg "UPDATING WATCH_HISTORY from sync. watch_date -> $temp_dt" lg "UPDATING watch_history from sync. watch_date -> $temp_dt"
stmt="UPDATE watch_history SET watch_date = '$temp_dt' WHERE anime_name = '$2' AND episode_number = $3;" stmt="UPDATE watch_history SET watch_date = '$temp_dt' WHERE anime_name = '$1' AND episode_number = $2;"
;; else
watch) lg "UPDATING WATCH_HISTORY: anime_name='$1', episode_number='$2' search_date='$datetime'"
lg "UPDATING WATCH_HISTORY: anime_name='$2', episode_number='$3' watch_date='$datetime'" stmt="UPDATE watch_history SET watch_date = '$datetime' WHERE anime_name = '$1' AND episode_number = $2;"
stmt="UPDATE watch_history SET watch_date = '$datetime' WHERE anime_name = '$2' AND episode_number = $3;" fi
;;
esac
wait # in case there's another insert/update still running in background? wait # in case there's another insert/update still running in background?
run_stmt "$stmt" run_stmt "$stmt"
} }
@ -273,6 +274,7 @@ sync_watch_history() {
errs=0 errs=0
while read -r line; do while read -r line; do
anime_name="${line/ //}" anime_name="${line/ //}"
# for each episode of $anime_name on the remote machine, check local
while read -r ep; do while read -r ep; do
episode_num=$(awk -F '|' '{print $1}' <<< "$ep") episode_num=$(awk -F '|' '{print $1}' <<< "$ep")
watch_date=$(awk -F '|' '{print $NF}' <<< "$ep") watch_date=$(awk -F '|' '{print $NF}' <<< "$ep")
@ -358,7 +360,8 @@ get_directory_data() {
find_media() { find_media() {
inp="$1" inp="$1"
[ -z "$inp" ] && die "No directory" [ -z "$inp" ] && die "No directory"
lg "BEGIN find_media() on $inp" 1> /dev/stderr # workaround to allow logging w/o affecting return output
lg "INPUT DIR: $inp" 1> /dev/stderr
# base case hit when a file is found # base case hit when a file is found
if [ -f "$inp" ]; then if [ -f "$inp" ]; then
@ -367,6 +370,7 @@ find_media() {
fi fi
get_directory_data "$inp" get_directory_data "$inp"
[ -z "$inp" ] && return 1
selection="$(rofi -dpi "$DPI" -dmenu -only-match -async-pre-read 33 -config "$ROFI_CFG" \ selection="$(rofi -dpi "$DPI" -dmenu -only-match -async-pre-read 33 -config "$ROFI_CFG" \
-l 15 -i -sep '|' -mesg "$(generate_span "Current directory: $inp")" -a "$watched" \ -l 15 -i -sep '|' -mesg "$(generate_span "Current directory: $inp")" -a "$watched" \
-p "Enter selection" <<< "$inputlist")" -p "Enter selection" <<< "$inputlist")"
@ -404,8 +408,13 @@ find_media() {
## main code ## ## main code ##
##################### #####################
# get query
get_search_query() { get_search_query() {
stmt="SELECT DISTINCT id || '. ' || anime_name FROM search_history ORDER BY search_date DESC;" # Get search history
stmt="SELECT DISTINCT id || '. ' || anime_name \
FROM search_history \
ORDER BY search_date DESC;"
msg="Choose from list of searched anime below, or enter a unique name of an anime to search for" msg="Choose from list of searched anime below, or enter a unique name of an anime to search for"
if [ "$IS_ROFI" -eq 1 ]; then if [ "$IS_ROFI" -eq 1 ]; then
query=$(rofi -dpi "$DPI" -dmenu -l 15 -p "Search Anime:" \ query=$(rofi -dpi "$DPI" -dmenu -l 15 -p "Search Anime:" \
@ -420,8 +429,8 @@ get_search_query() {
fi fi
} }
# get anime name along with its id
search_anime() { search_anime() {
# get anime name along with its id
lg "NUM ARGS: $#" lg "NUM ARGS: $#"
if [[ $# -gt 1 ]]; then if [[ $# -gt 1 ]]; then
# if multi-word query, concatenate into one string and replace spaces with '-' # if multi-word query, concatenate into one string and replace spaces with '-'
@ -441,7 +450,9 @@ search_anime() {
} }
search_eps() { search_eps() {
# get available episodes for anime_id
anime_id=$1 anime_id=$1
curl -s "$BASE_URL/category/$anime_id" | curl -s "$BASE_URL/category/$anime_id" |
sed -n -E ' sed -n -E '
/^[[:space:]]*<a href="#" class="active" ep_start/{ /^[[:space:]]*<a href="#" class="active" ep_start/{
@ -451,8 +462,8 @@ search_eps() {
' '
} }
# Select anime from query results
anime_selection() { anime_selection() {
# Select anime from query results
search_results=$* search_results=$*
if [ "$IS_ROFI" -eq 1 ]; then if [ "$IS_ROFI" -eq 1 ]; then
menu=() menu=()
@ -464,7 +475,7 @@ anime_selection() {
menu+="$((cnt + 1)). $anime_id\n" menu+="$((cnt + 1)). $anime_id\n"
res["$cnt"]="$anime_id" res["$cnt"]="$anime_id"
lg "ANIME: $anime_id" lg "ANIME: $anime_id"
if ! check_db "search" "$anime_id"; then if ! check_db "$anime_id" "search"; then
lg "$anime_id HAS BEEN SEARCHED BEFORE" lg "$anime_id HAS BEEN SEARCHED BEFORE"
[ -z "$searched" ] && searched="$cnt" || searched="$searched, $cnt" [ -z "$searched" ] && searched="$cnt" || searched="$searched, $cnt"
fi fi
@ -499,7 +510,7 @@ anime_selection() {
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
while read -r 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 ||
@ -547,7 +558,9 @@ episode_selection() {
if [ "$IS_ROFI" -eq 1 ]; then if [ "$IS_ROFI" -eq 1 ]; then
# select episode number for anime # select episode number for anime
lg "Anime ID: $anime_id" lg "Anime ID: $anime_id"
stmt="SELECT DISTINCT episode_number FROM watch_history WHERE anime_name = '$anime_id';" stmt="SELECT DISTINCT episode_number \
FROM watch_history \
WHERE anime_name = '$anime_id';"
# Get Watch History for $anime_id as comma separated list # Get Watch History for $anime_id as comma separated list
watch_history="" watch_history=""
@ -561,9 +574,7 @@ episode_selection() {
lg "Episode watch history -> $watch_history" lg "Episode watch history -> $watch_history"
# get user choice and set the start and end # get user choice and set the start and end
msg1="Anime Name: $anime_id" msg=$(printf "%s\n%s" "$(generate_span "Anime Name: $anime_id")" "$(generate_span "Range of episodes can be provided as: START_EPISODE - END_EPISODE")")
msg2="Range of episodes can be provided as: START_EPISODE - END_EPISODE"
[ "$is_download" -eq 1 ] && msg=$(printf "%s\n%s" "$(generate_span "$msg1")" "$(generate_span "$msg2")") || msg=$(printf "%s\n" "$(generate_span "$msg1")")
choice=$( choice=$(
seq 1 "$last_ep_number" | seq 1 "$last_ep_number" |
rofi -dpi "$DPI" -dmenu -l 12 \ rofi -dpi "$DPI" -dmenu -l 12 \
@ -673,7 +684,7 @@ stream() {
anime_id="${query// /}" anime_id="${query// /}"
[ -z "$anime_id" ] && die "No anime selected or queried" [ -z "$anime_id" ] && die "No anime selected or queried"
lg "Checking if anime: $anime_id has been searched before..." lg "Checking if anime: $anime_id has been searched before..."
check_db "search" "$anime_id" check_db "$anime_id" "search"
searched="$?" searched="$?"
lg "Searched before: $searched" lg "Searched before: $searched"
if [ "$searched" -eq 0 ]; then if [ "$searched" -eq 0 ]; then
@ -694,6 +705,7 @@ stream() {
episode_selection episode_selection
} }
# option parsing
parse_args() { parse_args() {
# to clear the colors when exited using SIGINT # to clear the colors when exited using SIGINT
trap "printf '$c_reset'" INT HUP trap "printf '$c_reset'" INT HUP
@ -702,8 +714,12 @@ parse_args() {
is_download=0 is_download=0
download_dir="." download_dir="."
half_ep=0 half_ep=0
while getopts 'd:Hsvq:cf:t:T:CQ:D:S' OPT; do while getopts 'hd:Hsvq:cf:t:T:CQ:D:S' OPT; do
case "$OPT" in case "$OPT" in
h)
help_text
exit 0
;;
d) d)
is_download=1 is_download=1
download_dir="$OPTARG" download_dir="$OPTARG"
@ -818,7 +834,11 @@ main() {
fi fi
lg "SELECTION: $selection_id" lg "SELECTION: $selection_id"
stmt="SELECT episode_number FROM watch_history WHERE anime_name = '$selection_id' ORDER BY watch_date DESC LIMIT 1;" 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") ep_choice_start=$(run_stmt "$stmt")
lg "Most recently watched episode: $ep_choice_start" lg "Most recently watched episode: $ep_choice_start"
;; ;;
@ -873,6 +893,7 @@ main() {
check_input check_input
# plays selected episode(s)
for ep in $episodes; do for ep in $episodes; do
open_episode "$selection_id" "$ep" "$download_dir" open_episode "$selection_id" "$ep" "$download_dir"
done done