mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-17 07:21:28 +00:00
254 lines
8.8 KiB
Python
254 lines
8.8 KiB
Python
from __future__ import annotations
|
|
|
|
import threading
|
|
from copy import deepcopy
|
|
from datetime import datetime, timezone
|
|
from typing import Any
|
|
|
|
from .database import connect, init_db
|
|
from .jellyfin_index import (
|
|
build_jellyfin_client,
|
|
index_libraries,
|
|
reconcile_selected_libraries,
|
|
resolve_library_ids,
|
|
)
|
|
from .search_service import SearchService
|
|
from .sync import rebuild_meili
|
|
|
|
|
|
class IndexJobManager:
|
|
"""Runs one plugin-triggered Jellyfin indexing job at a time."""
|
|
|
|
def __init__(self, search_service: SearchService) -> None:
|
|
self.search_service = search_service
|
|
self._state_lock = threading.Lock()
|
|
self._thread: threading.Thread | None = None
|
|
self._state: dict[str, Any] = {
|
|
"running": False,
|
|
"stage": "idle",
|
|
"started_at": None,
|
|
"finished_at": None,
|
|
"error": None,
|
|
}
|
|
|
|
@property
|
|
def running(self) -> bool:
|
|
with self._state_lock:
|
|
return bool(self._state.get("running"))
|
|
|
|
def status(self) -> dict[str, Any]:
|
|
with self._state_lock:
|
|
return deepcopy(self._state)
|
|
|
|
def libraries(self) -> list[dict[str, Any]]:
|
|
client = build_jellyfin_client()
|
|
libraries = client.media_folders()
|
|
init_db(self.search_service.db_path)
|
|
|
|
counts: dict[str, tuple[int, int]] = {}
|
|
with connect(self.search_service.db_path) as con:
|
|
rows = con.execute(
|
|
"""
|
|
SELECT m.jellyfin_library_id,
|
|
COUNT(DISTINCT m.id) AS indexed_items,
|
|
COUNT(f.id) AS indexed_frames
|
|
FROM media m
|
|
LEFT JOIN frames f ON f.media_id = m.id
|
|
WHERE m.source_kind = 'jellyfin'
|
|
AND m.jellyfin_library_id IS NOT NULL
|
|
GROUP BY m.jellyfin_library_id
|
|
"""
|
|
).fetchall()
|
|
for row in rows:
|
|
counts[str(row["jellyfin_library_id"])] = (
|
|
int(row["indexed_items"]),
|
|
int(row["indexed_frames"]),
|
|
)
|
|
|
|
result = []
|
|
for library in sorted(
|
|
libraries,
|
|
key=lambda item: str(item.get("Name") or "").casefold(),
|
|
):
|
|
library_id = str(library.get("Id") or "")
|
|
indexed_items, indexed_frames = counts.get(library_id, (0, 0))
|
|
result.append(
|
|
{
|
|
"id": library_id,
|
|
"name": str(library.get("Name") or library_id),
|
|
"indexed_items": indexed_items,
|
|
"indexed_frames": indexed_frames,
|
|
}
|
|
)
|
|
return result
|
|
|
|
def start(self, library_ids: list[str], *, force: bool = False) -> dict[str, Any]:
|
|
with self._state_lock:
|
|
if self._state.get("running"):
|
|
raise RuntimeError("A Jellyfin indexing job is already running.")
|
|
|
|
requested_ids = [
|
|
str(value).strip()
|
|
for value in library_ids
|
|
if str(value).strip()
|
|
]
|
|
client = build_jellyfin_client()
|
|
libraries = (
|
|
resolve_library_ids(client, requested_ids)
|
|
if requested_ids
|
|
else []
|
|
)
|
|
selected_ids = {
|
|
str(item.get("Id") or "")
|
|
for item in libraries
|
|
if str(item.get("Id") or "")
|
|
}
|
|
library_item_totals = [
|
|
client.item_count(str(item.get("Id") or ""))
|
|
for item in libraries
|
|
]
|
|
overall_items_total = sum(library_item_totals)
|
|
|
|
with self._state_lock:
|
|
self._state = {
|
|
"running": True,
|
|
"stage": "starting",
|
|
"started_at": datetime.now(timezone.utc).isoformat(),
|
|
"finished_at": None,
|
|
"error": None,
|
|
"library_total": len(libraries),
|
|
"library_ids": sorted(selected_ids),
|
|
"library_item_totals": library_item_totals,
|
|
"overall_items_total": overall_items_total,
|
|
"overall_items_completed": 0,
|
|
"overall_percent": 0.0,
|
|
}
|
|
|
|
self._thread = threading.Thread(
|
|
target=self._run,
|
|
args=(libraries, selected_ids, force),
|
|
name="scene-search-index",
|
|
daemon=True,
|
|
)
|
|
self._thread.start()
|
|
return self.status()
|
|
|
|
def _progress(self, update: dict) -> None:
|
|
with self._state_lock:
|
|
self._state.update(update)
|
|
self._update_overall_progress_locked()
|
|
|
|
def _update_overall_progress_locked(self) -> None:
|
|
stage = str(self._state.get("stage") or "")
|
|
total = int(self._state.get("overall_items_total") or 0)
|
|
totals = [
|
|
int(value)
|
|
for value in (self._state.get("library_item_totals") or [])
|
|
]
|
|
|
|
if stage == "complete":
|
|
self._state["overall_items_completed"] = total
|
|
self._state["overall_percent"] = 100.0
|
|
return
|
|
|
|
if stage in {"indexed", "syncing", "sync", "synced"}:
|
|
self._state["overall_items_completed"] = total
|
|
self._state["overall_percent"] = 99.0 if total else 99.0
|
|
return
|
|
|
|
library_number = int(self._state.get("library_number") or 1)
|
|
library_index = max(0, min(library_number - 1, len(totals)))
|
|
before_library = sum(totals[:library_index])
|
|
|
|
item_number = int(self._state.get("item_number") or 0)
|
|
completed_before_item = max(item_number - 1, 0)
|
|
completed = min(before_library + completed_before_item, total)
|
|
|
|
fractional_item = 0.0
|
|
frames_total = int(self._state.get("frames_total") or 0)
|
|
frames_processed = int(self._state.get("frames_processed") or 0)
|
|
if stage == "embedding" and frames_total > 0:
|
|
fractional_item = min(max(frames_processed / frames_total, 0.0), 1.0)
|
|
|
|
position = min(completed + fractional_item, float(total))
|
|
self._state["overall_items_completed"] = completed
|
|
self._state["overall_percent"] = (
|
|
round((position / total) * 99.0, 1)
|
|
if total > 0
|
|
else 0.0
|
|
)
|
|
|
|
def _run(
|
|
self,
|
|
libraries: list[dict],
|
|
selected_ids: set[str],
|
|
force: bool,
|
|
) -> None:
|
|
try:
|
|
removed = reconcile_selected_libraries(selected_ids)
|
|
removed_ids = [
|
|
str(value)
|
|
for value in (removed.get("removed_library_ids") or [])
|
|
]
|
|
if removed_ids:
|
|
self.search_service.meili.delete_jellyfin_libraries(removed_ids)
|
|
self._progress(
|
|
{
|
|
"removed_libraries": int(removed["removed_libraries"]),
|
|
"removed_library_media": int(removed["removed_media"]),
|
|
}
|
|
)
|
|
print(
|
|
"Removed deselected Jellyfin libraries: "
|
|
+ ", ".join(removed_ids)
|
|
+ f" ({int(removed.get('removed_media') or 0)} media item(s))"
|
|
)
|
|
|
|
if not libraries:
|
|
self._progress(
|
|
{
|
|
"running": False,
|
|
"stage": "complete",
|
|
"finished_at": datetime.now(timezone.utc).isoformat(),
|
|
"documents_processed": 0,
|
|
"library_total": 0,
|
|
"indexed": 0,
|
|
"skipped": 0,
|
|
"failed": 0,
|
|
"pruned": int(removed.get("removed_media") or 0),
|
|
}
|
|
)
|
|
return
|
|
|
|
# Search and indexing share one model instance and never use it concurrently.
|
|
with self.search_service.model_lock:
|
|
embedder = self.search_service.get_embedder()
|
|
_, summary = index_libraries(
|
|
build_jellyfin_client(),
|
|
libraries,
|
|
embedder=embedder,
|
|
force=force,
|
|
progress=self._progress,
|
|
)
|
|
self._progress({"stage": "syncing", **summary})
|
|
documents = rebuild_meili(progress=self._progress)
|
|
|
|
self._progress(
|
|
{
|
|
"running": False,
|
|
"stage": "complete",
|
|
"finished_at": datetime.now(timezone.utc).isoformat(),
|
|
"documents_processed": documents,
|
|
**summary,
|
|
}
|
|
)
|
|
except Exception as exc:
|
|
self._progress(
|
|
{
|
|
"running": False,
|
|
"stage": "failed",
|
|
"finished_at": datetime.now(timezone.utc).isoformat(),
|
|
"error": str(exc),
|
|
}
|
|
)
|