modified: app/routes.py
This commit is contained in:
@@ -39,13 +39,25 @@ def api_player():
|
||||
|
||||
@bp.route("/api/search")
|
||||
def api_search():
|
||||
from difflib import SequenceMatcher
|
||||
|
||||
q = (request.args.get("q") or "").strip().lower()
|
||||
if not q:
|
||||
return jsonify([])
|
||||
movies = Movie.query.filter(Movie.title.ilike(f"%{q}%")).limit(10).all()
|
||||
movies = Movie.query.filter(Movie.title.ilike(f"%{q}%")).limit(50).all()
|
||||
results = []
|
||||
for m in movies:
|
||||
score = SequenceMatcher(None, q, m.title.lower()).ratio()
|
||||
# Substring-Treffer gelten bereits als starke Uebereinstimmung
|
||||
if q in m.title.lower():
|
||||
score = max(score, 0.6)
|
||||
if score >= 0.6:
|
||||
results.append((score, m))
|
||||
results.sort(key=lambda x: x[1].year, reverse=True)
|
||||
results.sort(key=lambda x: x[0], reverse=True)
|
||||
return jsonify([
|
||||
{"id": m.id, "title": m.title, "year": m.year}
|
||||
for m in movies
|
||||
for _, m in results[:10]
|
||||
])
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user