152 lines
6.1 KiB
Python
152 lines
6.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""OAuth + live-map via API venv and session cookie."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import paramiko
|
|
|
|
REMOTE = r'''
|
|
import json, os, sys, asyncio, urllib.request, http.cookiejar
|
|
os.chdir("/opt/douyinWEB/backend")
|
|
sys.path.insert(0, ".")
|
|
|
|
# mimic API env
|
|
os.environ.setdefault("DY_YOUTUBE_SVC_URL", "http://127.0.0.1:8020")
|
|
|
|
async def main():
|
|
from app import db, youtube_live
|
|
from app import youtube_manage
|
|
|
|
groups = [g for g in db.list_yt_assign_groups() if g.get("enabled")]
|
|
print("=== oauth liveBroadcasts ===")
|
|
for g in groups:
|
|
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
|
|
ch = db.get_shorts_channel(pk) or {}
|
|
if not str(ch.get("refresh_token") or "").strip():
|
|
print(name, "no refresh_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 []
|
|
lives=[]
|
|
for it in items:
|
|
st=str(((it.get("status") or {}).get("lifeCycleStatus") or "")).lower()
|
|
sn=it.get("snippet") or {}
|
|
lives.append((st, it.get("id"), (sn.get("title") or "")[:50]))
|
|
live_only=[x for x in lives if x[0]=="live"]
|
|
print(name, (cid or "-")[:14], "n", len(items), "live", live_only, "all_status", sorted(set(x[0] for x in lives)))
|
|
except Exception as e:
|
|
print(name, "OAUTH_ERR", type(e).__name__, str(e)[:200])
|
|
|
|
print("=== build_live_map force ===")
|
|
youtube_live._CACHE["at"]=0
|
|
youtube_live._CACHE["payload"]=None
|
|
p = await youtube_live.build_live_map(force=True)
|
|
print("live_count", p.get("live_count"), "source", p.get("source"), "svc_error", (p.get("svc_error") or "")[:120])
|
|
for k,v in sorted((p.get("entries") or {}).items()):
|
|
if k.startswith("assign:"):
|
|
print((" LIVE" if v.get("is_live") else " off "), k, v.get("source"), (v.get("title") or "")[:36], (v.get("live_url") or "")[:70], "err", (v.get("error") or "")[:80])
|
|
|
|
asyncio.run(main())
|
|
'''
|
|
|
|
|
|
def run(port, user, pwd, label, py="/opt/douyinWEB/backend/.venv/bin/python"):
|
|
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)
|
|
# also curl live-map with login
|
|
login = r'''
|
|
python3 - <<'PY'
|
|
import json,urllib.request,http.cookiejar
|
|
cj=http.cookiejar.CookieJar()
|
|
op=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
|
|
# try login
|
|
for path in ["/api/auth/login","/api/session/login","/api/login"]:
|
|
try:
|
|
req=urllib.request.Request("http://127.0.0.1:8014"+path, data=json.dumps({"username":"hackrobot","password":"Xiao4669805@"}).encode(), headers={"Content-Type":"application/json"}, method="POST")
|
|
r=op.open(req, timeout=15)
|
|
print("login", path, r.status, r.read()[:80])
|
|
break
|
|
except Exception as e:
|
|
print("login fail", path, e)
|
|
try:
|
|
raw=op.open("http://127.0.0.1:8014/api/youtube/live-map?force=1", timeout=120).read()
|
|
p=json.loads(raw)
|
|
print("api live_count", p.get("live_count"), "source", p.get("source"), "svc", (p.get("svc_error") or "")[:100])
|
|
for k,v in (p.get("entries") or {}).items():
|
|
if v.get("is_live"):
|
|
print(" LIVE", k, v.get("source"), (v.get("title") or "")[:40], (v.get("live_url") or "")[:80])
|
|
except Exception as e:
|
|
print("api map ERR", e)
|
|
PY
|
|
'''
|
|
stdin, stdout, stderr = c.exec_command(
|
|
f"cd /opt/douyinWEB/backend && {py} - <<'PY'\n" + REMOTE + "\nPY\n" + login,
|
|
timeout=300,
|
|
)
|
|
# Wait - heredoc nesting is messy. Use stdin pipe instead.
|
|
c.close()
|
|
|
|
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)
|
|
stdin, stdout, stderr = c.exec_command(f"cd /opt/douyinWEB/backend && {py} -", timeout=300)
|
|
stdin.write(REMOTE)
|
|
stdin.channel.shutdown_write()
|
|
print("########", label, "venv ########")
|
|
print(stdout.read().decode("utf-8", "replace"))
|
|
err = stderr.read().decode("utf-8", "replace")
|
|
if err.strip():
|
|
print("STDERR", err[-2500:])
|
|
c.close()
|
|
|
|
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)
|
|
stdin, stdout, stderr = c.exec_command("python3 -", timeout=180)
|
|
stdin.write(
|
|
"""
|
|
import json,urllib.request,http.cookiejar
|
|
cj=http.cookiejar.CookieJar()
|
|
op=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
|
|
for path in ["/api/auth/login","/api/session/login","/api/login"]:
|
|
try:
|
|
req=urllib.request.Request("http://127.0.0.1:8014"+path, data=json.dumps({"username":"hackrobot","password":"Xiao4669805@"}).encode(), headers={"Content-Type":"application/json"}, method="POST")
|
|
r=op.open(req, timeout=15)
|
|
print("login", path, r.status, r.read()[:120])
|
|
break
|
|
except Exception as e:
|
|
print("login fail", path, type(e).__name__, e)
|
|
try:
|
|
raw=op.open("http://127.0.0.1:8014/api/youtube/live-map?force=1", timeout=120).read()
|
|
p=json.loads(raw)
|
|
print("api live_count", p.get("live_count"), "source", p.get("source"), "svc", (p.get("svc_error") or "")[:120])
|
|
for k,v in (p.get("entries") or {}).items():
|
|
if v.get("is_live"):
|
|
print(" LIVE", k, v.get("source"), (v.get("title") or "")[:40], (v.get("live_url") or "")[:80])
|
|
except Exception as e:
|
|
print("api map ERR", type(e).__name__, e)
|
|
"""
|
|
)
|
|
stdin.channel.shutdown_write()
|
|
print("########", label, "api ########")
|
|
print(stdout.read().decode("utf-8", "replace"))
|
|
err = stderr.read().decode("utf-8", "replace")
|
|
if err.strip():
|
|
print("STDERR", err[-800:])
|
|
c.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(22, "root", "Xiao4669805", "vps")
|
|
run(4014, "eric", "4669805", "phys")
|