add option to specify download directory

This commit is contained in:
ksyasuda 2021-10-30 16:30:00 -07:00
parent 9da0043ea5
commit 817662f489

127
ani-cli
View File

@ -16,6 +16,7 @@ c_magenta="\033[1;35m"
c_cyan="\033[1;36m" c_cyan="\033[1;36m"
c_reset="\033[0m" c_reset="\033[0m"
VERBOSE=1
help_text() { help_text() {
while IFS= read line; do while IFS= read line; do
@ -95,8 +96,17 @@ dep_ch() {
done done
} }
check_anime_name() {
printf "%s\n" "VAR: $1"
if [[ "$1" == "" ]]; then
printf "%s\n" "Passed in name is null"
return 1
fi
return 0
}
check_db() { check_db() {
echo "$1 $2" # echo "$1 $2"
if [[ "$2" == "search" ]]; then if [[ "$2" == "search" ]]; then
stmt="SELECT DISTINCT COUNT(*) FROM search_history \ stmt="SELECT DISTINCT COUNT(*) FROM search_history \
WHERE name = '$1'" WHERE name = '$1'"
@ -116,22 +126,27 @@ update_date() {
if [[ "$2" == "search" ]]; then if [[ "$2" == "search" ]]; then
curdate=$(date +'%Y-%m-%d') curdate=$(date +'%Y-%m-%d')
stmt="UPDATE search_history SET search_date = '$curdate' \ stmt="UPDATE search_history SET search_date = '$curdate' \
WHERE name = '$1'" \ WHERE name = '$1'" &&
&& return 0 || return 1 return 0 || return 1
else else
curdate=$(date +'%Y-%m-%d') curdate=$(date +'%Y-%m-%d')
stmt="UPDATE watch_history SET watch_date = '$curdate' \ stmt="UPDATE watch_history SET watch_date = '$curdate' \
WHERE anime_name = '$1' \ WHERE anime_name = '$1' \
AND episode_number = $2" \ AND episode_number = $2" &&
&& return 0 || return 1 return 0 || return 1
fi fi
} }
insert_history() { insert_history() {
# check_anime_name "$1"
# check the anime_name/id
if [[ "$?" -ne 0 ]]; then
return 1
fi
curdate=$(date +'%Y-%m-%d') curdate=$(date +'%Y-%m-%d')
check_db "$@" check_db "$@"
num=$? num=$?
echo "CHECKDB RETURNED: $num" # echo "CHECKDB RETURNED: $num"
if [[ "$num" -gt 0 ]]; then if [[ "$num" -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"
@ -157,14 +172,18 @@ insert_history() {
get_search_query() { get_search_query() {
stmt="SELECT DISTINCT name FROM search_history" stmt="SELECT DISTINCT name FROM search_history"
cnt_stmt="SELECT DISTINCT COUNT(*) FROM search_history" cnt_stmt="SELECT DISTINCT COUNT(*) FROM search_history"
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 "$history_db")
cnt=$(printf "%s\n" "$cnt_stmt" | sqlite3 "$history_db" | tail -1) cnt=$(printf "%s\n" "$cnt_stmt" | sqlite3 "$history_db" | tail -1)
echo "HISTORY: $hist" if [[ "$VERBOSE" -eq 1 ]]; then
echo "COUNT: $cnt" echo "HISTORY: ${hist[@]}"
fi
# echo "HISTORY: $hist"
# echo "COUNT: $cnt"
if [ -z "$*" ]; then if [ -z "$*" ]; then
echo "QUERYING" # echo "QUERYING"
query=$(printf "%s\n" "${hist[@]}" \ query=$(printf "%s\n" "${hist[@]}" |
| rofi -dmenu -l "$cnt" -p "Search Anime:" \ rofi -dmenu -l "$cnt" -p "Search Anime:" \
-config ${XDG_CONFIG_HOME:-$HOME/.ani-cli}/meh.rasi) -config ${XDG_CONFIG_HOME:-$HOME/.ani-cli}/meh.rasi)
# if [[ "$query" != "" ]]; then # if [[ "$query" != "" ]]; then
# insert_history "$query" "search" # insert_history "$query" "search"
@ -193,6 +212,7 @@ anime_selection() {
count=1 count=1
menu=() menu=()
res=()
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 ] &&
@ -201,13 +221,31 @@ anime_selection() {
# printf "$menu_format_string" "$count" "$anime_id" # printf "$menu_format_string" "$count" "$anime_id"
menu+="$count - $anime_id\n" menu+="$count - $anime_id\n"
idx=$((count - 1))
res["$idx"]="$anime_id"
count=$((count + 1)) count=$((count + 1))
done <<-EOF done <<-EOF
$search_results $search_results
EOF EOF
user_input=$(printf "${menu[@]}" | \ searched=()
cnt=0
for anime in "${res[@]}"; do
# printf "ANIME: $anime"
check_db "$anime" "search"
if [[ $? -gt 0 ]]; then
# printf "%s\n" "SEARCHED BEFORE"
searched+="$cnt, "
fi
((cnt++))
done
# printf "%s\n" "SEARCHED: $searched"
# get the anime from indexed list
user_input=$(printf "${menu[@]}" |
rofi -dmenu -config ${XDG_CONFIG_HOME:-$HOME/.ani-cli}/meh.rasi \ rofi -dmenu -config ${XDG_CONFIG_HOME:-$HOME/.ani-cli}/meh.rasi \
-a "$searched" \
-l 10 -i -p "Enter number:") -l 10 -i -p "Enter number:")
choice=$(printf '%s\n' "$user_input" | awk '{print $1}') choice=$(printf '%s\n' "$user_input" | awk '{print $1}')
@ -248,21 +286,37 @@ anime_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 "%s\n" "Anime ID: $anime_id"
# stmt="SELECT DISTINCT episode_number \ stmt="SELECT DISTINCT episode_number \
# FROM watch_history WHERE anime_name = '$anime_id'" FROM watch_history WHERE anime_name = '$anime_id'"
# cnt_stmt="SELECT DISTINCT COUNT(*) \ # cnt_stmt="SELECT DISTINCT COUNT(*) \
# FROM watch_history WHERE anime_name = '$anime_id'" # FROM watch_history WHERE anime_name = '$anime_id'"
# hist=$(echo "$stmt" | \ hist=$(echo "$stmt" | sqlite3 "$history_db")
# sqlite3 "$history_db"| awk '{ if ( NR > 2 ) { print } }') if [[ "$VERBOSE" -eq 1 ]]; then
echo "HISTORY: ${hist[@]}"
fi
# Get Watch History for $anime_id as comma separated list
watch_history=""
for i in $hist; do
if [[ "$watch_history" == "" ]]; then
watch_history="$((--i))"
else
watch_history="$watch_history, $((--i))"
fi
done
# printf "WATCH HISTORY: %s\n" "$watch_history"
# cnt=$(printf "%s\n" "$cnt_stmt" | sqlite3 "$history_db" | tail -1) # cnt=$(printf "%s\n" "$cnt_stmt" | sqlite3 "$history_db" | tail -1)
eplist="" # printf "NUM EPISODES: $last_ep_number"
echo "NUM EPISODES: $last_ep_number" choice=$(
echo "${eplist[@]}" seq 1 $last_ep_number |
choice=$(seq 1 10 | \
rofi -dmenu -l "$last_ep_number" \ rofi -dmenu -l "$last_ep_number" \
-a "$watch_history" \
-p "Select Episode (1, $last_ep_number):" \ -p "Select Episode (1, $last_ep_number):" \
-config ${XDG_CONFIG_HOME:-$HOME/.ani-cli}/meh.rasi) -config ${XDG_CONFIG_HOME:-$HOME/.ani-cli}/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 $2}')
# echo "$ep_choice_start $ep_choice_end" # echo "$ep_choice_start $ep_choice_end"
@ -274,6 +328,7 @@ episode_selection() {
open_episode() { open_episode() {
anime_id=$1 anime_id=$1
episode=$2 episode=$2
ddir=$3
# clear the screen # clear the screen
printf '\x1B[2J\x1B[1;1H' printf '\x1B[2J\x1B[1;1H'
@ -283,9 +338,13 @@ open_episode() {
FROM watch_history WHERE anime_name = '$anime_id'" FROM watch_history WHERE anime_name = '$anime_id'"
cnt_stmt="SELECT DISTINCT COUNT(*) \ cnt_stmt="SELECT DISTINCT COUNT(*) \
FROM watch_history WHERE anime_name = '$anime_id'" FROM watch_history 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 "$history_db")
if [[ "$VERBOSE" -eq 1 ]]; then
echo "HISTORY: ${hist[@]}"
fi
cnt=$(printf "%s\n" "$cnt_stmt" | sqlite3 "$history_db" | tail -1) cnt=$(printf "%s\n" "$cnt_stmt" | sqlite3 "$history_db" | tail -1)
episode=$(printf "%s\n" "${hist[@]}" | \ episode=$(printf "%s\n" "${hist[@]}" |
rofi -dmenu -l "$cnt" -p "Choose Episode:" \ rofi -dmenu -l "$cnt" -p "Choose Episode:" \
-config ${XDG_CONFIG_HOME:-$HOME/.ani-cli}/meh.rasi) -config ${XDG_CONFIG_HOME:-$HOME/.ani-cli}/meh.rasi)
printf "$c_reset" printf "$c_reset"
@ -325,10 +384,14 @@ open_episode() {
# add 0 padding to the episode name # add 0 padding to the episode name
episode=$(printf "%03d" $episode) episode=$(printf "%03d" $episode)
{ {
(
cd "$download_dir" || return 1
mkdir -p "$anime_id" && cd "$anime_id" || return 1
ffmpeg -headers "Referer: $dpage_url" -i "$video_url" \ ffmpeg -headers "Referer: $dpage_url" -i "$video_url" \
-codec copy "${anime_id}-${episode}.mkv" >/dev/null 2>&1 && -codec copy "${anime_id}-${episode}.mkv" >/dev/null 2>&1 &&
printf "${c_green}Downloaded episode: %s${c_reset}\n" "$episode" || printf "${c_green}Downloaded episode: %s${c_reset}\n" "$episode" ||
printf "${c_red}Download failed episode: %s${c_reset}\n" "$episode" printf "${c_red}Download failed episode: %s${c_reset}\n" "$episode"
)
} }
fi fi
} }
@ -346,7 +409,8 @@ dep_ch "$player_fn" "curl" "sed" "grep"
is_download=0 is_download=0
list_history=0 list_history=0
scrape=query scrape=query
while getopts 'hdHl' OPT; do download_dir="."
while getopts 'hd:Hl' OPT; do
case $OPT in case $OPT in
h) h)
help_text help_text
@ -354,6 +418,11 @@ while getopts 'hdHl' OPT; do
;; ;;
d) d)
is_download=1 is_download=1
download_dir="$OPTARG"
if [ "$VERBOSE" -eq 1 ]; then
echo "DOWNLOAD DIR: $download_dir"
fi
;; ;;
H) H)
scrape=history scrape=history
@ -374,7 +443,7 @@ if [[ "$list_history" -eq 1 ]]; then
cnt_stmt="SELECT DISTINCT COUNT(*) FROM search_history" cnt_stmt="SELECT DISTINCT COUNT(*) FROM search_history"
hist=$(echo "$stmt" | sqlite3 "$history_db") hist=$(echo "$stmt" | sqlite3 "$history_db")
cnt=$(printf "%s\n" "$cnt_stmt" | sqlite3 "$history_db") cnt=$(printf "%s\n" "$cnt_stmt" | sqlite3 "$history_db")
printf "%s\n" "${hist[@]}" | \ printf "%s\n" "${hist[@]}" |
rofi -config ${XDG_CONFIG_HOME:-$HOME/.ani-cli}/meh.rasi \ rofi -config ${XDG_CONFIG_HOME:-$HOME/.ani-cli}/meh.rasi \
-dmenu -l "$cnt" -i -p "Search History" -dmenu -l "$cnt" -i -p "Search History"
exit 0 exit 0
@ -404,7 +473,7 @@ esac
if [ -n "$ep_choice_end" ]; then if [ -n "$ep_choice_end" ]; then
[ "$ep_choice_end" -eq "$ep_choice_end" ] 2>/dev/null || die "Invalid number entered" [ "$ep_choice_end" -eq "$ep_choice_end" ] 2>/dev/null || die "Invalid number entered"
# create list of episodes to download/watch # create list of episodes to download/watch
episodes=$(seq $ep_choice_start $ep_choice_end) episodes=$(seq 1 $ep_choice_end)
fi fi
} }
@ -457,5 +526,5 @@ while :; do
die "invalid choice" die "invalid choice"
;; ;;
esac esac
open_episode "$selection_id" "$episode" open_episode "$selection_id" "$episode" "$download_dir"
done done