128 lines
4.5 KiB
Python
128 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Probe which assign channels are actually live vs youtubei."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import urllib.request
|
|
|
|
import paramiko
|
|
|
|
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 http_get(url: str, timeout: float = 12.0) -> tuple[int, str]:
|
|
req = urllib.request.Request(
|
|
url,
|
|
headers={
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
|
"Accept-Language": "en-US,en;q=0.9",
|
|
},
|
|
)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=timeout) as r:
|
|
return r.status, r.read().decode("utf-8", "replace")
|
|
except Exception as ex:
|
|
return 0, f"{type(ex).__name__}: {ex}"
|
|
|
|
|
|
def page_looks_live(html: str) -> dict:
|
|
low = html.lower()
|
|
hits = {
|
|
"isLiveNow": '"islivenow":true' in low or '"isLiveNow":true' in html,
|
|
"LIVE_badge": "\"style\":\"LIVE\"" in html or ">LIVE<" in html,
|
|
"watching_now": "watching now" in low or "watching" in low and "viewers" in low,
|
|
"live_stream": "live stream" in low or "livestream" in low,
|
|
"hqdefault_live": "hqdefault_live" in low,
|
|
}
|
|
# videoId from canonical /watch
|
|
vid = ""
|
|
import re
|
|
|
|
m = re.search(r"https://www\.youtube\.com/watch\?v=([a-zA-Z0-9_-]{6,})", html)
|
|
if m:
|
|
vid = m.group(1)
|
|
if not vid:
|
|
m = re.search(r'"videoId":"([a-zA-Z0-9_-]{11})".{0,80}"isLiveNow":true', html)
|
|
if m:
|
|
vid = m.group(1)
|
|
hits["video_id"] = vid
|
|
hits["likely_live"] = bool(hits["isLiveNow"] or hits["hqdefault_live"] or (hits["LIVE_badge"] and vid))
|
|
return hits
|
|
|
|
|
|
def main():
|
|
print("=== public channel pages (from this machine) ===")
|
|
for name, cid in CARDS:
|
|
url = f"https://www.youtube.com/channel/{cid}/live"
|
|
code, body = http_get(url)
|
|
if code != 200:
|
|
# fallback channel home
|
|
code2, body2 = http_get(f"https://www.youtube.com/channel/{cid}")
|
|
print(f"{name} {cid} /live->{code} home->{code2}")
|
|
body = body2 if code2 == 200 else body
|
|
else:
|
|
print(f"{name} {cid} /live->{code}")
|
|
info = page_looks_live(body if isinstance(body, str) else "")
|
|
print(" ", info)
|
|
|
|
# VPS youtubei all 8
|
|
print("\n=== VPS youtubei batch ===")
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=30, allow_agent=False, look_for_keys=False)
|
|
script = r'''
|
|
import asyncio, os, sys
|
|
os.chdir("/opt/douyinWEB/backend"); sys.path.insert(0,".")
|
|
from app import youtube_client
|
|
URLS=[
|
|
"https://www.youtube.com/channel/UCXFErW9DeE8EXrInlnyCnVw",
|
|
"https://www.youtube.com/channel/UCYnwXkCMmVFf8Vr6MlWb-Jw",
|
|
"https://www.youtube.com/channel/UCf-uYs6f5yg3iWylnHkIKug",
|
|
"https://www.youtube.com/channel/UC-8V3pDMlM2B75fvbYRBsNg",
|
|
"https://www.youtube.com/channel/UC0ojQ8aKSSOse1z-FGcUKUw",
|
|
"https://www.youtube.com/channel/UCtVfMFdLTN4ap__8g7MFtVw",
|
|
"https://www.youtube.com/channel/UCjQ8jnBm2N4FGRowNXcXThw",
|
|
"https://www.youtube.com/channel/UCO9wMc9tj9mD221Gu0rLBeA",
|
|
]
|
|
async def main():
|
|
# also try /live URLs
|
|
live_urls=[u+"/live" for u in URLS]
|
|
for label, items in [("channel", URLS), ("channel/live", live_urls)]:
|
|
data=await youtube_client.batch_live(items)
|
|
n=0
|
|
print("BATCH", label)
|
|
for r in data.get("results") or []:
|
|
if r.get("is_live"):
|
|
n+=1
|
|
live=r.get("live") or {}
|
|
print(" LIVE", r.get("channel_id"), live.get("title"), live.get("url"))
|
|
else:
|
|
print(" off ", r.get("channel_id"), "err", (r.get("error") or "")[:50])
|
|
print(" live_n", n)
|
|
asyncio.run(main())
|
|
'''
|
|
sftp = c.open_sftp()
|
|
with sftp.file("/tmp/_probe_all_live.py", "w") as f:
|
|
f.write(script)
|
|
sftp.close()
|
|
_, o, e = c.exec_command("/opt/douyinWEB/backend/.venv/bin/python /tmp/_probe_all_live.py", timeout=120)
|
|
print((o.read() + e.read()).decode("utf-8", "replace"))
|
|
c.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|