Skip to content

Commit c96fe19

Browse files
author
SM_SAYEED
committed
'keys debug 3'
1 parent 3d5386c commit c96fe19

File tree

1 file changed

+35
-79
lines changed

1 file changed

+35
-79
lines changed

app.py

Lines changed: 35 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,94 +1677,50 @@ def search():
16771677
return render_template('search_results.html', query=query, materials=materials, clips=clips)
16781678
##############################################################################################################################################################
16791679

1680-
@app.route("/keys", methods=["GET"])
1680+
@app.route("/keys")
16811681
def available_keys():
1682-
props = []
1683-
clip_titles = []
1682+
# --- Materials properties (always list all visible ones) ---
1683+
with sqlite3.connect(DB_NAME) as conn:
1684+
conn.row_factory = sqlite3.Row
1685+
cur = conn.cursor()
1686+
cur.execute("""
1687+
SELECT
1688+
p.slug,
1689+
p.display_name AS title,
1690+
COALESCE((
1691+
SELECT COUNT(*)
1692+
FROM uploads_log u
1693+
WHERE u.property = p.slug
1694+
AND COALESCE(u.storage, 'local') = 'drive'
1695+
), 0) AS count
1696+
FROM properties p
1697+
WHERE COALESCE(p.visible, 1) = 1
1698+
ORDER BY p.display_name
1699+
""")
1700+
props = cur.fetchall()
16841701

1685-
# --- Materials: list every property from `properties`, plus file count from uploads_log ---
1686-
try:
1687-
with sqlite3.connect(DB_NAME) as conn:
1688-
cur = conn.cursor()
1689-
cur.execute("""
1690-
SELECT p.slug, p.title, COALESCE(COUNT(u.filename), 0) AS cnt
1691-
FROM properties p
1692-
LEFT JOIN uploads_log u
1693-
ON u.property = p.slug
1694-
GROUP BY p.slug, p.title
1695-
ORDER BY LOWER(COALESCE(p.title, p.slug))
1696-
""")
1697-
for slug, raw_title, cnt in cur.fetchall():
1698-
title = (raw_title or slug.replace("_", " ").title()).strip()
1699-
props.append({"slug": slug, "title": title, "count": int(cnt or 0)})
1700-
except Exception as e:
1701-
# Fallback if `properties` doesn't exist yet: derive from uploads_log
1702-
app.logger.warning("available_keys: LEFT JOIN failed (%s); falling back.", e)
1703-
try:
1704-
with sqlite3.connect(DB_NAME) as conn:
1705-
cur = conn.cursor()
1706-
cur.execute("""
1707-
SELECT property, COUNT(*) AS cnt
1708-
FROM uploads_log
1709-
WHERE property IS NOT NULL AND TRIM(property) <> ''
1710-
GROUP BY property
1711-
ORDER BY LOWER(property)
1712-
""")
1713-
for prop, cnt in cur.fetchall():
1714-
slug = (prop or "").strip()
1715-
if slug:
1716-
props.append({"slug": slug,
1717-
"title": slug.replace("_", " ").title(),
1718-
"count": int(cnt or 0)})
1719-
except Exception as e2:
1720-
app.logger.warning("available_keys: uploads_log fallback failed: %s", e2)
1721-
1722-
# --- Clips: prefer DB table (music_clips); fallback to CSV if needed ---
1702+
# --- Clip titles: prefer DB table; fall back to CSV (/data/drive_music.csv) ---
1703+
clip_titles = []
17231704
try:
17241705
with sqlite3.connect(DB_NAME) as conn:
17251706
cur = conn.cursor()
1726-
cur.execute("""
1727-
SELECT title
1728-
FROM music_clips
1729-
WHERE title IS NOT NULL AND TRIM(title) <> ''
1730-
GROUP BY title
1731-
ORDER BY LOWER(title)
1732-
""")
1707+
cur.execute("SELECT title FROM music_clips WHERE title <> '' ORDER BY id DESC")
17331708
clip_titles = [r[0] for r in cur.fetchall()]
17341709
except Exception:
1735-
pass # table may not exist; try CSV below
1710+
pass
17361711

17371712
if not clip_titles:
1738-
import csv, os
1739-
for path in ("/data/drive_music.csv", "drive_music.csv"):
1740-
if os.path.exists(path):
1741-
try:
1742-
with open(path, encoding="utf-8") as f:
1743-
sample = f.read(4096)
1744-
f.seek(0)
1745-
titles = set()
1746-
try:
1747-
has_header = csv.Sniffer().has_header(sample)
1748-
except Exception:
1749-
has_header = False
1750-
1751-
if has_header:
1752-
reader = csv.DictReader(f)
1753-
for row in reader:
1754-
t = (row.get("title") or "").strip()
1755-
if t: titles.add(t)
1756-
else:
1757-
reader = csv.reader(f)
1758-
for row in reader:
1759-
if row:
1760-
t = (row[0] or "").strip()
1761-
if t: titles.add(t)
1762-
1763-
clip_titles = sorted(titles, key=str.lower)
1764-
if clip_titles:
1765-
break
1766-
except Exception as e:
1767-
app.logger.warning("available_keys: CSV fallback failed for %s: %s", path, e)
1713+
try:
1714+
with open("/data/drive_music.csv", encoding="utf-8") as f:
1715+
reader = csv.DictReader(f)
1716+
need = {"title", "description", "preview_url", "download_url"}
1717+
if reader.fieldnames and need.issubset(set(reader.fieldnames)):
1718+
for row in reader:
1719+
t = (row.get("title") or "").strip()
1720+
if t:
1721+
clip_titles.append(t)
1722+
except Exception:
1723+
pass
17681724

17691725
return render_template("available_keys.html", props=props, clip_titles=clip_titles)
17701726
##############################################################################################################################################################

0 commit comments

Comments
 (0)