update code formatting

This commit is contained in:
ksyasuda 2022-01-02 09:53:16 -08:00
parent 37f60ffb21
commit 55470e87e9
4 changed files with 522 additions and 564 deletions

66
ani-cli
View File

@ -34,7 +34,7 @@ c_reset="\033[0m"
help_text() {
while IFS= read -r line; do
printf "%s\n" "$line"
done <<-EOF
done <<- EOF
USAGE: $prog <query>
-h show this help text
-d download episode
@ -92,10 +92,10 @@ search_eps() {
}
check_input() {
[ "$ep_choice_start" -eq "$ep_choice_start" ] 2>/dev/null || die "Invalid number entered"
[ "$ep_choice_start" -eq "$ep_choice_start" ] 2> /dev/null || die "Invalid number entered"
episodes=$ep_choice_start
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
episodes=$(seq $ep_choice_start $ep_choice_end)
fi
@ -124,7 +124,7 @@ get_links() {
get_video_quality() {
get_links
video_quality=$(curl -s get_links "$video_url" | grep -oE "(http|https):\/\/.*com\/cdn.*expiry=[0-9]*"| sort -V | sed 's/amp;//')
video_quality=$(curl -s get_links "$video_url" | grep -oE "(http|https):\/\/.*com\/cdn.*expiry=[0-9]*" | sort -V | sed 's/amp;//')
case $quality in
best)
play_link=$(echo "$video_quality" | sort -V | tail -n 1)
@ -146,7 +146,7 @@ get_video_quality() {
dep_ch() {
for dep; do
if ! command -v "$dep" >/dev/null; then
if ! command -v "$dep" > /dev/null; then
die "Program \"$dep\" not found. Please install it."
fi
done
@ -241,17 +241,17 @@ insert_history() {
sync_search_history() {
cnt=0
while read -r line; do
anime_name=$(awk -F '|' '{print $2}' <<<"$line")
anime_name=$(awk -F '|' '{print $2}' <<< "$line")
res=$(sqlite3 -noheader "$HISTORY_DB" "SELECT COUNT(*) FROM search_history WHERE anime_name = '$anime_name'")
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..."
if ! sqlite3 "$HISTORY_DB" "INSERT INTO search_history(anime_name, search_date) VALUES('$anime_name', '$search_date')"; then
err "Error inserting row $line"
fi
((++cnt))
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"
}
@ -265,14 +265,14 @@ sync_watch_history() {
# for each episode of $anime_name on the remote machine, check local
while read -r ep; do
# log "EP: $ep"
episode_num=$(awk -F '|' '{print $1}' <<<"$ep")
episode_num=$(awk -F '|' '{print $1}' <<< "$ep")
# log "EPISODE NUMBER: $episode_num"
check_db "$anime_name" "$episode_num"
num=$?
log "COUNT for $anime_name - episode $episode_num: $num"
if [[ "$num" -eq 0 ]]; then
log "$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..."
if ! sqlite3 "$HISTORY_DB" "INSERT INTO watch_history(anime_name, episode_number, watch_date) VALUES('$anime_name', '$episode_num', '$watch_date')"; then
err "Error inserting row $ep"
@ -281,8 +281,8 @@ sync_watch_history() {
else
log "$anime_name - Episode: $episode_num found in the db... skipping"
fi
done <<<"${episodes[@]}"
done <<<"$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT anime_name FROM watch_history")"
done <<< "${episodes[@]}"
done <<< "$(sqlite3 -noheader "$temp_db" "SELECT DISTINCT anime_name FROM watch_history")"
log "Inserted $cnt rows into watch_history table"
}
@ -322,7 +322,7 @@ get_search_query() {
}
# create history file
[ -f "$logfile" ] || : >"$logfile"
[ -f "$logfile" ] || : > "$logfile"
#####################
## Anime selection ##
@ -340,7 +340,7 @@ anime_selection() {
idx=$((count - 1))
res["$idx"]="$anime_id"
count=$((count + 1))
done <<-EOF
done <<- EOF
$search_results
EOF
@ -389,7 +389,7 @@ anime_selection() {
menu_format_string=$menu_format_string_c2
printf "$menu_format_string" "$count" "$anime_id"
count=$((count+1))
count=$((count + 1))
done <<< "$search_results"
# User input
@ -409,7 +409,7 @@ anime_selection() {
fi
fi
# Check if input is a number
[ "$choice" -eq "$choice" ] 2>/dev/null || die "Invalid number entered"
[ "$choice" -eq "$choice" ] 2> /dev/null || die "Invalid number entered"
# Select respective anime_id
count=1
@ -419,7 +419,7 @@ anime_selection() {
break
fi
count=$((count + 1))
done <<-EOF
done <<- EOF
$search_results
EOF
@ -431,7 +431,7 @@ anime_selection() {
[ -z "$selection_id" ] && die "Invalid number entered"
read -r last_ep_number <<-EOF
read -r last_ep_number <<- EOF
$(search_eps "$selection_id")
EOF
}
@ -528,7 +528,7 @@ open_episode() {
fi
get_video_quality
status_code=$(curl -s -I get_video_quality "$play_link" | head -n 1|cut -d ' ' -f2)
status_code=$(curl -s -I get_video_quality "$play_link" | head -n 1 | cut -d ' ' -f2)
log "Status code: $status_code"
if [ "$is_download" -eq 0 ]; then
@ -548,12 +548,12 @@ open_episode() {
mkdir -p "$anime_id" || die "Could not create directory"
cd "$anime_id" || die "Could not enter subdirectory $ddir/$anime_id"
# ffmpeg -i "$play_link" -c copy "${anime_id}-${episode}.mkv" >/dev/null 2>&1 &&
if command -v "notify-send" >/dev/null; then
ffmpeg -i "$play_link" -c copy "${episode}.mkv" >/dev/null 2>&1 &&
if command -v "notify-send" > /dev/null; then
ffmpeg -i "$play_link" -c copy "${episode}.mkv" > /dev/null 2>&1 &&
notify-send -i "$ANIWRAPPER_ICON_PATH" "Download complete for ${anime_id//-/ } - Episode: $episode" ||
notify-send -i "$MAISAN_ICON_PATH" "Download failed for ${anime_id//-/ } - Episode: $episode. Please retry or check your internet connection"
else
ffmpeg -i "$play_link" -c copy "${episode}.mkv" >/dev/null 2>&1 &&
ffmpeg -i "$play_link" -c copy "${episode}.mkv" > /dev/null 2>&1 &&
printf "${c_green}Downloaded complete for %s - Episode: %s${c_reset}\n" "${anime_id//-/ }" "$episode" ||
printf "${c_red}Download failed for %s - Episode: %s, please retry or check your internet connection${c_reset}\n" "${anime_id//-/ }" "$episode"
fi
@ -639,7 +639,7 @@ shift $((OPTIND - 1))
########
case $scrape in
query)
query)
get_search_query "$*"
searched=0
if [ $# -eq 1 ]; then
@ -666,7 +666,7 @@ query)
fi
episode_selection
;;
history)
history)
stmt="SELECT DISTINCT anime_name FROM watch_history ORDER BY watch_date DESC"
search_results=$(printf "%s\n" "$stmt" | sqlite3 -noheader "$HISTORY_DB")
[ -z "$search_results" ] && die "History is empty"
@ -683,8 +683,8 @@ history)
ep_choice_start=$(run_stmt "$stmt")
log "Most recently watched episode: $ep_choice_start"
;;
playlist)
lines=$(wc -l <"$playlist_file")
playlist)
lines=$(wc -l < "$playlist_file")
log "Num lines in playlist: " "$lines"
if [[ "$lines" -eq 0 ]]; then
get_search_query "$*"
@ -703,14 +703,14 @@ playlist)
episodes=("$selection_id")
ep_choice_start="${line[1]}"
ep_choice_end=""
read -r last_ep_number <<-EOF
read -r last_ep_number <<- EOF
$(search_eps "$selection_id")
EOF
[ "$VERBOSE" -eq 1 ] && printf "Anime: %s Episode: %d\n" "$episodes" "$ep_choice_start"
[ "$VERBOSE" -eq 1 ] && printf "Episodes: %s\n" "${episodes[@]}"
fi
;;
add)
add)
get_search_query "$*"
search_results=$(search_anime "$query")
[ -z "$search_results" ] && die "No search results found"
@ -718,7 +718,7 @@ add)
[ $? -ne 0 ] && die "No anime selection found"
episode_selection
;;
sync)
sync)
printf "%s" "Enter username for remote user: "
read -r username
printf "%s" "Enter host for remote user: "
@ -753,11 +753,11 @@ sync)
esac
{ # checking input
[ "$ep_choice_start" -eq "$ep_choice_start" ] 2>/dev/null || die "Invalid number entered"
[ "$ep_choice_start" -eq "$ep_choice_start" ] 2> /dev/null || die "Invalid number entered"
episodes=$ep_choice_start
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
episodes=$(seq $ep_choice_start $ep_choice_end)
fi
@ -768,7 +768,7 @@ for ep in $episodes; do
if [[ "$is_add" -eq 1 ]]; then
log "ID: $selection_id"
log "EPISODES: $episodes"
printf "%s\n" "$selection_id $ep" >>"$playlist_file"
printf "%s\n" "$selection_id $ep" >> "$playlist_file"
log "Added to playlist file"
else
open_episode "$selection_id" "$ep" "$download_dir"
@ -813,7 +813,7 @@ while :; do
printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " "$last_ep_number"
read -r episode
printf "$c_reset"
[ "$episode" -eq "$episode" ] 2>/dev/null || die "Invalid number entered"
[ "$episode" -eq "$episode" ] 2> /dev/null || die "Invalid number entered"
;;
r) ;;

