diff --git a/docs/dev/creating-a-service.md b/docs/dev/creating-a-service.md index 8d384182..5261b7c0 100644 --- a/docs/dev/creating-a-service.md +++ b/docs/dev/creating-a-service.md @@ -550,7 +550,10 @@ exists. Timestamps take `"HH:MM:SS[.mmm]"`, int milliseconds, or float seconds. ```python def get_chapters(self, title: Title_T) -> Chapters: chapters = [Chapter(name=c.get("name"), timestamp=c["start"]) for c in title.data.get("chapters", [])] - return Chapters(sorted(chapters, key=lambda c: c.timestamp)) + unique: dict[str, Chapter] = {} + for chapter in sorted(chapters, key=lambda c: not c.named): + unique.setdefault(chapter.timestamp, chapter) + return Chapters(sorted(unique.values(), key=lambda c: c.timestamp)) ``` !!! warning "Sort by timestamp before you build the `Chapters`" @@ -559,7 +562,7 @@ def get_chapters(self, title: Title_T) -> Chapters: lands before it. You never need to add `Chapter(0)`. An API chapter at `0` keeps its name. Drop - duplicate timestamps. + duplicate timestamps and keep the named chapter, as the example does. !!! note "Close a named range with an unnamed chapter" An unnamed `Chapter` ends the range before it: `Chapter(name="Intro")` at the diff --git a/unshackle/core/api/handlers.py b/unshackle/core/api/handlers.py index d746c650..e62e6523 100644 --- a/unshackle/core/api/handlers.py +++ b/unshackle/core/api/handlers.py @@ -4290,7 +4290,8 @@ async def session_tracks_handler( try: chapters = await asyncio.to_thread(service_instance.get_chapters, title) session.chapters_by_title[str(title_id)] = chapters if chapters else [] - except (Exception, SystemExit): + except (Exception, SystemExit) as e: + log.debug("get_chapters failed for title %s: %s", title_id, e) session.chapters_by_title[str(title_id)] = [] snapshot = await asyncio.to_thread(capture, tracks, config.serve.get("compression_level", 1)) diff --git a/unshackle/services/EXAMPLE/__init__.py b/unshackle/services/EXAMPLE/__init__.py index 4e38c486..9b04e291 100644 --- a/unshackle/services/EXAMPLE/__init__.py +++ b/unshackle/services/EXAMPLE/__init__.py @@ -443,14 +443,17 @@ class EXAMPLE(Service): return tracks def get_chapters(self, title: Title_T) -> Chapters: - chapters = Chapters() + chapters = [] for chapter in title.data.get("chapters", []): if chapter["name"] == "Intro": - chapters.add(Chapter(timestamp=chapter["start"], name="Opening")) - chapters.add(Chapter(timestamp=chapter["end"])) # unnamed marker = chapter break + chapters.append(Chapter(timestamp=chapter["start"], name="Opening")) + chapters.append(Chapter(timestamp=chapter["end"])) # unnamed marker = chapter break elif chapter["name"] == "Credits": - chapters.add(Chapter(timestamp=chapter["start"], name="Credits")) - return chapters + chapters.append(Chapter(timestamp=chapter["start"], name="Credits")) + unique: dict[str, Chapter] = {} + for chapter in sorted(chapters, key=lambda c: not c.named): + unique.setdefault(chapter.timestamp, chapter) + return Chapters(sorted(unique.values(), key=lambda c: c.timestamp)) def get_widevine_service_certificate(self, **_: Any) -> Optional[str]: # Returning the service cert enables privacy-mode license requests.