|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Extract guest metadata from a scheduled issue and write guest-promo.json.""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +import re |
| 8 | +import sys |
| 9 | +import urllib.request |
| 10 | +from datetime import datetime |
| 11 | + |
| 12 | +GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN", "") |
| 13 | +REPO = "githubevents/open-source-friday" |
| 14 | +HOST_NAMES = { |
| 15 | + "AndreaGriffiths11": "Andrea Griffiths", |
| 16 | + "KevinCrosby": "Kevin Crosby", |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +def fetch_issue(number: int) -> dict: |
| 21 | + url = f"https://api.github.com/repos/{REPO}/issues/{number}" |
| 22 | + req = urllib.request.Request(url, headers={ |
| 23 | + "Authorization": f"token {GITHUB_TOKEN}", |
| 24 | + "Accept": "application/vnd.github.v3+json", |
| 25 | + "User-Agent": "osf-guest-promo", |
| 26 | + }) |
| 27 | + with urllib.request.urlopen(req) as resp: |
| 28 | + return json.loads(resp.read()) |
| 29 | + |
| 30 | + |
| 31 | +def parse_field(body: str, field: str) -> str: |
| 32 | + m = re.search(rf"### {re.escape(field)}\s*\n+(.+?)(?:\n###|\Z)", body, re.DOTALL) |
| 33 | + if m: |
| 34 | + val = m.group(1).strip() |
| 35 | + if val.upper() not in ("_NO RESPONSE_", "TBD", "NOT YET", ""): |
| 36 | + return val |
| 37 | + return "" |
| 38 | + |
| 39 | + |
| 40 | +def parse_date(raw: str) -> str: |
| 41 | + for fmt in ("%m-%d-%Y", "%m-%d-%y", "%m/%d/%Y", "%B %d, %Y", "%B %-d, %Y"): |
| 42 | + try: |
| 43 | + return datetime.strptime(raw.strip(), fmt).strftime("%B %-d, %Y") |
| 44 | + except ValueError: |
| 45 | + continue |
| 46 | + return raw |
| 47 | + |
| 48 | + |
| 49 | +def main() -> None: |
| 50 | + if len(sys.argv) < 2: |
| 51 | + print("Usage: extract_guest_metadata.py <issue_number>") |
| 52 | + sys.exit(1) |
| 53 | + |
| 54 | + number = int(sys.argv[1]) |
| 55 | + issue = fetch_issue(number) |
| 56 | + body = issue.get("body") or "" |
| 57 | + title = issue.get("title") or "" |
| 58 | + |
| 59 | + guest_name = parse_field(body, "Name") |
| 60 | + github_handle = parse_field(body, "GitHub Handle").lstrip("@") |
| 61 | + bio = parse_field(body, "Tell us about yourself") |
| 62 | + project_name = parse_field(body, "Project Name") |
| 63 | + project_url = parse_field(body, "Project Repo Link") |
| 64 | + raw_date = parse_field(body, "Dates") |
| 65 | + |
| 66 | + # Calendly-style body: "Name: Angela Wen @handle" |
| 67 | + if not guest_name: |
| 68 | + m = re.search(r"Name:\s+(.+?)(?:\s*@\S+)?\s*$", body, re.MULTILINE) |
| 69 | + if m: |
| 70 | + guest_name = m.group(1).strip() |
| 71 | + |
| 72 | + # Title-based date fallback |
| 73 | + if not raw_date or raw_date.upper() in ("TBD", "_NO RESPONSE_", "NOT YET", ""): |
| 74 | + m = re.search(r"(\d{1,2}[-/]\d{1,2}[-/]\d{2,4})", title) |
| 75 | + if m: |
| 76 | + raw_date = m.group(1) |
| 77 | + |
| 78 | + stream_date = parse_date(raw_date) if raw_date else "Date TBD" |
| 79 | + |
| 80 | + assignees = issue.get("assignees") or [] |
| 81 | + host_name = "TBD" |
| 82 | + if assignees: |
| 83 | + login = assignees[0]["login"] |
| 84 | + host_name = HOST_NAMES.get(login, login) |
| 85 | + |
| 86 | + # Truncate bio for video overlay |
| 87 | + if bio and len(bio) > 280: |
| 88 | + bio = bio[:277] + "..." |
| 89 | + |
| 90 | + metadata = { |
| 91 | + "guest_name": guest_name or "Guest", |
| 92 | + "github_handle": github_handle, |
| 93 | + "project_name": project_name or "Open Source", |
| 94 | + "project_url": project_url, |
| 95 | + "bio": bio, |
| 96 | + "stream_date": stream_date, |
| 97 | + "stream_time": "1 PM ET", |
| 98 | + "host_name": host_name, |
| 99 | + "issue_number": number, |
| 100 | + "issue_url": issue["html_url"], |
| 101 | + "has_audio": False, # set to True by workflow after TTS succeeds |
| 102 | + } |
| 103 | + |
| 104 | + output_path = os.environ.get("METADATA_OUTPUT", "video/public/guest-promo.json") |
| 105 | + os.makedirs(os.path.dirname(output_path), exist_ok=True) |
| 106 | + with open(output_path, "w") as f: |
| 107 | + json.dump(metadata, f, indent=2) |
| 108 | + |
| 109 | + print(f"✅ Wrote metadata for '{guest_name}' → {output_path}") |
| 110 | + print(json.dumps(metadata, indent=2)) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments