mirror of
https://github.com/ksyasuda/aniwrapper.git
synced 2024-11-22 03:19:53 -08:00
Compare commits
No commits in common. "ebc8587e6fbe86fc6e6be8b00081cd2a4458a73f" and "24f3c251422a05d93d62adeaf21882a132d33ff5" have entirely different histories.
ebc8587e6f
...
24f3c25142
117
ani-cli
117
ani-cli
@ -15,6 +15,7 @@ SILENT=0
|
||||
|
||||
player_fn="mpv"
|
||||
playable="\.mp4|\.mkv|\.ts|\.mp3|\.webm"
|
||||
prog="ani-cli"
|
||||
c_red="\033[1;31m"
|
||||
c_green="\033[1;32m"
|
||||
c_yellow="\033[1;33m"
|
||||
@ -23,6 +24,17 @@ c_magenta="\033[1;35m"
|
||||
c_cyan="\033[1;36m"
|
||||
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() {
|
||||
((SILENT != 1)) && printf "$c_red%s$c_reset\n" "$*" >&2
|
||||
exit 1
|
||||
@ -47,8 +59,8 @@ check_input() {
|
||||
fi
|
||||
}
|
||||
|
||||
# get the download page url
|
||||
get_dpage_link() {
|
||||
# get the download page url
|
||||
anime_id=$1
|
||||
ep_no=$2
|
||||
|
||||
@ -143,22 +155,17 @@ run_stmt() {
|
||||
|
||||
# Return number of matches for anime/episode in db
|
||||
check_db() {
|
||||
case "$1" in
|
||||
directory)
|
||||
if [[ "$1" == "directory" ]]; then
|
||||
stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2';"
|
||||
;;
|
||||
file)
|
||||
elif [[ "$1" == "file" ]]; then
|
||||
stmt="SELECT DISTINCT COUNT(*) FROM file_history WHERE directory = '$2' AND filename = '$3';"
|
||||
;;
|
||||
search)
|
||||
stmt="SELECT DISTINCT COUNT(*) FROM search_history WHERE anime_name = '$2';"
|
||||
;;
|
||||
watch | sync)
|
||||
stmt="SELECT DISTINCT COUNT(*) FROM watch_history WHERE anime_name = '$2' AND episode_number = '$3';"
|
||||
;;
|
||||
esac
|
||||
elif [[ "$2" == "search" ]]; then
|
||||
stmt="SELECT DISTINCT COUNT(*) FROM search_history WHERE anime_name = '$1';"
|
||||
else
|
||||
stmt="SELECT DISTINCT COUNT(*) FROM watch_history WHERE anime_name = '$1' AND episode_number = '$2';"
|
||||
fi
|
||||
res=$(run_stmt "$stmt")
|
||||
return "$res"
|
||||
return $res
|
||||
}
|
||||
|
||||
# return true (0) if $source_dt > $target_dt
|
||||
@ -176,37 +183,31 @@ check_date() {
|
||||
update_date() {
|
||||
datetime=$(date +'%Y-%m-%d %H:%M:%S')
|
||||
stmt=""
|
||||
case "$1" in
|
||||
directory)
|
||||
lg "UPDATING FILE_HISTORY for with directory: directory='$2', filename='DIRECTORY', search_date='$datetime'"
|
||||
if [[ "$1" == "directory" ]]; then
|
||||
lg "UPDATING FILE_HISTORY: directory='$2', filename='DIRECTORY', search_date='$datetime'"
|
||||
stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';"
|
||||
;;
|
||||
file)
|
||||
lg "UPDATING FILE_HISTORY: directory='$2', filename='$3', watch_date='$datetime'"
|
||||
elif [[ "$1" == "file" ]]; then
|
||||
lg "UPDATING FILE_HISTORY: directory='$2', filename='$3', search_date='$datetime'"
|
||||
stmt="UPDATE file_history SET watch_date = '$datetime' WHERE directory = '$2' and filename = '$3';"
|
||||
;;
|
||||
search)
|
||||
lg "UPDATING SEARCH_HISTORY: anime_name='$2', search_date='$datetime'"
|
||||
stmt="UPDATE search_history SET search_date = '$datetime' WHERE anime_name = '$2';"
|
||||
;;
|
||||
sync)
|
||||
elif [[ "$2" == "search" ]]; then
|
||||
lg "UPDATING SEARCH_HISTORY: anime_name='$1', search_date='$datetime'"
|
||||
stmt="UPDATE search_history SET search_date = '$datetime' WHERE anime_name = '$1';"
|
||||
elif [[ $# -ge 3 ]]; then
|
||||
temp_dt="${3// /:}"
|
||||
[ -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// /:}"
|
||||
lg "Checking if update is needed..."
|
||||
if ! check_date "$hist_dt" "$temp_dt"; then
|
||||
lg "Passed in date is older or same than current date... doing nothing"
|
||||
return 1
|
||||
fi
|
||||
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;"
|
||||
;;
|
||||
watch)
|
||||
lg "UPDATING WATCH_HISTORY: anime_name='$2', episode_number='$3' watch_date='$datetime'"
|
||||
stmt="UPDATE watch_history SET watch_date = '$datetime' WHERE anime_name = '$2' AND episode_number = $3;"
|
||||
;;
|
||||
esac
|
||||
lg "UPDATING watch_history from sync. watch_date -> $temp_dt"
|
||||
stmt="UPDATE watch_history SET watch_date = '$temp_dt' WHERE anime_name = '$1' AND episode_number = $2;"
|
||||
else
|
||||
lg "UPDATING WATCH_HISTORY: anime_name='$1', episode_number='$2' search_date='$datetime'"
|
||||
stmt="UPDATE watch_history SET watch_date = '$datetime' WHERE anime_name = '$1' AND episode_number = $2;"
|
||||
fi
|
||||
wait # in case there's another insert/update still running in background?
|
||||
run_stmt "$stmt"
|
||||
}
|
||||
@ -273,6 +274,7 @@ sync_watch_history() {
|
||||
errs=0
|
||||
while read -r line; do
|
||||
anime_name="${line/ //}"
|
||||
# for each episode of $anime_name on the remote machine, check local
|
||||
while read -r ep; do
|
||||
episode_num=$(awk -F '|' '{print $1}' <<< "$ep")
|
||||
watch_date=$(awk -F '|' '{print $NF}' <<< "$ep")
|
||||
@ -358,7 +360,8 @@ get_directory_data() {
|
||||
find_media() {
|
||||
inp="$1"
|
||||
[ -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
|
||||
if [ -f "$inp" ]; then
|
||||
@ -367,6 +370,7 @@ find_media() {
|
||||
fi
|
||||
|
||||
get_directory_data "$inp"
|
||||
[ -z "$inp" ] && return 1
|
||||
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" \
|
||||
-p "Enter selection" <<< "$inputlist")"
|
||||
@ -404,8 +408,13 @@ find_media() {
|
||||
## main code ##
|
||||
#####################
|
||||
|
||||
# get 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"
|
||||
if [ "$IS_ROFI" -eq 1 ]; then
|
||||
query=$(rofi -dpi "$DPI" -dmenu -l 15 -p "Search Anime:" \
|
||||
@ -420,8 +429,8 @@ get_search_query() {
|
||||
fi
|
||||
}
|
||||
|
||||
# get anime name along with its id
|
||||
search_anime() {
|
||||
# get anime name along with its id
|
||||
lg "NUM ARGS: $#"
|
||||
if [[ $# -gt 1 ]]; then
|
||||
# if multi-word query, concatenate into one string and replace spaces with '-'
|
||||
@ -441,7 +450,9 @@ search_anime() {
|
||||
}
|
||||
|
||||
search_eps() {
|
||||
# get available episodes for anime_id
|
||||
anime_id=$1
|
||||
|
||||
curl -s "$BASE_URL/category/$anime_id" |
|
||||
sed -n -E '
|
||||
/^[[:space:]]*<a href="#" class="active" ep_start/{
|
||||
@ -451,8 +462,8 @@ search_eps() {
|
||||
'
|
||||
}
|
||||
|
||||
# Select anime from query results
|
||||
anime_selection() {
|
||||
# Select anime from query results
|
||||
search_results=$*
|
||||
if [ "$IS_ROFI" -eq 1 ]; then
|
||||
menu=()
|
||||
@ -464,7 +475,7 @@ anime_selection() {
|
||||
menu+="$((cnt + 1)). $anime_id\n"
|
||||
res["$cnt"]="$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"
|
||||
[ -z "$searched" ] && searched="$cnt" || searched="$searched, $cnt"
|
||||
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_c2="$c_blue[$c_cyan%d$c_blue] $c_yellow%s$c_reset\n"
|
||||
count=1
|
||||
while read -r anime_id; do
|
||||
while read anime_id; do
|
||||
# alternating colors for menu
|
||||
[ $((count % 2)) -eq 0 ] &&
|
||||
menu_format_string=$menu_format_string_c1 ||
|
||||
@ -547,7 +558,9 @@ episode_selection() {
|
||||
if [ "$IS_ROFI" -eq 1 ]; then
|
||||
# select episode number for anime
|
||||
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
|
||||
watch_history=""
|
||||
@ -561,9 +574,7 @@ episode_selection() {
|
||||
lg "Episode watch history -> $watch_history"
|
||||
|
||||
# get user choice and set the start and end
|
||||
msg1="Anime Name: $anime_id"
|
||||
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")")
|
||||
msg=$(printf "%s\n%s" "$(generate_span "Anime Name: $anime_id")" "$(generate_span "Range of episodes can be provided as: START_EPISODE - END_EPISODE")")
|
||||
choice=$(
|
||||
seq 1 "$last_ep_number" |
|
||||
rofi -dpi "$DPI" -dmenu -l 12 \
|
||||
@ -673,7 +684,7 @@ stream() {
|
||||
anime_id="${query// /}"
|
||||
[ -z "$anime_id" ] && die "No anime selected or queried"
|
||||
lg "Checking if anime: $anime_id has been searched before..."
|
||||
check_db "search" "$anime_id"
|
||||
check_db "$anime_id" "search"
|
||||
searched="$?"
|
||||
lg "Searched before: $searched"
|
||||
if [ "$searched" -eq 0 ]; then
|
||||
@ -694,6 +705,7 @@ stream() {
|
||||
episode_selection
|
||||
}
|
||||
|
||||
# option parsing
|
||||
parse_args() {
|
||||
# to clear the colors when exited using SIGINT
|
||||
trap "printf '$c_reset'" INT HUP
|
||||
@ -702,8 +714,12 @@ parse_args() {
|
||||
is_download=0
|
||||
download_dir="."
|
||||
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
|
||||
h)
|
||||
help_text
|
||||
exit 0
|
||||
;;
|
||||
d)
|
||||
is_download=1
|
||||
download_dir="$OPTARG"
|
||||
@ -818,7 +834,11 @@ main() {
|
||||
fi
|
||||
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")
|
||||
lg "Most recently watched episode: $ep_choice_start"
|
||||
;;
|
||||
@ -873,6 +893,7 @@ main() {
|
||||
|
||||
check_input
|
||||
|
||||
# plays selected episode(s)
|
||||
for ep in $episodes; do
|
||||
open_episode "$selection_id" "$ep" "$download_dir"
|
||||
done
|
||||
|
Loading…
Reference in New Issue
Block a user