douyinWEB/scripts/_probe_oauth_yt_svc.py
2026-09-13 22:10:49 -05:00

112 lines
4.3 KiB
Python

# -*- coding: utf-8 -*-
"""OAuth broadcasts via venv; youtube-svc /batch/live; asian studio snapshot."""
from __future__ import annotations
import sys
import paramiko
REMOTE = r'''
import json, os, sys, asyncio, urllib.request, sqlite3
os.chdir("/opt/douyinWEB/backend")
sys.path.insert(0, ".")
os.environ.setdefault("DY_YOUTUBE_SVC_URL", "http://127.0.0.1:8020")
# youtube-svc
print("=== youtube-svc batch/live ===")
body=json.dumps({"items":[
"https://www.youtube.com/channel/UCXFErW9DeE8EXrInlnyCnVw",
"https://www.youtube.com/channel/UCjQ8jnBm2N4FGRowNXcXThw",
"https://www.youtube.com/channel/UCYnwXkCMmVFf8Vr6MlWb-Jw",
"https://www.youtube.com/channel/UCf-uYs6f5yg3iWylnHkIKug",
]}).encode()
for path in ["/batch/live","/api/batch/live","/live/batch"]:
try:
req=urllib.request.Request("http://127.0.0.1:8020"+path, data=body, headers={"Content-Type":"application/json"}, method="POST")
raw=urllib.request.urlopen(req, timeout=120).read().decode()
print(path, raw[:2000])
break
except Exception as e:
print(path, "ERR", e)
async def main():
from app import db, youtube_manage
# discover API
names=[n for n in dir(db) if "assign" in n.lower()]
print("db assign fns", names)
groups=[]
if hasattr(db, "list_yt_assign_groups"):
groups=db.list_yt_assign_groups()
elif hasattr(db, "yt_assign_list_groups"):
groups=db.yt_assign_list_groups()
else:
con=sqlite3.connect("data/monitor.db"); con.row_factory=sqlite3.Row
groups=[dict(r) for r in con.execute("select * from yt_assign_groups where enabled=1")]
print("=== oauth ===")
for g in groups:
if not g.get("enabled") and "enabled" in g and not g["enabled"]:
continue
if g.get("enabled") in (0, False, "0"):
continue
pk=str(g.get("channel_pk") or "").strip()
cid=str(g.get("youtube_channel_id") or "").strip()
name=g.get("name")
if not pk:
print(name,"no pk"); continue
try:
ch=db.get_shorts_channel(pk) or {}
except Exception:
con=sqlite3.connect("data/monitor.db"); con.row_factory=sqlite3.Row
r=con.execute("select * from shorts_channels where id=?", (pk,)).fetchone()
ch=dict(r) if r else {}
if not str(ch.get("refresh_token") or "").strip():
print(name,"no token"); continue
try:
token,_=await youtube_manage._access_for_channel(pk)
data=await youtube_manage._get("liveBroadcasts", token=token, params={"part":"id,snippet,status","mine":"true","maxResults":25})
items=data.get("items") or []
rows=[]
for it in items:
st=str(((it.get("status") or {}).get("lifeCycleStatus") or "")).lower()
sn=it.get("snippet") or {}
rows.append((st, it.get("id"), (sn.get("title") or "")[:45]))
print(name, cid[:14], "live", [r for r in rows if r[0]=="live"], "status_set", sorted(set(r[0] for r in rows)), "n", len(rows))
except Exception as e:
print(name,"ERR", type(e).__name__, str(e)[:220])
# studio telemetry latest
print("=== studio telemetry files ===")
import glob
for p in sorted(glob.glob("/opt/douyinWEB/backend/data/neko/studio_telemetry*.json"))[:5]:
try:
obj=json.load(open(p,encoding="utf-8"))
print(p, json.dumps(obj, ensure_ascii=False)[:500])
except Exception as e:
print(p, e)
asyncio.run(main())
'''
def run(port, user, pwd, label):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("107.173.30.245", port=port, username=user, password=pwd, timeout=25)
py = "/opt/douyinWEB/backend/.venv/bin/python"
stdin, stdout, stderr = c.exec_command(f"cd /opt/douyinWEB/backend && {py} -", timeout=300)
stdin.write(REMOTE)
stdin.channel.shutdown_write()
print("########", label, "########")
out = stdout.read().decode("utf-8", "replace")
err = stderr.read().decode("utf-8", "replace")
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
print(out)
if err.strip():
print("STDERR", err[-2000:])
c.close()
if __name__ == "__main__":
run(22, "root", "Xiao4669805", "vps")
run(4014, "eric", "4669805", "phys")