View File

@ -22,7 +22,6 @@ IS_DOWNLOAD=0
quit="6. Quit"
options="1. Stream|2. Download|3. Continue|4. Play from File|5. Sync History|$quit"
#############
# Functions #
#############
@ -162,13 +161,13 @@ playlist_mode() {
lines="$lines|$((cnt + 1)). $line"
fi
((cnt++))
done <"$PLAYLIST_FILE"
done < "$PLAYLIST_FILE"
choice=$(rofi -dmenu -l 12 \
-sep '|' \
-config "$CFG_DIR/$CFG_FILE" -p "Choose episode to delete:" \
<<<"${lines[*]}")
<<< "${lines[*]}")
log "choice: $choice"
selection=$(awk '{print $1}' <<<"$choice")
selection=$(awk '{print $1}' <<< "$choice")
idx=${selection//\./}
log "index selected: $idx"
log "deleting entry: $idx"
@ -196,7 +195,7 @@ play_file() {
if [[ "$VERBOSE" -eq 1 ]]; then
$PLAYER_CMD "$1"
else
$PLAYER_CMD "$1" &>/dev/null &
$PLAYER_CMD "$1" &> /dev/null &
fi
exit $?
else
@ -232,7 +231,6 @@ find_videos() {
fi
}
########
# Main #
########
@ -382,4 +380,3 @@ case "$selection" in
;;
esac

51
db.sh
View File

@ -9,51 +9,12 @@ log() {
printf "%s\n" "$1"
}
sync_search_history() {
cnt=0
temp_db="/tmp/aniwrapper_tmp_history.sqlite3"
while read -r line; do
anime_name=$(awk -F '|' '{print $2}' <<<"$line")
res=$(sqlite3 -noheader "$DIR/$DB" <<<"SELECT anime_name FROM search_history WHERE anime_name = '$anime_name'")
if [[ "${res/ //}" == "" ]]; then
log "Adding $line to search history..."
search_date=$(awk -F '|' '{print $3}' <<<"$line")
sqlite3 "$DIR/$DB" <<<"INSERT INTO search_history(anime_name, search_date) VALUES('$anime_name', '$search_date')"
if [[ "$?" -ne 0 ]]; then
log "Error inserting row $line"
fi
((++cnt))
fi
done <<<"$(sqlite3 -noheader "$temp_db" <<<"SELECT DISTINCT * FROM search_history")"
log "Inserted $cnt rows into search_history table"
}
sync_watch_history() {
cnt=0
temp_db="/tmp/aniwrapper_tmp_history.sqlite3"
while read -r line; do
anime_name=$(awk -F '|' '{print $2}' <<<"$line")
res=$(sqlite3 -noheader "$DIR/$DB" <<<"SELECT anime_name FROM watch_history WHERE anime_name = '$anime_name'")
if [[ "${res/ //}" == "" ]]; then
log "Adding $line to watch history..."
episode_num=$(awk -F '|' '{print $3}' <<<"$line")
watch_date=$(awk -F '|' '{print $NF}' <<<"$line")
sqlite3 "$DIR/$DB" <<<"INSERT INTO watch_history(anime_name, episode_number, watch_date) VALUES('$anime_name', '$episode_num', '$watch_date')"
if [[ "$?" -ne 0 ]]; then
log "Error inserting row $line"
fi
((++cnt))
fi
done <<<"$(sqlite3 -noheader "$temp_db" <<<"SELECT DISTINCT * FROM watch_history")"
log "Inserted $cnt rows into watch_history table"
}
while getopts 'cdrqCs' OPT; do
case "$OPT" in
c)
log "Creating database..."
sqlite3 "$DIR"/"$DB" <sql/search_history_tbl.sql
sqlite3 "$DIR"/"$DB" <sql/watch_history_tbl.sql
sqlite3 "$DIR"/"$DB" < sql/search_history_tbl.sql
sqlite3 "$DIR"/"$DB" < sql/watch_history_tbl.sql
log "Created database..."
;;
d)
@ -69,8 +30,8 @@ while getopts 'cdrqCs' OPT; do
mkdir -p "$DIR"
log "Database deleted..."
log "Creating database..."
sqlite3 "$DIR"/"$DB" <sql/search_history_tbl.sql
sqlite3 "$DIR"/"$DB" <sql/watch_history_tbl.sql
sqlite3 "$DIR"/"$DB" < sql/search_history_tbl.sql
sqlite3 "$DIR"/"$DB" < sql/watch_history_tbl.sql
log "Created database..."
;;
q)
@ -82,10 +43,10 @@ while getopts 'cdrqCs' OPT; do
log "Cleaning up database..."
stmt="DELETE FROM search_history WHERE anime_name IS NULL OR anime_name = ''"
log "Cleaning up search history..."
sqlite3 "$DIR/$DB" <<<"$stmt"
sqlite3 "$DIR/$DB" <<< "$stmt"
stmt="DELETE FROM watch_history WHERE anime_name IS NULL OR anime_name = '' OR episode_number = 0"
log "Cleaning up watch history..."
sqlite3 "$DIR/$DB" <<<"$stmt"
sqlite3 "$DIR/$DB" <<< "$stmt"
;;
s)
printf "%s" "Enter connection string for remote user in the form user@host: "

View File

@ -41,8 +41,8 @@ run_setup() {
if [[ ! -f "$DIR"/"$DB" ]]; then
# Create the DB if not exists
log "Creating history database..."
sqlite3 "$DIR"/"$DB" <sql/watch_history_tbl.sql
sqlite3 "$DIR"/"$DB" <sql/search_history_tbl.sql
sqlite3 "$DIR"/"$DB" < sql/watch_history_tbl.sql
sqlite3 "$DIR"/"$DB" < sql/search_history_tbl.sql
log "History database created..."
fi