fix scraping

This commit is contained in:
ksyasuda 2022-01-07 09:24:13 -08:00
parent 016263cb69
commit 5e5d004728
2 changed files with 108 additions and 119 deletions

181
ani-cli
View File

@ -46,14 +46,14 @@ err() {
printf "$c_red%s$c_reset\n" "$*" >&2 printf "$c_red%s$c_reset\n" "$*" >&2
} }
log() { logger() {
# prints passed in args to stdout if $VERBOSE is set to 1 # prints passed in args to stdout if $VERBOSE is set to 1
[ "$VERBOSE" -eq 1 ] && printf "%s\n" "$*" >&2 [ "$VERBOSE" -eq 1 ] && printf "%s\n" "$*" >&2
} }
search_anime() { search_anime() {
# get anime name along with its id # get anime name along with its id
log "NUM ARGS: $#" logger "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 '-'
search="$*" search="$*"
@ -62,7 +62,7 @@ search_anime() {
# if one word, remove leading or trailing whitespace # if one word, remove leading or trailing whitespace
search="${1// /}" search="${1// /}"
fi fi
log "Search Query: $search" logger "Search Query: $search"
titlepattern='<a href="/category/' titlepattern='<a href="/category/'
curl -s "$BASE_URL//search.html" \ curl -s "$BASE_URL//search.html" \
-G \ -G \
@ -86,9 +86,9 @@ search_eps() {
} }
check_input() { check_input() {
log "Checking input" logger "Checking input"
log "EP START: $ep_choice_start" logger "EP START: $ep_choice_start"
log "EP END: $ep_choice_end" logger "EP END: $ep_choice_end"
[ "$ep_choice_start" -eq "$ep_choice_start" ] 2> /dev/null || die "Invalid number entered: $ep_choice_start" [ "$ep_choice_start" -eq "$ep_choice_start" ] 2> /dev/null || die "Invalid number entered: $ep_choice_start"
episodes=$ep_choice_start episodes=$ep_choice_start
if [ -n "$ep_choice_end" ]; then if [ -n "$ep_choice_end" ]; then
@ -112,39 +112,26 @@ get_dpage_link() {
fi fi
printf '%s' "$anime_page" | printf '%s' "$anime_page" |
sed -n -E ' sed -n -E 's/.*rel="13" data-video="([^"]*)".*/\1/p' | cut -d/ -f5
/^[[:space:]]*<li class="dowloads">/{
s/.*href="([^"]*)".*/\1/p
q
}'
} }
get_video_quality() { get_video_quality() {
dpage_url=$1 dpage_url=$1
video_links=$(curl -s "$sb_url" | sed -n -E 's/.*a href="#" onclick="download_video([^"]*)".*/\1/p' | head -1) video_links=$(curl -s "https://sbplay2.com/d/$dpage_url" | sed -n -E 's/.*a href="#" onclick="download_video([^"]*)".*/\1/p' | head -1)
logger "VIDEO LINKS: $video_links" 1> /dev/stderr
m=$(printf "%s" "$quality" | cut -c1) m=$(printf "%s" "$quality" | cut -c1)
i=$(printf "%s" "$video_links" | cut -d\' -f2)
h=$(printf "%s" "$video_links" | cut -d\' -f6) h=$(printf "%s" "$video_links" | cut -d\' -f6)
video_link=$(curl -s "https://sbplay2.com/dl?op=download_orig&id=$i&mode=$m&hash=$h" | sed -n -E 's/<a href="([^"]*.mp4)".*/\1/p') logger "constructed video link -> https://sbplay2.com/dl?op=download_orig&id=$dpage_url&mode=$m&hash=$h" 1> /dev/stderr
video_link=$(curl -s "https://sbplay2.com/dl?op=download_orig&id=$dpage_url&mode=$m&hash=$h" | sed -n -E 's/<a href="([^"]*.mp4)".*/\1/p')
if [ -z "$video_link" ]; then if [ -z "$video_link" ]; then
err "$quality quality doesn't exist, falling back to normal quality" err "$quality quality doesn't exist, falling back to normal quality"
video_link=$(curl -s "https://sbplay2.com/dl?op=download_orig&id=$i&mode=n&hash=$h" | sed -n -E 's/<a href="([^"]*.mp4)".*/\1/p') video_link=$(curl -s "https://sbplay2.com/dl?op=download_orig&id=$dpage_url&mode=n&hash=$h" | sed -n -E 's/<a href="([^"]*.mp4)".*/\1/p')
fi fi
printf '%s' "$video_link" printf '%s' "$video_link"
} }
get_links() {
dpage_url="$1"
sb_url=$(curl -s "$dpage_url" |
sed -n -E 's/^[[:space:]]*href="([^"]*)".*/\1/p' | grep -oE ".*sbplay.*")
video_url=$(get_video_quality "$sb_url")
printf '%s' "$video_url"
}
dep_ch() { dep_ch() {
for dep; do for dep; do
if ! command -v "$dep" > /dev/null; then if ! command -v "$dep" > /dev/null; then
@ -155,9 +142,9 @@ dep_ch() {
check_anime_name() { check_anime_name() {
# Check to make sure passed in name is not empty # Check to make sure passed in name is not empty
log "VAR: $1" logger "VAR: $1"
if [[ "$1" == "" ]] || [[ "$1" == " " ]] || [[ "$1" == "\n" ]]; then if [[ "$1" == "" ]] || [[ "$1" == " " ]] || [[ "$1" == "\n" ]]; then
log "Passed in name is nothing" logger "Passed in name is nothing"
return 1 return 1
fi fi
return 0 return 0
@ -176,7 +163,7 @@ check_db() {
# args: # args:
# $1: anime name: str # $1: anime name: str
# $2: either 'search' or 'watch' for which db to query # $2: either 'search' or 'watch' for which db to query
log "check_db $*" 1> /dev/stderr logger "check_db $*" 1> /dev/stderr
if [[ "$1" == "directory" ]]; then if [[ "$1" == "directory" ]]; then
stmt="SELECT DISTINCT COUNT(*) \ stmt="SELECT DISTINCT COUNT(*) \
FROM file_history \ FROM file_history \
@ -197,7 +184,7 @@ check_db() {
AND episode_number = '$2';" AND episode_number = '$2';"
fi fi
res=$(run_stmt "$stmt") res=$(run_stmt "$stmt")
log "check_db result: $res" logger "check_db result: $res"
return "$res" return "$res"
} }
@ -206,21 +193,21 @@ update_date() {
datetime=$(date +'%Y-%m-%d %H:%M:%S') datetime=$(date +'%Y-%m-%d %H:%M:%S')
stmt="" stmt=""
if [[ "$1" == "file" ]]; then if [[ "$1" == "file" ]]; then
log "UPDATING FILE_HISTORY: directory='$2', filename='$3', search_date='$datetime'" logger "UPDATING FILE_HISTORY: directory='$2', filename='$3', search_date='$datetime'"
stmt="UPDATE file_history SET watch_date = '$datetime' \ stmt="UPDATE file_history SET watch_date = '$datetime' \
WHERE directory = '$2' and filename = '$3';" WHERE directory = '$2' and filename = '$3';"
elif [[ "$2" == "search" ]]; then elif [[ "$2" == "search" ]]; then
log "UPDATING SEARCH_HISTORY: anime_name='$1', search_date='$datetime'" logger "UPDATING SEARCH_HISTORY: anime_name='$1', search_date='$datetime'"
stmt="UPDATE search_history SET search_date = '$datetime' \ stmt="UPDATE search_history SET search_date = '$datetime' \
WHERE anime_name = '$1';" WHERE anime_name = '$1';"
elif [[ $# -ge 3 ]]; then elif [[ $# -ge 3 ]]; then
[ -z "$3" ] && return 1 [ -z "$3" ] && return 1
log "UPDATING watch_history from sync. watch_date -> $3" logger "UPDATING watch_history from sync. watch_date -> $3"
stmt="UPDATE watch_history SET watch_date = '$3' \ stmt="UPDATE watch_history SET watch_date = '$3' \
WHERE anime_name = '$1' \ WHERE anime_name = '$1' \
AND episode_number = $2;" AND episode_number = $2;"
else else
log "UPDATING WATCH_HISTORY: anime_name='$1', episode_number='$2' search_date='$datetime'" logger "UPDATING WATCH_HISTORY: anime_name='$1', episode_number='$2' search_date='$datetime'"
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;"
@ -231,10 +218,10 @@ update_date() {
insert_history() { insert_history() {
# inserts into search/watch history db # inserts into search/watch history db
# check the anime_name/id # check the anime_name/id
log "BEGIN: insert_history function" logger "BEGIN: insert_history function"
if [[ ! "$1" == "file" ]]; then if [[ ! "$1" == "file" ]]; then
if ! check_anime_name "$1"; then if ! check_anime_name "$1"; then
log "ERROR: Anime name is none... exiting" logger "ERROR: Anime name is none... exiting"
return 1 return 1
fi fi
fi fi
@ -243,16 +230,16 @@ insert_history() {
res="$?" res="$?"
if [[ $res -gt 0 ]]; then if [[ $res -gt 0 ]]; then
if [[ "$1" == "file" ]]; then if [[ "$1" == "file" ]]; then
log "Already in file history db... updaing watch_date" logger "Already in file history db... updaing watch_date"
elif [[ "$2" == "search" ]]; then elif [[ "$2" == "search" ]]; then
log "Already in search db... Updating search_date" logger "Already in search db... Updating search_date"
else else
log "Already in watch db... Updating watch_date" logger "Already in watch db... Updating watch_date"
fi fi
update_date "$@" update_date "$@"
else else
if [[ "$1" == "file" ]]; then if [[ "$1" == "file" ]]; then
log "inserting $2 into file_history..." logger "inserting $2 into file_history..."
stmt="INSERT INTO file_history(directory, filename, watch_date) \ stmt="INSERT INTO file_history(directory, filename, watch_date) \
VALUES('$2', '$3', '$datetime');" VALUES('$2', '$3', '$datetime');"
elif [[ "$2" == "search" ]]; then elif [[ "$2" == "search" ]]; then
@ -265,7 +252,7 @@ insert_history() {
fi fi
run_stmt "$stmt" run_stmt "$stmt"
fi fi
log "END: insert_history function" logger "END: insert_history function"
} }
sync_search_history() { sync_search_history() {
@ -275,14 +262,14 @@ sync_search_history() {
res=$(sqlite3 -noheader "$HISTORY_DB" "SELECT COUNT(*) FROM search_history WHERE anime_name = '$anime_name'") res=$(sqlite3 -noheader "$HISTORY_DB" "SELECT COUNT(*) FROM search_history WHERE anime_name = '$anime_name'")
if [[ "$res" -eq 0 ]]; then if [[ "$res" -eq 0 ]]; then
search_date=$(awk -F '|' '{print $3}' <<< "$line") search_date=$(awk -F '|' '{print $3}' <<< "$line")
log "Adding ($anime_name|$search_date) to search history..." logger "Adding ($anime_name|$search_date) to search history..."
if ! sqlite3 "$HISTORY_DB" "INSERT INTO search_history(anime_name, search_date) VALUES('$anime_name', '$search_date')"; then if ! sqlite3 "$HISTORY_DB" "INSERT INTO search_history(anime_name, search_date) VALUES('$anime_name', '$search_date')"; then
err "Error inserting row $line" err "Error inserting row $line"
fi fi
((++cnt)) ((++cnt))
fi fi
done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT * FROM search_history")" done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT * FROM search_history")"
log "Inserted $cnt rows into search_history table" logger "Inserted $cnt rows into search_history table"
} }
sync_watch_history() { sync_watch_history() {
@ -290,27 +277,27 @@ sync_watch_history() {
while read -r line; do while read -r line; do
# anime_name=$(awk -F '|' '{print $2}' <<<"$line") # anime_name=$(awk -F '|' '{print $2}' <<<"$line")
anime_name="${line/ //}" anime_name="${line/ //}"
log "ANIME: $anime_name" logger "ANIME: $anime_name"
episodes=$(sqlite3 -list -noheader "$temp_db" "SELECT episode_number, watch_date FROM watch_history WHERE anime_name = '$anime_name'") episodes=$(sqlite3 -list -noheader "$temp_db" "SELECT episode_number, watch_date FROM watch_history WHERE anime_name = '$anime_name'")
# for each episode of $anime_name on the remote machine, check local # for each episode of $anime_name on the remote machine, check local
while read -r ep; do while read -r ep; do
# log "ROW: $ep" # logger "ROW: $ep"
episode_num=$(awk -F '|' '{print $1}' <<< "$ep") episode_num=$(awk -F '|' '{print $1}' <<< "$ep")
check_db "$anime_name" "$episode_num" check_db "$anime_name" "$episode_num"
num=$? num=$?
log "COUNT for $anime_name - episode $episode_num: $num" logger "COUNT for $anime_name - episode $episode_num: $num"
if [[ "$num" -eq 0 ]]; then if [[ "$num" -eq 0 ]]; then
log "$anime_name - E$episode_num NOT IN DB" logger "$anime_name - E$episode_num NOT IN DB"
watch_date=$(awk -F '|' '{print $NF}' <<< "$ep") watch_date=$(awk -F '|' '{print $NF}' <<< "$ep")
log "Adding ($anime_name|$episode_num|$watch_date) to watch history..." logger "Adding ($anime_name|$episode_num|$watch_date) to watch history..."
insert_history "$anime_name" "$episode_num" "$watch_date" insert_history "$anime_name" "$episode_num" "$watch_date"
((++cnt)) ((++cnt))
else else
log "$anime_name - Episode: $episode_num found in the db... skipping" logger "$anime_name - Episode: $episode_num found in the db... skipping"
fi fi
done <<< "${episodes[@]}" done <<< "${episodes[@]}"
done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT anime_name FROM watch_history")" done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT anime_name FROM watch_history")"
log "Inserted $cnt rows into watch_history table" logger "Inserted $cnt rows into watch_history table"
} }
##################### #####################
@ -319,20 +306,20 @@ sync_watch_history() {
# opens the passed in file with $PLAYER_CMD # opens the passed in file with $PLAYER_CMD
play_file() { play_file() {
log "Checking if file is playable" logger "Checking if file is playable"
if [[ "$1" =~ ($playable)$ ]]; then if [[ "$1" =~ ($playable)$ ]]; then
log "File is playable..." logger "File is playable..."
filename=$(grep -oE '[^/]*$' <<< "$1") filename=$(grep -oE '[^/]*$' <<< "$1")
directory=$(sed -E "s/\/$filename//" <<< "$1") directory=$(sed -E "s/\/$filename//" <<< "$1")
log "FILENAME: $filename" logger "FILENAME: $filename"
log "DIRECTORY: $directory" logger "DIRECTORY: $directory"
insert_history "file" "$directory" "$filename" & insert_history "file" "$directory" "$filename" &
if [[ "$1" =~ .mp3 ]]; then if [[ "$1" =~ .mp3 ]]; then
log ".mp3 file found... playing without video" logger ".mp3 file found... playing without video"
log "MPV COMMAND: $PLAYER_CMD --no-video $1" logger "MPV COMMAND: $PLAYER_CMD --no-video $1"
$PLAYER_CMD --no-video "$1" $PLAYER_CMD --no-video "$1"
else else
log "MPV COMMAND: $PLAYER_CMD $1" logger "MPV COMMAND: $PLAYER_CMD $1"
$PLAYER_CMD "$1" $PLAYER_CMD "$1"
fi fi
exit $? exit $?
@ -351,14 +338,14 @@ generate_inputlist() {
fi fi
[ "$outstr" = "" ] && outstr="$directory" || outstr="$outstr|$directory" [ "$outstr" = "" ] && outstr="$directory" || outstr="$outstr|$directory"
done <<< "$(find "$1" -maxdepth 1 -type d | sed "s|$1/||" | tail -n +2 | sort -V)" done <<< "$(find "$1" -maxdepth 1 -type d | sed "s|$1/||" | tail -n +2 | sort -V)"
log "DIRS: $outstr" 1> /dev/stderr logger "DIRS: $outstr" 1> /dev/stderr
while read -r filename; do while read -r filename; do
if [[ "${filename// /}" == "" ]]; then if [[ "${filename// /}" == "" ]]; then
continue continue
fi fi
[ "$outstr" = "" ] && outstr="$filename" || outstr="$outstr|$filename" [ "$outstr" = "" ] && outstr="$filename" || outstr="$outstr|$filename"
done <<< "$(find "$1" -maxdepth 1 -type f | sed "s|$1/||" | grep -E "$playable$" | sort -V)" done <<< "$(find "$1" -maxdepth 1 -type f | sed "s|$1/||" | grep -E "$playable$" | sort -V)"
log "DIRS + FILES: $outstr" 1> /dev/stderr logger "DIRS + FILES: $outstr" 1> /dev/stderr
outstr="$outstr|Back|Quit" outstr="$outstr|Back|Quit"
printf "%s\n" "$outstr" printf "%s\n" "$outstr"
} }
@ -377,19 +364,19 @@ generate_file_watchedlist() {
# sometimes direetory is empty string due to find parsing # sometimes direetory is empty string due to find parsing
[ -z "$directory" ] && continue [ -z "$directory" ] && continue
if ! check_db "directory" "$search_dir/$directory"; then if ! check_db "directory" "$search_dir/$directory"; then
log "$search_dir/$directory opened before... adding $cnt to list" 1> /dev/stderr logger "$search_dir/$directory opened before... adding $cnt to list" 1> /dev/stderr
[ "$watched" = "" ] && watched="$cnt" || watched="$watched, $cnt" [ "$watched" = "" ] && watched="$cnt" || watched="$watched, $cnt"
fi fi
((++cnt)) ((++cnt))
done <<< "$(find "$search_dir" -maxdepth 1 -type d | sed -E "s|$search_dir/||" | tail -n +2 | sort -V)" done <<< "$(find "$search_dir" -maxdepth 1 -type d | sed -E "s|$search_dir/||" | tail -n +2 | sort -V)"
while read -r filename; do while read -r filename; do
if ! check_db "file" "$search_dir" "$filename"; then if ! check_db "file" "$search_dir" "$filename"; then
log "$filename watched before... adding $cnt to list" 1> /dev/stderr logger "$filename watched before... adding $cnt to list" 1> /dev/stderr
[ "$watched" = "" ] && watched="$cnt" || watched="$watched, $cnt" [ "$watched" = "" ] && watched="$cnt" || watched="$watched, $cnt"
fi fi
((++cnt)) ((++cnt))
done <<< "$(find "$search_dir" -maxdepth 1 -type f | sed "s|$search_dir/||" | grep -E "$playable$" | sort -V)" done <<< "$(find "$search_dir" -maxdepth 1 -type f | sed "s|$search_dir/||" | grep -E "$playable$" | sort -V)"
log "WATCHED LIST: $watched" 1> /dev/stderr logger "WATCHED LIST: $watched" 1> /dev/stderr
printf "%s\n" "$watched" printf "%s\n" "$watched"
} }
@ -398,7 +385,7 @@ find_media() {
inp="$1" inp="$1"
[ -z "$inp" ] && inp="/" [ -z "$inp" ] && inp="/"
# workaround to allow logging w/o affecting return output # workaround to allow logging w/o affecting return output
log "INPUT DIR: $inp" 1> /dev/stderr logger "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
@ -409,7 +396,7 @@ find_media() {
span=$(generate_span "Current directory: $inp") span=$(generate_span "Current directory: $inp")
inputlist=$(generate_inputlist "$inp") inputlist=$(generate_inputlist "$inp")
watched_files=$(generate_file_watchedlist "$inp") watched_files=$(generate_file_watchedlist "$inp")
log "watched files -> $watched_files" logger "watched files -> $watched_files"
selection=$(rofi -dmenu -only-match -config "$ROFI_CFG" \ selection=$(rofi -dmenu -only-match -config "$ROFI_CFG" \
-l 13 -i -sep '|' -mesg "$span" -a "$watched_files" \ -l 13 -i -sep '|' -mesg "$span" -a "$watched_files" \
-p "Enter selection" <<< "${inputlist[@]}") -p "Enter selection" <<< "${inputlist[@]}")
@ -445,7 +432,7 @@ get_search_query() {
# Remove the id from the query # Remove the id from the query
query="${query//[1-9]*\. /}" query="${query//[1-9]*\. /}"
# query="${query// /}" # query="${query// /}"
log "Query: $query" logger "Query: $query"
elif [ "$is_rofi" -eq 0 ]; then elif [ "$is_rofi" -eq 0 ]; then
printf "Search Anime: " printf "Search Anime: "
read -r query read -r query
@ -486,10 +473,10 @@ anime_selection() {
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
log "ANIME: $anime" logger "ANIME: $anime"
check_db "$anime" "search" check_db "$anime" "search"
if [[ $? -gt 0 ]]; then if [[ $? -gt 0 ]]; then
log "SEARCHED BEFORE" logger "SEARCHED BEFORE"
if [ -z "$searched" ]; then if [ -z "$searched" ]; then
searched="$cnt" searched="$cnt"
else else
@ -499,7 +486,7 @@ anime_selection() {
((++cnt)) ((++cnt))
done done
log "SEARCHED: $searched" logger "SEARCHED: $searched"
# get the anime from indexed list # get the anime from indexed list
msg="<span foreground='peachpuff' style='italic' size='small' weight='normal'>Query: $query</span>" msg="<span foreground='peachpuff' style='italic' size='small' weight='normal'>Query: $query</span>"
@ -510,7 +497,7 @@ anime_selection() {
-mesg "$msg" -only-match) -mesg "$msg" -only-match)
[ -z "$user_input" ] && return 1 [ -z "$user_input" ] && return 1
if [ $(awk '{ print $NF }' <<< "$user_input") = "Quit" ]; then if [ $(awk '{ print $NF }' <<< "$user_input") = "Quit" ]; then
log "QUITTING" logger "QUITTING"
return 1 return 1
fi fi
@ -541,7 +528,7 @@ anime_selection() {
name="$anime_id" name="$anime_id"
fi fi
log "CHOICE: $choice" logger "CHOICE: $choice"
if [ "$is_rofi" -eq 1 ]; then if [ "$is_rofi" -eq 1 ]; then
# check both choice and name are set # check both choice and name are set
@ -565,7 +552,7 @@ anime_selection() {
EOF EOF
[ -z "$name" ] && name="$anime_id" [ -z "$name" ] && name="$anime_id"
log "NAME: $name" logger "NAME: $name"
insert_history "$name" "search" & insert_history "$name" "search" &
printf "$c_reset" printf "$c_reset"
@ -585,12 +572,12 @@ episode_selection() {
ep_choice_start="1" ep_choice_start="1"
if [ "$is_rofi" -eq 1 ]; then if [ "$is_rofi" -eq 1 ]; then
# select episode number for anime # select episode number for anime
log "Anime ID: $anime_id" logger "Anime ID: $anime_id"
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=$(run_stmt "$stmt") hist=$(run_stmt "$stmt")
# log "HISTORY: ${hist[*]}" # logger "HISTORY: ${hist[*]}"
# Get Watch History for $anime_id as comma separated list # Get Watch History for $anime_id as comma separated list
watch_history="" watch_history=""
@ -614,7 +601,7 @@ episode_selection() {
) )
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 $NF}') ep_choice_end=$(printf '%s\n' "${choice}" | awk '{print $NF}')
log "START: $ep_choice_start | END: $ep_choice_end" logger "START: $ep_choice_start | END: $ep_choice_end"
elif [ $last_ep_number -gt 1 ]; then elif [ $last_ep_number -gt 1 ]; then
[ $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"
@ -624,9 +611,9 @@ episode_selection() {
printf "$c_reset" printf "$c_reset"
fi fi
# check for half episode # check for half episode
log "Checking if selected a half episode" logger "Checking if selected a half episode"
if [ "$(echo "$ep_choice_start" | awk '{ printf substr($0, 1, 1) }')" = "h" ]; then if [ "$(echo "$ep_choice_start" | awk '{ printf substr($0, 1, 1) }')" = "h" ]; then
log "IS A HALF EPISODE" logger "IS A HALF EPISODE"
half_ep=1 half_ep=1
ep_choice_start=$(echo "$ep_choice_start" | awk '{ printf substr($0, 2) }') ep_choice_start=$(echo "$ep_choice_start" | awk '{ printf substr($0, 2) }')
ep_choice_end=$ep_choice_start ep_choice_end=$ep_choice_start
@ -654,7 +641,7 @@ open_episode() {
episode=$episode"-5" episode=$episode"-5"
fi fi
log "Getting data for episode $episode" logger "Getting data for episode $episode"
# Don't update watch history if downloading episode # Don't update watch history if downloading episode
if [ "$is_download" -eq 0 ]; then if [ "$is_download" -eq 0 ]; then
@ -662,7 +649,7 @@ open_episode() {
fi fi
dpage_link=$(get_dpage_link "$anime_id" "$episode") dpage_link=$(get_dpage_link "$anime_id" "$episode")
video_url=$(get_links "$dpage_link") video_url=$(get_video_quality "$dpage_link")
referer_link="https://sbplay2.com" referer_link="https://sbplay2.com"
if [ $half_ep -eq 1 ]; then if [ $half_ep -eq 1 ]; then
@ -672,14 +659,14 @@ open_episode() {
if [ "$is_download" -eq 0 ]; then if [ "$is_download" -eq 0 ]; then
kill "$PID" > /dev/null 2>&1 kill "$PID" > /dev/null 2>&1
log "PLAYING VIDEO: $video_url" logger "PLAYING VIDEO: $video_url"
log "REFERRER: $referer_link" logger "REFERRER: $referer_link"
nohup "$player_fn" --http-header-fields="Referer:$referer_link" "$video_url" > /dev/null 2>&1 & nohup "$player_fn" --http-header-fields="Referer:$referer_link" "$video_url" > /dev/null 2>&1 &
PID=$! PID=$!
printf "${c_green}\nVideo playing" printf "${c_green}\nVideo playing"
else else
log "Downloading episode $episode ..." logger "Downloading episode $episode ..."
log "$video_url" logger "$video_url"
dl_dir="${ddir// /}/$anime_id" dl_dir="${ddir// /}/$anime_id"
# add 0 padding to the episode name # add 0 padding to the episode name
episode=$(printf "%03d" "$episode") episode=$(printf "%03d" "$episode")
@ -700,21 +687,23 @@ open_episode() {
} }
stream() { stream() {
log "Running stream()" logger "Running stream()"
log "args: $*" logger "args: $*"
get_search_query "$*" get_search_query "$*"
searched=0 searched=0
if [ $# -le 1 ]; then if [ $# -le 1 ]; then
# check if anime has been searched before # check if anime has been searched before
anime_id="${query// /}" anime_id="${query// /}"
log "Checking if anime: $anime_id has been searched before..." [ -z "$anime_id" ] && die "No anime selected or queried"
logger "Checking if anime: $anime_id has been searched before..."
check_db "$anime_id" "search" check_db "$anime_id" "search"
searched="$?" searched="$?"
log "Searched before: $searched" logger "Searched before: $searched"
fi fi
if [ "$searched" -eq 0 ]; then if [ "$searched" -eq 0 ]; then
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"
logger "SEARCH RESULTS: $search_results"
if ! anime_selection "$search_results"; then if ! anime_selection "$search_results"; then
die "No anime selection found" die "No anime selection found"
fi fi
@ -752,7 +741,7 @@ while getopts 'hd:Hsvq:c-:f:t:T:' OPT; do
d) d)
is_download=1 is_download=1
download_dir="$OPTARG" download_dir="$OPTARG"
log "DOWNLOAD DIR: $download_dir" logger "DOWNLOAD DIR: $download_dir"
;; ;;
H) H)
scrape=history scrape=history
@ -765,7 +754,7 @@ while getopts 'hd:Hsvq:c-:f:t:T:' OPT; do
;; ;;
q) q)
quality="$OPTARG" quality="$OPTARG"
log "passed in quality: $quality" logger "passed in quality: $quality"
;; ;;
c) c)
is_rofi=0 is_rofi=0
@ -813,14 +802,14 @@ while getopts 'hd:Hsvq:c-:f:t:T:' OPT; do
die "$theme not a valid theme file. Themes: [$THEMES]" die "$theme not a valid theme file. Themes: [$THEMES]"
;; ;;
esac esac
log "Setting theme for ani-cli -> $ROFI_THEME" logger "Setting theme for ani-cli -> $ROFI_THEME"
ROFI_CFG="$CFG_DIR/themes/$ROFI_THEME" ROFI_CFG="$CFG_DIR/themes/$ROFI_THEME"
log "ROFI_CFG: $ROFI_CFG" logger "ROFI_CFG: $ROFI_CFG"
;; ;;
T) T)
ROFI_CFG="$OPTARG" ROFI_CFG="$OPTARG"
[ ! -f "$ROFI_CFG" ] && die "$ROFI_CFG does not exist" [ ! -f "$ROFI_CFG" ] && die "$ROFI_CFG does not exist"
log "CUSTOM ROFI_CFG: $ROFI_CFG" logger "CUSTOM ROFI_CFG: $ROFI_CFG"
;; ;;
*) *)
printf "%s\n" "Invalid option" printf "%s\n" "Invalid option"
@ -847,7 +836,7 @@ case $scrape in
if ! anime_selection "${search_results[@]}"; then if ! anime_selection "${search_results[@]}"; then
die "No anime selection found" die "No anime selection found"
fi fi
log "SELECTION: $selection_id" logger "SELECTION: $selection_id"
stmt="SELECT episode_number \ stmt="SELECT episode_number \
FROM watch_history \ FROM watch_history \
@ -855,7 +844,7 @@ case $scrape in
ORDER BY watch_date DESC \ ORDER BY watch_date DESC \
LIMIT 1" LIMIT 1"
ep_choice_start=$(run_stmt "$stmt") ep_choice_start=$(run_stmt "$stmt")
log "Most recently watched episode: $ep_choice_start" logger "Most recently watched episode: $ep_choice_start"
;; ;;
sync) sync)
printf "%s" "Enter username for remote user: " printf "%s" "Enter username for remote user: "
@ -890,10 +879,10 @@ case $scrape in
exit $? exit $?
;; ;;
file) file)
log "STARTING DIR: $play_dir" logger "STARTING DIR: $play_dir"
[ ! -d "$play_dir" ] && die "$play_dir does not exist" [ ! -d "$play_dir" ] && die "$play_dir does not exist"
video_path=$(find_media "$play_dir") || die video_path=$(find_media "$play_dir") || die
log "VIDEO PATH: $video_path" logger "VIDEO PATH: $video_path"
if [ -z "$video_path" ]; then if [ -z "$video_path" ]; then
die "Something went wrong getting path... path is empty" die "Something went wrong getting path... path is empty"
fi fi
@ -910,7 +899,7 @@ for ep in $episodes; do
done done
if [[ "$is_download" -eq 1 ]]; then if [[ "$is_download" -eq 1 ]]; then
log "Finished downloading episodes: $episodes for $selection_id... exiting" logger "Finished downloading episodes: $episodes for $selection_id... exiting"
exit 0 exit 0
fi fi
@ -961,7 +950,7 @@ while :; do
a) a)
stream stream
episode=$ep_choice_start episode=$ep_choice_start
log "NEW EPISODE: $selection_id - $episode" logger "NEW EPISODE: $selection_id - $episode"
;; ;;
q) q)

View File

@ -27,7 +27,7 @@ theme=default
############# #############
# Functions # # Functions #
############# #############
log() { logger() {
if [[ "$VERBOSE" -eq 1 ]]; then if [[ "$VERBOSE" -eq 1 ]]; then
printf "%s\n" "$*" printf "%s\n" "$*"
fi fi
@ -76,7 +76,7 @@ get_quality() {
printf "%s" "Enter quality [$QUALITIES]: " printf "%s" "Enter quality [$QUALITIES]: "
read -r QUALITY read -r QUALITY
fi fi
log "selected quality: $QUALITY" logger "selected quality: $QUALITY"
} }
# generates a span mesg for rofi given # generates a span mesg for rofi given
@ -100,28 +100,28 @@ while getopts 'vhqcdf:-:t:T:' OPT; do
;; ;;
q) q)
GET_QUALITY=1 GET_QUALITY=1
log "Quality prompt enabled" logger "Quality prompt enabled"
;; ;;
c) c)
IS_ROFI=0 IS_ROFI=0
log "Command-line (ani-cli) mode set" logger "Command-line (ani-cli) mode set"
;; ;;
d) d)
IS_DOWNLOAD=1 IS_DOWNLOAD=1
log "Download flag set..." logger "Download flag set..."
;; ;;
f) f)
IS_PLAY_FROM_FILE=1 IS_PLAY_FROM_FILE=1
play_path="$OPTARG" play_path="$OPTARG"
log "Play from file flag set... skipping main menu" logger "Play from file flag set... skipping main menu"
log "PLAY_PATH: $play_path" logger "PLAY_PATH: $play_path"
;; ;;
t) t)
theme="$OPTARG" theme="$OPTARG"
log "custom theme provided: $theme" logger "custom theme provided: $theme"
case "$theme" in case "$theme" in
aniwrapper) aniwrapper)
log "Default theme chosen... doing nothing" logger "Default theme chosen... doing nothing"
theme=default theme=default
;; ;;
dracula) dracula)
@ -151,14 +151,14 @@ while getopts 'vhqcdf:-:t:T:' OPT; do
IS_CUSTOM_THEME=1 IS_CUSTOM_THEME=1
;; ;;
*) *)
log "Invalid option" logger "Invalid option"
exit 1 exit 1
;; ;;
esac esac
done done
log "CONFIG DIR: $CFG_DIR" logger "CONFIG DIR: $CFG_DIR"
log "ROFI CFG: $CFG_FILE" logger "ROFI CFG: $CFG_FILE"
# Check if command-line flag is set # Check if command-line flag is set
if [[ "$IS_ROFI" -eq 0 ]] && [[ "$IS_DOWNLOAD" -eq 0 ]]; then if [[ "$IS_ROFI" -eq 0 ]] && [[ "$IS_DOWNLOAD" -eq 0 ]]; then
@ -167,7 +167,7 @@ if [[ "$IS_ROFI" -eq 0 ]] && [[ "$IS_DOWNLOAD" -eq 0 ]]; then
elif [[ "$IS_ROFI" -eq 0 ]] && [[ "$IS_DOWNLOAD" -eq 1 ]]; then elif [[ "$IS_ROFI" -eq 0 ]] && [[ "$IS_DOWNLOAD" -eq 1 ]]; then
printf "%s" "Enter download dir: " printf "%s" "Enter download dir: "
read -r dl_dir read -r dl_dir
log "Download dir: $dl_dir" logger "Download dir: $dl_dir"
[ -d "$dl_dir" ] && mkdir -p "$dl_dir" || dir "Error creating directory: $dl_dir" [ -d "$dl_dir" ] && mkdir -p "$dl_dir" || dir "Error creating directory: $dl_dir"
run "-cd $dl_dir" run "-cd $dl_dir"
exit $? exit $?
@ -194,14 +194,14 @@ case "$selection" in
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# streaming # streaming
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
log "Streaming mode" logger "Streaming mode"
run run
;; ;;
2.) 2.)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# download # download
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
log "Download anime" logger "Download anime"
dl_dir=$(rofi -dmenu -config "$CFG_FILE" \ dl_dir=$(rofi -dmenu -config "$CFG_FILE" \
-l 1 -p "Enter download dir:") -l 1 -p "Enter download dir:")
# if dl_dir is none set to current directory # if dl_dir is none set to current directory
@ -214,14 +214,14 @@ case "$selection" in
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# continue # continue
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
log "Continue watching" logger "Continue watching"
run -H run -H
;; ;;
4.) 4.)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# play # play
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
log "Play from file selected" logger "Play from file selected"
IS_PLAY_FROM_FILE=1 IS_PLAY_FROM_FILE=1
msg="Provide a valid path to a directory or leave blank to go with the default: $HOME/Videos/sauce/" msg="Provide a valid path to a directory or leave blank to go with the default: $HOME/Videos/sauce/"
msg="$(printf '%s\n%s\n' "$msg" "The program will begin searching for media files from the supplied directory")" msg="$(printf '%s\n%s\n' "$msg" "The program will begin searching for media files from the supplied directory")"
@ -236,17 +236,17 @@ case "$selection" in
fi fi
;; ;;
5.) 5.)
log "Sync history database" logger "Sync history database"
username=$(rofi -dmenu -config "$CFG_FILE" \ username=$(rofi -dmenu -config "$CFG_FILE" \
-l 1 -p "Enter the username of the remote user:") -l 1 -p "Enter the username of the remote user:")
if [[ -z "$username" ]] || [[ "$username" == "" ]]; then if [[ -z "$username" ]] || [[ "$username" == "" ]]; then
log "No username provided... exiting" logger "No username provided... exiting"
exit 1 exit 1
fi fi
host=$(rofi -dmenu -config "$CFG_FILE" \ host=$(rofi -dmenu -config "$CFG_FILE" \
-l 1 -p "Enter the host for the remote machine (eg 192.168.1.99):") -l 1 -p "Enter the host for the remote machine (eg 192.168.1.99):")
if [[ -z "$host" ]] || [[ "$host" == "" ]]; then if [[ -z "$host" ]] || [[ "$host" == "" ]]; then
log "No host provided... exiting" logger "No host provided... exiting"
exit 1 exit 1
fi fi
port=$(rofi -dmenu -config "$CFG_FILE" \ port=$(rofi -dmenu -config "$CFG_FILE" \
@ -264,10 +264,10 @@ case "$selection" in
fi fi
if [[ "$?" -ne 0 ]]; then if [[ "$?" -ne 0 ]]; then
log "Aniwrapper was unable to sync the databases..." logger "Aniwrapper was unable to sync the databases..."
exit 1 exit 1
else else
log "Databases synced successfully" logger "Databases synced successfully"
quit quit
fi fi
;; ;;
@ -278,7 +278,7 @@ case "$selection" in
quit quit
;; ;;
*) *)
log "Invalid choice..." logger "Invalid choice..."
exit 1 exit 1
;; ;;
esac esac