Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 100 additions & 52 deletions backend/api/media_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
from backend.util.arr_delete import ArrDeleter, ArrOutcome
from backend.util.arr_instances import instance_detail
from backend.util.config import ConfigError, load_config
from backend.util.helper import collection_title_fields
from backend.util.normalization import normalize_titles
from backend.util.public_error import failure_text
from backend.util.database import (
INCOMPLETE_METADATA_FIELDS,
Expand Down Expand Up @@ -746,6 +748,56 @@ def delete_collection(
)


def _is_text(value: Any) -> bool:
"""A string with more than whitespace in it."""
return isinstance(value, str) and bool(value.strip())


def _scalar_fields(payload: dict) -> Tuple[Dict[str, Any], Optional[str]]:
"""The year, ids, folder and library ``payload`` sets, or why one is invalid."""
fields: Dict[str, Any] = {}
for key in ("year", "tmdb_id", "tvdb_id"):
if key in payload:
number = None if payload[key] is None else _media_id(payload[key])
if payload[key] is not None and number is None:
return {}, f"Field '{key}' must be a positive whole number or null"
fields[key] = number
for key in ("imdb_id", "folder"):
if key in payload:
if payload[key] is not None and not isinstance(payload[key], str):
return {}, f"Field '{key}' must be a string or null"
fields[key] = payload[key]
if "library_name" in payload:
if not _is_text(payload["library_name"]):
return {}, "Field 'library_name' must be a non-empty string"
fields["library_name"] = payload["library_name"]
return fields, None


def _collection_fields(payload: dict) -> Tuple[Dict[str, Any], Optional[str]]:
"""The collection columns ``payload`` sets (a title derived as a Plex sync derives it), or why one is invalid."""
fields, why = _scalar_fields(payload)
if why:
return {}, why
if "title" in payload:
if not _is_text(payload["title"]):
return {}, "Field 'title' must be a non-empty string"
titles = collection_title_fields(payload["title"].strip())
Comment thread
chodeus marked this conversation as resolved.
Resolved
if not _is_text(titles["title"]):
return {}, "Field 'title' is blank once decoded to ASCII"
fields.update(titles)
if "alternate_titles" in payload:
alts = payload["alternate_titles"]
if not isinstance(alts, list) or not all(_is_text(t) for t in alts):
return {}, "Field 'alternate_titles' must be a list of non-empty strings"
alts = [t.strip() for t in alts]
normalized = [normalize_titles(t) for t in alts]
if not all(normalized):
return {}, "Field 'alternate_titles' has a title that is blank once normalized"
fields.update(alternate_titles=alts, normalized_alternate_titles=normalized)
return fields, None


@router.post(
"/collections",
summary="Create collection",
Expand All @@ -763,7 +815,7 @@ def delete_collection(
}
},
},
400: {"description": "Missing 'title' or 'instance_name'"},
400: {"description": "Missing 'title', 'instance_name' or 'library_name', or a field of the wrong type"},
413: {"description": "Request body too large"},
},
)
Expand All @@ -787,40 +839,40 @@ async def create_collection(
return body_too_large_error()
logger.debug(f"Serving POST /api/media/collections with payload: {payload}")

title = payload.get("title")
if not title:
if "title" not in payload:
return error(
"Field 'title' is required", code="MISSING_TITLE", status_code=400
)

instance_name = payload.get("instance_name")
if not instance_name:
if "instance_name" not in payload:
return error(
"Field 'instance_name' is required",
code="MISSING_INSTANCE",
status_code=400,
)
instance_name = payload["instance_name"]
if not _is_text(instance_name):
return error(
"Field 'instance_name' must be a non-empty string",
code="INVALID_FIELDS",
status_code=400,
)

record = {
"title": title,
"normalized_title": title.lower().strip() if title else None,
"year": payload.get("year"),
"tmdb_id": payload.get("tmdb_id"),
"tvdb_id": payload.get("tvdb_id"),
"imdb_id": payload.get("imdb_id"),
"folder": payload.get("folder"),
"library_name": payload.get("library_name"),
"alternate_titles": payload.get("alternate_titles", []),
"normalized_alternate_titles": payload.get(
"normalized_alternate_titles", []
),
}
if "library_name" not in payload:
return error(
"Field 'library_name' is required",
code="MISSING_LIBRARY",
status_code=400,
)
record, why = _collection_fields(payload)
if why:
return error(why, code="INVALID_FIELDS", status_code=400)

db.collection.upsert(record, instance_name)

# Fetch the created/updated record
# Read back by the stored title: the sync's derivation may have changed it.
created = db.collection.get_by_title_and_instance(
title, instance_name, record["library_name"]
record["title"], instance_name, record["library_name"]
)

return ok("Collection created successfully", {"collection": created})
Expand Down Expand Up @@ -852,7 +904,10 @@ async def create_collection(
}
}
},
}
},
400: {"description": "No updatable field, or a field of the wrong type"},
404: {"description": "No collection with that id"},
409: {"description": "Another collection in this library already has that title"},
},
)
async def update_collection(
Expand All @@ -865,7 +920,7 @@ async def update_collection(
Update a media collection by ID.

Fetches the existing collection, merges in the provided updates,
and upserts the result back to the database.
and writes the result back onto that row.

Args:
collection_id: The unique identifier of the collection to update
Expand All @@ -877,9 +932,8 @@ async def update_collection(
payload = await read_request_json(request)
if payload is BODY_TOO_LARGE:
return body_too_large_error()
# An unusable body used to raise into the 500 handler; keep that
# response — falling through would upsert the record unchanged.
if not isinstance(payload, dict) or not payload:
# A non-object body used to raise into the 500 handler; keep that response.
if not isinstance(payload, dict):
return error(
"Error updating collection",
code="COLLECTION_UPDATE_ERROR",
Expand All @@ -897,33 +951,27 @@ async def update_collection(
status_code=404,
)

# Merge updates into existing record
updatable_fields = [
"title",
"year",
"tmdb_id",
"tvdb_id",
"imdb_id",
"folder",
"library_name",
"alternate_titles",
"normalized_alternate_titles",
]
for field in updatable_fields:
if field in payload:
existing[field] = payload[field]

# Update normalized_title if title changed
if "title" in payload:
existing["normalized_title"] = (
payload["title"].lower().strip() if payload["title"] else None
)

instance_name = existing.get("instance_name")
db.collection.upsert(existing, instance_name)

# Fetch the updated record
fields, why = _collection_fields(payload)
if why:
return error(why, code="INVALID_FIELDS", status_code=400)
if not fields:
return error("No valid fields to update", code="NO_FIELDS", status_code=400)
existing.update(fields)
# In place by id: an upsert keyed on the new title would add a second row.
written = db.collection.update_row(collection_id, existing)
updated = db.collection.get_by_id(collection_id)
if updated is None:
return error(
f"Collection {collection_id} not found",
code="COLLECTION_NOT_FOUND",
status_code=404,
)
if not written:
return error(
"Another collection in this library already has that title",
code="COLLECTION_EXISTS",
status_code=409,
)

return ok("Collection updated successfully", {"collection": updated})

Expand Down
15 changes: 3 additions & 12 deletions backend/modules/poster_renamerr.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
)
from backend.util.helper import (
as_list,
parse_alternate_titles,
classify_match,
is_id_match_reason,
create_table,
Expand Down Expand Up @@ -529,15 +530,7 @@ def find_asset_candidate(
season_number = media.get("season_number")
expected_asset_type = "collection" if is_collection else asset_type

alt_titles = []
try:
alt_titles = json.loads(media.get("alternate_titles") or "[]")
except Exception:
pass
# The collections API stores these unchecked: keep only the strings.
if not isinstance(alt_titles, list):
alt_titles = []
alt_titles = [t for t in alt_titles if isinstance(t, str) and t]
alt_titles = parse_alternate_titles(media.get("alternate_titles"))

reasons = []
candidates = []
Expand Down Expand Up @@ -644,9 +637,7 @@ def _title_match_reason(
"""is_match()'s reason when a title-lookup poster belongs to ``media``, else ""."""
if cand.get("season_number") != media.get("season_number"):
return ""
cand_alt_titles = set(
json.loads(cand.get("normalized_alternate_titles", "[]") or "[]")
)
cand_alt_titles = set(parse_alternate_titles(cand.get("normalized_alternate_titles")))
if (
cand.get("normalized_title", "") not in all_titles
and not all_titles & cand_alt_titles
Expand Down
9 changes: 2 additions & 7 deletions backend/util/asset_candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"""

import difflib
import json
from typing import Any, Dict, List, Optional, Tuple

from backend.util.helper import is_match
from backend.util.helper import is_match, parse_alternate_titles
from backend.util.normalization import normalize_titles


Expand All @@ -22,11 +21,7 @@ def rank_candidates(
) -> List[Tuple[Dict[str, Any], bool, float, str]]:
"""Best-first (row, would_match, similarity, reason) tuples for a media row."""
season_number = row.get("season_number")
try:
alts = json.loads(row.get("alternate_titles") or "[]")
except (ValueError, TypeError):
alts = []
search_titles = [row.get("title")] + [a for a in alts if a]
search_titles = [row.get("title")] + parse_alternate_titles(row.get("alternate_titles"))
row_norm = row.get("normalized_title") or normalize_titles(row.get("title") or "")

seen = set()
Expand Down
45 changes: 39 additions & 6 deletions backend/util/database/collection_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,51 @@ class CollectionCache(DatabaseBase):
Provides CRUD and sync operations.
"""

def upsert(self, record: dict, instance_name: str) -> None:
"""Insert or update a collection record for a given instance."""
record["instance_name"] = instance_name
record["asset_type"] = "collection"

# Serialize list/dict fields to JSON
@staticmethod
def _store_json(record: dict) -> None:
"""Serialize ``record``'s title lists to JSON in place; a missing list is stored empty."""
for key in ("alternate_titles", "normalized_alternate_titles"):
if isinstance(record.get(key), (list, dict)):
record[key] = json.dumps(record[key])
elif record.get(key) is None:
record[key] = json.dumps([])

def update_row(self, id: int, record: dict) -> bool:
"""Write ``record``'s fields onto row ``id`` in place; False when that row is gone or another holds its key."""
self._store_json(record)
columns = (
"title",
"normalized_title",
"alternate_titles",
"normalized_alternate_titles",
"year",
"tmdb_id",
"tvdb_id",
"imdb_id",
"folder",
"library_name",
)
# `IS`: the UNIQUE key treats NULL libraries as distinct, so it never refuses a clash there.
written = self.execute_query(
f"UPDATE collections_cache SET {', '.join(c + '=?' for c in columns)} WHERE id=? AND NOT EXISTS "
"(SELECT 1 FROM collections_cache WHERE title IS ? AND library_name IS ? AND instance_name IS ? AND id<>?)",
(
*(record.get(c) for c in columns),
id,
record.get("title"),
record.get("library_name"),
record.get("instance_name"),
id,
),
)
return bool(written)

def upsert(self, record: dict, instance_name: str) -> None:
"""Insert or update a collection record for a given instance."""
record["instance_name"] = instance_name
record["asset_type"] = "collection"
self._store_json(record)

self.execute_query(
"""
INSERT INTO collections_cache
Expand Down
Loading
Loading