The plugin contract, the model task and the tools roledesk imports, copied rather than reached through an editable install of news, which also exposes a package named pipeline. news stays the source of truth for desk. Refreshed from news/desk at a275c38a1aa0fe80a7ec7dd5781ecccf683f71c6 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
4.5 KiB
Python
114 lines
4.5 KiB
Python
"""Where a video actually lives, and how to link into the middle of it.
|
|
|
|
Every segment newsdesk stores has to answer one question: *press play at
|
|
1:08:27 — where?* Today the answer is a YouTube URL. It will not always be.
|
|
The same episode may sit on disk, ripped or recorded; a show may take its
|
|
uploads down; a video may only ever have existed as a local file.
|
|
|
|
So nothing in this codebase stores a YouTube URL as a fact. It stores a
|
|
`MediaRef` — a kind and a key — and asks this module for a link when it needs
|
|
one. Swapping YouTube for a copy on disk is then a line of config rather than
|
|
an UPDATE across every item that mentioned it.
|
|
|
|
video:
|
|
local_media:
|
|
GUYEofBebDc: D:/media/lwiai/255.mp4
|
|
|
|
`resolve` applies that map. Everything downstream keeps working because it was
|
|
never holding a URL in the first place.
|
|
|
|
The one thing this deliberately does not do is *find* local copies. Matching
|
|
"is this file the same episode as that video" by filename or duration is a
|
|
guess, and a wrong guess points a citation at the wrong recording. The mapping
|
|
is written by a person, or it does not exist.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from pathlib import Path, PurePath
|
|
|
|
#: A local file, playable in the reading pane's <video> element. Anything else
|
|
#: on disk (mkv, wav) still resolves; the pane just may not play it.
|
|
PLAYABLE = frozenset({".mp4", ".m4v", ".webm", ".ogg", ".mp3", ".m4a", ".wav"})
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class MediaRef:
|
|
"""A video, identified by where it is rather than by a URL.
|
|
|
|
`kind` is what the key means: `youtube` is an 11-character video id,
|
|
`spotify` is an episode id, `file` is a path on this machine, `url` is
|
|
anything else somebody handed us. `key` is stable — it is what
|
|
`Segment.external_id` is built from — so re-pointing a video at a local
|
|
copy must *not* change it. That is why `resolve` returns a separate
|
|
playback ref instead of rewriting this one.
|
|
"""
|
|
|
|
kind: str
|
|
key: str
|
|
|
|
@property
|
|
def playable_locally(self) -> bool:
|
|
return self.kind == "file" and PurePath(self.key).suffix.lower() in PLAYABLE
|
|
|
|
def at(self, seconds: float = 0) -> str:
|
|
"""A link that opens this video at that moment.
|
|
|
|
The media fragment (`#t=`) is the same idea in every case: it is what
|
|
YouTube's `&t=` and Spotify's `?t=` do, what a `<video>` element
|
|
honours natively, and what most desktop players understand from a
|
|
`file://` URL.
|
|
"""
|
|
offset = max(0, int(seconds))
|
|
if self.kind == "youtube":
|
|
return f"https://www.youtube.com/watch?v={self.key}&t={offset}s"
|
|
if self.kind == "spotify":
|
|
return f"https://open.spotify.com/episode/{self.key}?t={offset}"
|
|
if self.kind == "file":
|
|
return Path(self.key).absolute().as_uri() + (f"#t={offset}" if offset else "")
|
|
return self.key + (f"#t={offset}" if offset else "")
|
|
|
|
def embed(self, seconds: float = 0) -> str:
|
|
"""The player a reading pane can frame at that moment, or "".
|
|
|
|
Only the hosts that publish a player built for framing have one: a
|
|
YouTube watch page and a Spotify episode page both refuse to be
|
|
framed, and each has an `/embed/` form that does not. A local file
|
|
needs none — the pane plays it in a `<video>` element — and an
|
|
arbitrary URL is not known to allow it.
|
|
"""
|
|
offset = max(0, int(seconds))
|
|
if self.kind == "youtube":
|
|
return f"https://www.youtube.com/embed/{self.key}?start={offset}"
|
|
if self.kind == "spotify":
|
|
return f"https://open.spotify.com/embed/episode/{self.key}?t={offset}"
|
|
return ""
|
|
|
|
|
|
def youtube(video_id: str) -> MediaRef:
|
|
return MediaRef(kind="youtube", key=video_id)
|
|
|
|
|
|
def local(path: str | Path) -> MediaRef:
|
|
return MediaRef(kind="file", key=str(path))
|
|
|
|
|
|
def resolve(ref: MediaRef, overrides: dict[str, str] | None = None) -> MediaRef:
|
|
"""The ref to *play*, after applying the local-media map.
|
|
|
|
Keyed on the original key, so a mapping written once keeps working however
|
|
the item was ingested. Returns the ref unchanged when there is no override
|
|
or the file has since gone — a stale mapping should fall back to the
|
|
original rather than hand out a link to a file that isn't there.
|
|
"""
|
|
if not overrides:
|
|
return ref
|
|
replacement = overrides.get(ref.key)
|
|
if not replacement:
|
|
return ref
|
|
path = Path(replacement).expanduser()
|
|
if not path.exists():
|
|
return ref
|
|
return local(path)
|