Spaces:
Running
Running
k0880k commited on
Commit ·
fd79ed3
1
Parent(s): be4a882
fix(postmeet): retain YouTube source fields for template hydration
Browse files- tools/meeting_source_fields.py +167 -0
tools/meeting_source_fields.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Canonical source-field projection for PostMeet.
|
| 3 |
+
|
| 4 |
+
PostMeet resolves media before it understands a meeting. The media resolver is
|
| 5 |
+
intentionally small, but a publish target such as a Vlog/YOOtheme template may
|
| 6 |
+
already expose richer source fields (channel, thumbnail, description, publish
|
| 7 |
+
date, etc.). This adapter preserves those facts as data so downstream template
|
| 8 |
+
hydration does not have to scrape the source again or invent values.
|
| 9 |
+
|
| 10 |
+
For YouTube-like sources we ask yt-dlp for metadata only (never the media bytes).
|
| 11 |
+
For every other provider the semantic ``source_media`` descriptor remains the
|
| 12 |
+
source of truth.
|
| 13 |
+
"""
|
| 14 |
+
from __future__ import annotations
|
| 15 |
+
|
| 16 |
+
import datetime as _dt
|
| 17 |
+
import json
|
| 18 |
+
import shutil
|
| 19 |
+
import subprocess
|
| 20 |
+
from typing import Any, Dict, Mapping, Optional
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
YTDLP_FIELDS = (
|
| 24 |
+
"id", "title", "description", "thumbnail", "duration", "timestamp",
|
| 25 |
+
"release_timestamp", "upload_date", "live_status", "channel", "channel_id",
|
| 26 |
+
"channel_url", "uploader", "uploader_id", "uploader_url", "view_count",
|
| 27 |
+
"like_count", "comment_count", "tags", "categories", "license",
|
| 28 |
+
"webpage_url", "original_url",
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def _ytdlp(url: str) -> Dict[str, Any]:
|
| 33 |
+
exe = shutil.which("yt-dlp")
|
| 34 |
+
if not exe or not url:
|
| 35 |
+
return {}
|
| 36 |
+
try:
|
| 37 |
+
proc = subprocess.run(
|
| 38 |
+
[exe, "--dump-single-json", "--skip-download", "--no-warnings",
|
| 39 |
+
"--no-playlist", url],
|
| 40 |
+
capture_output=True, text=True, timeout=90,
|
| 41 |
+
)
|
| 42 |
+
if proc.returncode != 0 or not proc.stdout.strip():
|
| 43 |
+
return {}
|
| 44 |
+
data = json.loads(proc.stdout)
|
| 45 |
+
return data if isinstance(data, dict) else {}
|
| 46 |
+
except Exception:
|
| 47 |
+
return {}
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def _iso_timestamp(value: Any) -> str:
|
| 51 |
+
try:
|
| 52 |
+
if value in (None, ""):
|
| 53 |
+
return ""
|
| 54 |
+
return _dt.datetime.fromtimestamp(float(value), tz=_dt.timezone.utc).isoformat().replace("+00:00", "Z")
|
| 55 |
+
except (TypeError, ValueError, OSError, OverflowError):
|
| 56 |
+
return ""
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def _upload_date(value: Any) -> str:
|
| 60 |
+
raw = str(value or "").strip()
|
| 61 |
+
if len(raw) == 8 and raw.isdigit():
|
| 62 |
+
return f"{raw[:4]}-{raw[4:6]}-{raw[6:8]}"
|
| 63 |
+
return raw
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def duration_label(seconds: Any) -> str:
|
| 67 |
+
try:
|
| 68 |
+
total = max(0, int(round(float(seconds))))
|
| 69 |
+
except (TypeError, ValueError):
|
| 70 |
+
return ""
|
| 71 |
+
h, rem = divmod(total, 3600)
|
| 72 |
+
m, s = divmod(rem, 60)
|
| 73 |
+
return f"{h:d}:{m:02d}:{s:02d}" if h else f"{m:d}:{s:02d}"
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def from_metadata(source_media: Mapping[str, Any], metadata: Optional[Mapping[str, Any]] = None) -> Dict[str, Any]:
|
| 77 |
+
"""Build the bounded, template-facing source field set.
|
| 78 |
+
|
| 79 |
+
``metadata`` is injectable so tests and non-YouTube adapters can use the
|
| 80 |
+
exact same projection without shelling out to yt-dlp.
|
| 81 |
+
"""
|
| 82 |
+
media = dict(source_media or {})
|
| 83 |
+
info = dict(metadata or {})
|
| 84 |
+
|
| 85 |
+
channel = str(info.get("channel") or info.get("uploader") or media.get("creator") or "")
|
| 86 |
+
canonical = str(info.get("webpage_url") or info.get("original_url") or media.get("canonical_ref") or media.get("input_ref") or "")
|
| 87 |
+
seconds = info.get("duration")
|
| 88 |
+
if seconds in (None, ""):
|
| 89 |
+
seconds = media.get("duration_seconds")
|
| 90 |
+
|
| 91 |
+
published_at = _iso_timestamp(info.get("release_timestamp") or info.get("timestamp"))
|
| 92 |
+
upload_date = _upload_date(info.get("upload_date"))
|
| 93 |
+
if not published_at:
|
| 94 |
+
published_at = upload_date
|
| 95 |
+
|
| 96 |
+
fields: Dict[str, Any] = {
|
| 97 |
+
"provider": str(media.get("provider") or ""),
|
| 98 |
+
"source_url": canonical,
|
| 99 |
+
"video_url": canonical,
|
| 100 |
+
"video_id": str(info.get("id") or ""),
|
| 101 |
+
"video_title": str(info.get("title") or media.get("title") or ""),
|
| 102 |
+
"description": str(info.get("description") or ""),
|
| 103 |
+
"thumbnail": str(info.get("thumbnail") or ""),
|
| 104 |
+
"duration_seconds": seconds,
|
| 105 |
+
"duration": duration_label(seconds),
|
| 106 |
+
"published_at": published_at,
|
| 107 |
+
"upload_date": upload_date,
|
| 108 |
+
"live_status": str(info.get("live_status") or ""),
|
| 109 |
+
"channel": channel,
|
| 110 |
+
"channel_name": channel,
|
| 111 |
+
"channel_id": str(info.get("channel_id") or info.get("uploader_id") or ""),
|
| 112 |
+
"channel_url": str(info.get("channel_url") or info.get("uploader_url") or ""),
|
| 113 |
+
"uploader": str(info.get("uploader") or channel),
|
| 114 |
+
"uploader_id": str(info.get("uploader_id") or ""),
|
| 115 |
+
"uploader_url": str(info.get("uploader_url") or ""),
|
| 116 |
+
"view_count": info.get("view_count"),
|
| 117 |
+
"like_count": info.get("like_count"),
|
| 118 |
+
"comment_count": info.get("comment_count"),
|
| 119 |
+
"tags": list(info.get("tags") or []),
|
| 120 |
+
"categories": list(info.get("categories") or []),
|
| 121 |
+
"license": str(info.get("license") or ""),
|
| 122 |
+
}
|
| 123 |
+
# Keep the object deterministic and JSON-safe while retaining explicit zero
|
| 124 |
+
# counts. Empty strings/lists are useful because the template can declare a
|
| 125 |
+
# field without us pretending a value exists.
|
| 126 |
+
return fields
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def extract(source_media: Mapping[str, Any], source_ref: str = "") -> Dict[str, Any]:
|
| 130 |
+
media = dict(source_media or {})
|
| 131 |
+
provider = str(media.get("provider") or "").lower()
|
| 132 |
+
metadata: Dict[str, Any] = {}
|
| 133 |
+
if provider == "youtube":
|
| 134 |
+
raw = _ytdlp(str(source_ref or media.get("canonical_ref") or media.get("input_ref") or ""))
|
| 135 |
+
metadata = {k: raw.get(k) for k in YTDLP_FIELDS if k in raw}
|
| 136 |
+
return from_metadata(media, metadata)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def apply(payload: Dict[str, Any], source_media: Mapping[str, Any], source_ref: str = "") -> Dict[str, Any]:
|
| 140 |
+
"""Attach source fields and convenient canonical aliases to ``payload``.
|
| 141 |
+
|
| 142 |
+
Existing meeting facts win. Source metadata fills blanks; it never replaces
|
| 143 |
+
a title/summary/poster that a later meeting specialist has established.
|
| 144 |
+
"""
|
| 145 |
+
fields = extract(source_media, source_ref)
|
| 146 |
+
payload["source_fields"] = fields
|
| 147 |
+
|
| 148 |
+
aliases = {
|
| 149 |
+
"poster": fields.get("thumbnail"),
|
| 150 |
+
"duration": fields.get("duration"),
|
| 151 |
+
"duration_seconds": fields.get("duration_seconds"),
|
| 152 |
+
"channel": fields.get("channel"),
|
| 153 |
+
"channel_name": fields.get("channel_name"),
|
| 154 |
+
"channel_id": fields.get("channel_id"),
|
| 155 |
+
"channel_url": fields.get("channel_url"),
|
| 156 |
+
"video_id": fields.get("video_id"),
|
| 157 |
+
"source_url": fields.get("source_url"),
|
| 158 |
+
"source_title": fields.get("video_title"),
|
| 159 |
+
"source_description": fields.get("description"),
|
| 160 |
+
"source_published_at": fields.get("published_at"),
|
| 161 |
+
"source_provider": fields.get("provider"),
|
| 162 |
+
}
|
| 163 |
+
for key, value in aliases.items():
|
| 164 |
+
if payload.get(key) in (None, "", []):
|
| 165 |
+
if value not in (None, "", []):
|
| 166 |
+
payload[key] = value
|
| 167 |
+
return fields
|