mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-17 23:41:28 +00:00
Collect detailed Jellyfin index outcomes
This commit is contained in:
@@ -463,10 +463,11 @@ def reconcile_selected_libraries(
|
||||
|
||||
removed_library_ids: list[str] = []
|
||||
removed_media_ids: list[str] = []
|
||||
removed_items: list[dict[str, object]] = []
|
||||
with connect(settings.db_path) as con:
|
||||
rows = con.execute(
|
||||
"""
|
||||
SELECT id, jellyfin_library_id
|
||||
SELECT id, title, jellyfin_item_id, jellyfin_library_id
|
||||
FROM media
|
||||
WHERE source_kind = 'jellyfin'
|
||||
AND jellyfin_library_id IS NOT NULL
|
||||
@@ -483,6 +484,17 @@ def reconcile_selected_libraries(
|
||||
{str(row["jellyfin_library_id"]) for row in stale}
|
||||
)
|
||||
removed_media_ids = [str(row["id"]) for row in stale]
|
||||
removed_items = [
|
||||
{
|
||||
"status": "removed",
|
||||
"library_id": str(row["jellyfin_library_id"] or ""),
|
||||
"library": str(row["jellyfin_library_id"] or ""),
|
||||
"item_id": str(row["jellyfin_item_id"] or ""),
|
||||
"title": str(row["title"] or row["jellyfin_item_id"] or row["id"]),
|
||||
"reason": "Library deselected from Scene Search",
|
||||
}
|
||||
for row in stale
|
||||
]
|
||||
|
||||
if removed_library_ids:
|
||||
placeholders = ",".join("?" for _ in removed_library_ids)
|
||||
@@ -502,15 +514,22 @@ def reconcile_selected_libraries(
|
||||
"removed_library_ids": removed_library_ids,
|
||||
"removed_libraries": len(removed_library_ids),
|
||||
"removed_media": len(removed_media_ids),
|
||||
"removed_items": removed_items,
|
||||
}
|
||||
|
||||
|
||||
def _reconcile_library(library_id: str, current_item_ids: set[str]) -> int:
|
||||
def _reconcile_library(
|
||||
library_id: str,
|
||||
current_item_ids: set[str],
|
||||
*,
|
||||
library_name: str | None = None,
|
||||
report_items: list[dict[str, object]] | None = None,
|
||||
) -> int:
|
||||
stale_rows = []
|
||||
with connect(settings.db_path) as con:
|
||||
rows = con.execute(
|
||||
"""
|
||||
SELECT id, jellyfin_item_id
|
||||
SELECT id, title, jellyfin_item_id
|
||||
FROM media
|
||||
WHERE source_kind = 'jellyfin'
|
||||
AND jellyfin_library_id = ?
|
||||
@@ -528,6 +547,17 @@ def _reconcile_library(library_id: str, current_item_ids: set[str]) -> int:
|
||||
|
||||
for row in stale_rows:
|
||||
shutil.rmtree(settings.thumbs_dir / str(row["id"]), ignore_errors=True)
|
||||
if report_items is not None:
|
||||
report_items.append(
|
||||
{
|
||||
"status": "removed",
|
||||
"library_id": library_id,
|
||||
"library": library_name or library_id,
|
||||
"item_id": str(row["jellyfin_item_id"] or ""),
|
||||
"title": str(row["title"] or row["jellyfin_item_id"] or row["id"]),
|
||||
"reason": "Item no longer present in Jellyfin library",
|
||||
}
|
||||
)
|
||||
|
||||
return len(stale_rows)
|
||||
|
||||
@@ -540,6 +570,7 @@ def _index_library(
|
||||
limit: int | None,
|
||||
force: bool,
|
||||
progress: ProgressCallback | None = None,
|
||||
report_items: list[dict[str, object]] | None = None,
|
||||
) -> tuple[SiglipEmbedder | None, int, int, int, int]:
|
||||
library_id = str(library["Id"])
|
||||
library_name = _library_name(library)
|
||||
@@ -599,6 +630,17 @@ def _index_library(
|
||||
if existing:
|
||||
con.execute("DELETE FROM media WHERE id = ?", (existing["id"],))
|
||||
pruned += 1
|
||||
if report_items is not None:
|
||||
report_items.append(
|
||||
{
|
||||
"status": "removed",
|
||||
"library_id": library_id,
|
||||
"library": library_name,
|
||||
"item_id": item_id,
|
||||
"title": title,
|
||||
"reason": "Jellyfin skip tag",
|
||||
}
|
||||
)
|
||||
shutil.rmtree(
|
||||
settings.thumbs_dir / jellyfin_media_id(item_id),
|
||||
ignore_errors=True,
|
||||
@@ -677,11 +719,23 @@ def _index_library(
|
||||
else:
|
||||
local_path = Path(source_path_text)
|
||||
if not local_path.is_file():
|
||||
reason = "No Trickplay and media path is not locally accessible"
|
||||
print(
|
||||
f"[{number}/{len(summaries)}] SKIP {title}: "
|
||||
"no Trickplay and media path is not locally accessible"
|
||||
)
|
||||
failed += 1
|
||||
if report_items is not None:
|
||||
report_items.append(
|
||||
{
|
||||
"status": "failed",
|
||||
"library_id": library_id,
|
||||
"library": library_name,
|
||||
"item_id": item_id,
|
||||
"title": title,
|
||||
"reason": reason,
|
||||
}
|
||||
)
|
||||
continue
|
||||
|
||||
print(
|
||||
@@ -703,8 +757,20 @@ def _index_library(
|
||||
trickplay_tile_height = None
|
||||
|
||||
if selection is None and not frame_paths:
|
||||
reason = "No frames were extracted"
|
||||
print(f"[{number}/{len(summaries)}] SKIP {title}: no frames")
|
||||
failed += 1
|
||||
if report_items is not None:
|
||||
report_items.append(
|
||||
{
|
||||
"status": "failed",
|
||||
"library_id": library_id,
|
||||
"library": library_name,
|
||||
"item_id": item_id,
|
||||
"title": title,
|
||||
"reason": reason,
|
||||
}
|
||||
)
|
||||
continue
|
||||
|
||||
if embedder is None:
|
||||
@@ -817,16 +883,47 @@ def _index_library(
|
||||
f"{preserved_note}"
|
||||
)
|
||||
indexed += 1
|
||||
if report_items is not None:
|
||||
report_items.append(
|
||||
{
|
||||
"status": "indexed",
|
||||
"library_id": library_id,
|
||||
"library": library_name,
|
||||
"item_id": item_id,
|
||||
"title": title,
|
||||
"frames": len(frame_rows),
|
||||
"preserved_annotations": preserved,
|
||||
"reason": "Indexed successfully",
|
||||
}
|
||||
)
|
||||
|
||||
except Exception as exc:
|
||||
failed += 1
|
||||
failed_item_id = str(summary.get("Id") or "")
|
||||
failed_title = str(summary.get("Name") or failed_item_id or "(unnamed)")
|
||||
print(
|
||||
f"[{number}/{len(summaries)}] FAIL "
|
||||
f"{summary.get('Name') or summary.get('Id')}: {exc}"
|
||||
f"{failed_title}: {exc}"
|
||||
)
|
||||
if report_items is not None:
|
||||
report_items.append(
|
||||
{
|
||||
"status": "failed",
|
||||
"library_id": library_id,
|
||||
"library": library_name,
|
||||
"item_id": failed_item_id,
|
||||
"title": failed_title,
|
||||
"reason": str(exc),
|
||||
}
|
||||
)
|
||||
|
||||
if limit is None:
|
||||
reconciled = _reconcile_library(library_id, current_item_ids)
|
||||
reconciled = _reconcile_library(
|
||||
library_id,
|
||||
current_item_ids,
|
||||
library_name=library_name,
|
||||
report_items=report_items,
|
||||
)
|
||||
pruned += reconciled
|
||||
if reconciled:
|
||||
print(f" pruned {reconciled} stale item(s) no longer present in Jellyfin")
|
||||
@@ -844,6 +941,7 @@ def index_libraries(
|
||||
limit: int | None = None,
|
||||
force: bool = False,
|
||||
progress: ProgressCallback | None = None,
|
||||
report_items: list[dict[str, object]] | None = None,
|
||||
) -> tuple[SiglipEmbedder | None, dict]:
|
||||
total_indexed = 0
|
||||
total_skipped = 0
|
||||
@@ -869,6 +967,7 @@ def index_libraries(
|
||||
limit=limit,
|
||||
force=force,
|
||||
progress=progress,
|
||||
report_items=report_items,
|
||||
)
|
||||
total_indexed += indexed
|
||||
total_skipped += skipped
|
||||
|
||||
Reference in New Issue
Block a user