97 lines
3.5 KiB
Python
97 lines
3.5 KiB
Python
import sqlite3
|
|
from flask import Flask, render_template, request, request, jsonify, session
|
|
import os
|
|
import random
|
|
|
|
app = Flask(__name__)
|
|
|
|
SEARCH_DB_NAME = 'search.db'
|
|
|
|
search_db = sqlite3.connect(SEARCH_DB_NAME, check_same_thread=False)
|
|
search_db.row_factory = sqlite3.Row
|
|
|
|
def searchcommand():
|
|
query = request.form.get("query", "").strip()
|
|
category = request.form.get("category", "").strip()
|
|
include_transcript = request.form.get("includeTranscript") in ["true", "on"]
|
|
words = [w for w in query.split() if w]
|
|
cursor = search_db.cursor()
|
|
|
|
allowed_basefolders = list(session['folders'].keys())
|
|
print("Allowed base folders:", allowed_basefolders)
|
|
|
|
if not include_transcript:
|
|
conditions = []
|
|
params = []
|
|
# Apply query words to relative_path and filename
|
|
for word in words:
|
|
conditions.append("(relative_path LIKE ? OR filename LIKE ?)")
|
|
params.extend([f"%{word}%", f"%{word}%"])
|
|
# Search category in filename
|
|
if category:
|
|
conditions.append("(filename LIKE ?)")
|
|
params.extend([f"%{category}%"])
|
|
# Only include rows where basefolder is in allowed_basefolders
|
|
if allowed_basefolders:
|
|
placeholders = ",".join("?" for _ in allowed_basefolders)
|
|
conditions.append(f"basefolder IN ({placeholders})")
|
|
params.extend(allowed_basefolders)
|
|
|
|
sql = "SELECT * FROM files"
|
|
if conditions:
|
|
sql += " WHERE " + " AND ".join(conditions)
|
|
|
|
cursor.execute(sql, params)
|
|
raw_results = cursor.fetchall()
|
|
results = [dict(row) for row in raw_results]
|
|
|
|
# Randomize the list before sorting to break ties randomly.
|
|
random.shuffle(results)
|
|
results.sort(key=lambda x: x["hitcount"], reverse=True)
|
|
|
|
else:
|
|
# Advanced search: include transcript. Count transcript hits.
|
|
conditions = []
|
|
params = []
|
|
# Apply query words for filename and transcript
|
|
for word in words:
|
|
conditions.append("(filename LIKE ? OR transcript LIKE ?)")
|
|
params.extend([f"%{word}%", f"%{word}%"])
|
|
# Search category in filename
|
|
if category:
|
|
conditions.append("(filename LIKE ?)")
|
|
params.extend([f"%{category}%"])
|
|
# Only include rows where basefolder is in allowed_basefolders
|
|
if allowed_basefolders:
|
|
placeholders = ",".join("?" for _ in allowed_basefolders)
|
|
conditions.append(f"basefolder IN ({placeholders})")
|
|
params.extend(allowed_basefolders)
|
|
|
|
sql = "SELECT * FROM files"
|
|
if conditions:
|
|
sql += " WHERE " + " AND ".join(conditions)
|
|
cursor.execute(sql, params)
|
|
raw_results = cursor.fetchall()
|
|
|
|
results = []
|
|
for row in raw_results:
|
|
result = dict(row)
|
|
transcript = result.get("transcript") or ""
|
|
total_hits = sum(transcript.lower().count(word.lower()) for word in words)
|
|
result["transcript_hits"] = total_hits
|
|
result.pop("transcript", None)
|
|
results.append(result)
|
|
|
|
# Randomize the list before sorting to break ties randomly.
|
|
random.shuffle(results)
|
|
results.sort(key=lambda x: x["transcript_hits"], reverse=True)
|
|
|
|
results = results[:100]
|
|
return jsonify(results=results)
|
|
|
|
def search():
|
|
title_short = os.environ.get('TITLE_SHORT', 'Default Title')
|
|
title_long = os.environ.get('TITLE_LONG', 'Default Title')
|
|
return render_template("search.html", title_short=title_short, title_long=title_long)
|
|
|