Mechanical, whole-tree `ruff format` pass — no behaviour change. Recorded in .git-blame-ignore-revs (next commit) so git blame skips it.
56 lines
2.7 KiB
Python
56 lines
2.7 KiB
Python
"""Small cross-cutting helpers shared across modules (kept dependency-light on purpose)."""
|
|
|
|
import re
|
|
from datetime import date, datetime, timezone
|
|
|
|
|
|
def iso(dt: date | None) -> str | None:
|
|
"""Serialize a date/datetime for a JSON response: ISO-8601, or None when the column is
|
|
NULL. Single source of truth for the `x.isoformat() if x else None` idiom the routes had
|
|
hand-rolled ~17 times — so a future tz/format tweak lands in one place. Accepts `date`
|
|
too (datetime is a subclass) — e.g. MediaAsset.upload_date is a plain `date`."""
|
|
return dt.isoformat() if dt is not None else None
|
|
|
|
|
|
# Single source of truth for "looks like an email" — used by every endpoint that accepts an
|
|
# address (auth register/reset/demo, admin invites + demo whitelist, the setup wizard). Keep
|
|
# the strictness in one place so routes don't drift between this and a looser "@"-in check.
|
|
_EMAIL_RE = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")
|
|
|
|
|
|
def valid_email(value: str | None) -> bool:
|
|
return bool(value and _EMAIL_RE.match(value))
|
|
|
|
|
|
def now_utc() -> datetime:
|
|
"""Current time as a timezone-aware UTC datetime."""
|
|
return datetime.now(timezone.utc)
|
|
|
|
|
|
def to_tsquery_str(q: str) -> str | None:
|
|
"""Turn free-text into a to_tsquery() string: word runs ANDed together, the LAST word
|
|
prefix-matched (`:*`) for as-you-type search — e.g. "ti amo magyar" → "ti & amo & magyar:*".
|
|
Only `\\w` runs containing an alphanumeric are kept, so no tsquery operators can leak in
|
|
(the string is built from sanitised tokens, never the raw query). Shared by the YouTube feed
|
|
search and the Plex library search, hence this neutral home rather than either domain module."""
|
|
tokens = [t for t in re.findall(r"\w+", q, flags=re.UNICODE) if any(c.isalnum() for c in t)]
|
|
if not tokens:
|
|
return None
|
|
tokens[-1] = tokens[-1] + ":*"
|
|
return " & ".join(tokens)
|
|
|
|
|
|
def normalize_description(text: str | None) -> str | None:
|
|
"""Strip whitespace-only lines from a CHANNEL description before storing. YouTube channel
|
|
descriptions pad their contact/link blocks with runs of blank lines; keeping them wastes storage,
|
|
bloats the About popover, and adds nothing to the search document. Drops purely-empty lines and
|
|
right-strips each kept line; `str.splitlines()` also folds CR / CRLF and Unicode line separators
|
|
to "\\n" (a benign line-ending normalization). None stays None; all-blank collapses to "".
|
|
|
|
Deliberately CHANNEL-only — video descriptions are left with their original formatting: they are
|
|
user-facing prose where paragraph breaks carry meaning, unlike a channel's metadata block."""
|
|
if text is None:
|
|
return None
|
|
kept = [line.rstrip() for line in text.splitlines() if line.strip()]
|
|
return "\n".join(kept)
|