This repository has been archived on 2026-11-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
videovault/app/search.py
T

73 lines
2.0 KiB
Python

import json
import urllib.request
import urllib.error
from . import config
class SearchEngine:
def __init__(self):
self._enable = not config.MEILI_DISABLE
self._ready = False
self._check()
def _check(self):
if not self._enable:
return
try:
self._req("GET", "/health")
self._ready = True
except Exception:
self._ready = False
def _req(self, method, path, body=None):
url = config.MEILI_URL.rstrip("/") + path
data = json.dumps(body).encode() if body is not None else None
req = urllib.request.Request(
url,
data=data,
method=method,
headers={
"Authorization": f"Bearer {config.MEILI_KEY}",
"Content-Type": "application/json",
},
)
with urllib.request.urlopen(req, timeout=5) as r:
return json.loads(r.read() or b"{}")
def available(self):
self._check()
return self._ready
def index_video(self, doc):
if not self.available():
return
try:
self._req("POST", "/indexes/videos/documents", [doc])
except Exception:
pass
def delete_video(self, video_id):
if not self.available():
return
try:
self._req("POST", "/indexes/videos/documents/delete-batch",
[str(video_id)])
except Exception:
pass
def search(self, q, filters=None, limit=200):
if q and self.available():
try:
payload = {"q": q, "limit": limit, "attributesToRetrieve": ["id"]}
if filters:
payload["filter"] = filters
res = self._req("POST", "/indexes/videos/search", payload)
hits = res.get("hits") or []
return [int(h["id"]) for h in hits]
except Exception:
pass
return None
engine = SearchEngine()