Skip to content

Commit

Permalink
feat(ytdl-mpv)!: improve cache logic and SQL syntax
Browse files Browse the repository at this point in the history
**breaking changes**
no more UNIQUE title constrain, only SELECT title for a
given query with DISTINCT

Flush cache db please!
  • Loading branch information
andros21 committed Dec 10, 2023
1 parent aa57ccf commit 14ab550
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
10 changes: 5 additions & 5 deletions bin/mpvctl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ _getPlaylist() {
# search for track title
local trtitle
trtitle="$(sqlite3 "${db}" \
"select distinct nchapters,title from main where id='${trid}'")"
"SELECT DISTINCT nchapters,title FROM main WHERE id='${trid}'")"
if [ -z "$trtitle" ]; then
local csv
csv=$(
Expand All @@ -138,8 +138,8 @@ _getPlaylist() {
($ICONV -f utf-8 -c -t ascii || true)
)
# cache this single search as relative to a NULL query
sqlite3 "${db}" "insert into main (query,id,title,nchapters,chapters) values (${csv})"
trtitle="$(sqlite3 "${db}" "select nchapters,distinct title from main where id='${trid}'")"
sqlite3 "${db}" "INSERT INTO main (id,title,nchapters,chapters) VALUES (${csv})"
trtitle="$(sqlite3 "${db}" "SELECT DISTINCT nchapters,title FROM main WHERE id='${trid}'")"
fi
else
# searching track title, using ytdl
Expand Down Expand Up @@ -197,10 +197,10 @@ _getChaptersDb() {
trid=${trid:7}
# number of chapters
local nch
nch="$(sqlite3 "${db}" "select nchapters from main where id='${trid}'")"
nch="$(sqlite3 "${db}" "SELECT nchapters FROM main WHERE id='${trid}'")"
if [ "$nch" -gt "0" ]; then
# get chapters list from sqlite3 db
sqlite3 "$db" "select chapters from main where id='${trid}'" |
sqlite3 "$db" "SELECT chapters FROM main WHERE id='${trid}'" |
jq -r '.[]' |
awk '{ print FNR ") " $0 }' |
sed 's/^[0-9])/0&/'
Expand Down
17 changes: 10 additions & 7 deletions bin/ytdl-mpv
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ _loadPlaylist() {
_ytdl_mpvctl load "$1" && _info "Playlist loaded from ... \"$1\""
}
_flushCache() {
rm -fr "$CACHEDIR" && _info "ytdl-mpv cache flushed"
[ -f "${DB}" ] &&
sqlite3 "$DB" "DROP TABLE main" &&
sqlite3 "$DB" "CREATE TABLE main (query STR,id STR,title STR,nchapters INT,chapters STR)" &&
_info "ytdl-mpv cache flushed"
}
_flushHist() {
rm -fr "$HISTORY" && _info "ytdl-mpv search history flushed"
Expand Down Expand Up @@ -222,7 +225,7 @@ _isCachedQuery() {
if [ -f "$DB" ]; then
local count
count="$(sqlite3 "${DB}" \
"select count(*) from main where query='${query}'")"
"SELECT COUNT(*) FROM main WHERE query='${query}'")"
if [[ "$count" -gt 0 ]]; then printf "cached"; fi
fi
}
Expand All @@ -233,22 +236,22 @@ _getCachedQuery() {
local query
query="$1"
sqlite3 "${DB}" \
"select nchapters,title from main where query='${query}'" |
"SELECT nchapters,title FROM main WHERE query='${query}'" |
sed 's/^0|/[##] /;s/\(^[0-9]\+\)|/[\1] /' |
awk '{ print FNR ") " $0 }' |
sed 's/^[0-9])/0&/;s/\[\([0-9]\)\]/[0\1]/'
}

# Get id of yt content from cached table
_getCachedIdQuery() {
local query
local title
query="$1"
title="$2"
# escape quotes
query=$(printf '%s' "$query" | sed "s/'/''/g")
title=$(printf '%s' "$title" | sed "s/'/''/g")
printf '%s' "$(sqlite3 "${DB}" \
"select id from main where query='${query}' and title='${title}'")"
"SELECT id FROM main WHERE query='${query}' AND title='${title}'")"
}

# Cache a query inside main table
Expand All @@ -257,15 +260,15 @@ _cacheQuery() {
query="$1"
# create main table and cache items
sqlite3 "${DB}" \
"create table if not exists main (query str,id str,title str,nchapters int,chapters str,unique(title))"
"CREATE TABLE IF NOT EXISTS main (query STR,id STR,title STR,nchapters INT,chapters STR)"
sqlite3 -separator ',' "${DB}" ".import ${TMPDIR}/${query} main"
}

# Delete a cached query inside main table
_deleteQuery() {
local query
query="$1"
sqlite3 "${DB}" "delete from main where query='${query}'" ||
sqlite3 "${DB}" "DELETE FROM main WHERE query='${query}'" ||
_die "Deleting query ${query} from cache db"
}

Expand Down

0 comments on commit 14ab550

Please sign in to comment.