update readme and fix handling of multiword vs selected search query

This commit is contained in:
ksyasuda
2022-01-01 16:54:32 -08:00
parent bc58d1677a
commit 7b03f71130
2 changed files with 43 additions and 38 deletions

74
ani-cli
View File

@@ -58,7 +58,14 @@ log() {
search_anime() {
# get anime name along with its id
search=${1// /-}
log "NUM ARGS: $#"
if [[ $# -gt 1 ]]; then
search="$*"
search="${search// /-}"
else
search="$1"
fi
# search=${1// /-}
log "Search Query: $search"
titlepattern='<a href="/category/'
curl -s "$BASE_URL//search.html" \
@@ -161,8 +168,11 @@ run_stmt() {
## Database Code ##
#####################
# Return number of matches for anime/episode in db
check_db() {
# Return number of matches for anime/episode in db
# args:
# $1: anime name: str
# $2: either 'search' or 'watch' for which db to query
if [[ "$2" == "search" ]]; then
stmt="SELECT DISTINCT COUNT(*) \
FROM search_history \
@@ -230,16 +240,16 @@ sync_search_history() {
cnt=0
while read -r line; do
anime_name=$(awk -F '|' '{print $2}' <<<"$line")
res=$(sqlite3 -noheader "$HISTORY_DB" <<<"SELECT anime_name FROM search_history WHERE anime_name = '$anime_name'")
if [[ "${res/ //}" == "" ]]; then
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")
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
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"
}
@@ -249,7 +259,7 @@ sync_watch_history() {
# anime_name=$(awk -F '|' '{print $2}' <<<"$line")
anime_name="${line/ //}"
log "ANIME: $anime_name"
episodes=$(sqlite3 -noheader "$temp_db" <<<"SELECT episode_number, watch_date FROM watch_history WHERE anime_name = '$anime_name'")
episodes=$(sqlite3 -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
while read -r ep; do
episode_num=$(awk -F '|' '{print $1}' <<<"$ep")
@@ -261,7 +271,7 @@ sync_watch_history() {
log "NOT IN DB"
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
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"
fi
((++cnt))
@@ -269,7 +279,7 @@ sync_watch_history() {
log "Episode: $episode_num found in the db... skipping"
fi
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"
}
@@ -291,11 +301,9 @@ get_search_query() {
msg="Choose from list of searched anime below, or enter a unique name of an anime to search for"
span="<span foreground='peachpuff' style='italic' size='small' weight='light'>$msg</span>"
if [ -z "$*" ] && [ "$is_rofi" -eq 1 ]; then
query=$(printf "%s\n" "${hist[@]}" |
rofi -dmenu -l 12 -p "Search Anime:" \
query=$(rofi -dmenu -l 12 -p "Search Anime:" \
-mesg "$span" \
-config "$CFG_DIR/${ROFI_CFG}")
-config "$CFG_DIR/${ROFI_CFG}" <<< "${hist[@]}")
# Remove the id from the query
query="${query//[1-9]*\. /}"
# fixes '-' at end of selected anime, but removes spaces from queries
@@ -363,28 +371,26 @@ anime_selection() {
choice="${choice::-1}"
name=$(printf '%s\n' "$user_input" | awk '{print $NF}')
else
menu_format_string='[%d] %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='[%d] %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"
count=1
while read anime_id; do
# alternating colors for menu
[ $((count % 2)) -eq 0 ] &&
menu_format_string=$menu_format_string_c1 ||
menu_format_string=$menu_format_string_c2
count=1
while read anime_id; do
# alternating colors for menu
[ $((count % 2)) -eq 0 ] &&
menu_format_string=$menu_format_string_c1 ||
menu_format_string=$menu_format_string_c2
printf "$menu_format_string" "$count" "$anime_id"
count=$((count+1))
done <<-EOF
$search_results
EOF
printf "$menu_format_string" "$count" "$anime_id"
count=$((count+1))
done <<< "$search_results"
# User input
printf "$c_blue%s$c_green" "Enter number: "
read choice
printf "$c_reset"
name="$anime_id"
# User input
printf "$c_blue%s$c_green" "Enter number: "
read choice
printf "$c_reset"
name="$anime_id"
fi
log "CHOICE: $choice"
@@ -629,7 +635,9 @@ shift $((OPTIND - 1))
case $scrape in
query)
get_search_query "$*"
search_results=$(search_anime "$query")
res=$(check_db "$query" search)
log "Searched before: $res"
search_results=$(search_anime $query)
[ -z "$search_results" ] && die "No search results found"
if ! anime_selection "$search_results"; then
die "No anime selection found"