#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import re import sys import urllib.request sys.stdout.reconfigure(encoding="utf-8", errors="replace") CARDS = [ ("asian streetfood", "UCXFErW9DeE8EXrInlnyCnVw"), ("hackrobotlite", "UCYnwXkCMmVFf8Vr6MlWb-Jw"), ("nomad Street Food", "UCf-uYs6f5yg3iWylnHkIKug"), ("Nomadro Hub", "UC-8V3pDMlM2B75fvbYRBsNg"), ("walking tour", "UC0ojQ8aKSSOse1z-FGcUKUw"), ("nomad vlog", "UCtVfMFdLTN4ap__8g7MFtVw"), ("asian streetfood2", "UCjQ8jnBm2N4FGRowNXcXThw"), ("nomadvlog", "UCO9wMc9tj9mD221Gu0rLBeA"), ] def fetch(url: str) -> str: req = urllib.request.Request( url, headers={"User-Agent": "Mozilla/5.0", "Accept-Language": "en-US,en;q=0.9"}, ) with urllib.request.urlopen(req, timeout=20) as r: return r.read().decode("utf-8", "replace") def parse_live(html: str) -> dict: out = {"is_live": False, "video_id": "", "title": ""} # ytInitialPlayerResponse for pat in ( r"ytInitialPlayerResponse\s*=\s*(\{.+?\})\s*;", r"var\s+ytInitialPlayerResponse\s*=\s*(\{.+?\})\s*;", ): m = re.search(pat, html, re.S) if not m: continue raw = m.group(1) # trim balanced-ish: find first { to matching via json raw_decode try: dec = json.JSONDecoder() obj, _ = dec.raw_decode(html[m.start(1) :]) except Exception: try: obj = json.loads(raw) except Exception: continue vd = obj.get("videoDetails") or {} out["video_id"] = str(vd.get("videoId") or "") out["title"] = str(vd.get("title") or "")[:80] out["is_live"] = bool(vd.get("isLive") or vd.get("isLiveContent")) micro = ((obj.get("microformat") or {}).get("playerMicroformatRenderer") or {}) if micro.get("liveBroadcastDetails", {}).get("isLiveNow"): out["is_live"] = True break if not out["is_live"]: if '"isLiveNow":true' in html or '"isLive":true' in html: out["is_live"] = True return out def main(): live_n = 0 for name, cid in CARDS: url = f"https://www.youtube.com/channel/{cid}/live" try: html = fetch(url) info = parse_live(html) except Exception as ex: print(name, "ERR", ex) continue if info["is_live"]: live_n += 1 print("LIVE", name, cid, info) else: print("off ", name, cid, info) print("TOTAL_LIVE", live_n) if __name__ == "__main__": main()