# -*- coding: utf-8 -*- """Deploy OAuth/profile fixes and probe + refresh profile B channels on dy.nomadro.live.""" from __future__ import annotations import sys import time from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") HOST = "108.174.50.68" USER = "root" PASSWORD = "Xiao4669805" REPO = "/opt/douyinWEB" ROOT = Path(__file__).resolve().parents[1] FILES = [ "backend/app/shorts_oauth.py", "backend/app/youtube_manage.py", ] REMOTE_PROBE = r""" cd /opt/douyinWEB/backend .venv/bin/python - <<'PY' import asyncio import json from app import db, shorts_oauth, youtube_manage from app.shorts_upload import refresh_access_token def _yt_api_disabled(msg: str) -> bool: m = str(msg or "") return "has not been used in project" in m or "youtube.googleapis.com" in m async def probe(pk: str) -> None: ch = db.get_shorts_channel(pk) or {} print( "===", pk[:8], "|", ch.get("name"), "| profile", ch.get("oauth_profile_id"), "| yt_id", ch.get("youtube_channel_id") or "(empty)", "| profile_at", ch.get("yt_profile_at"), ) creds = shorts_oauth.channel_oauth_creds(ch) if not creds.get("refresh_token"): print(" skip: no refresh_token") return try: token = await refresh_access_token( client_id=creds["client_id"], client_secret=creds["client_secret"], refresh_token=creds["refresh_token"], ) except Exception as exc: print(" token FAIL:", exc) db.update_shorts_channel(pk, oauth_ok=0, oauth_error=str(exc)) return import httpx async with httpx.AsyncClient(timeout=30, trust_env=False) as client: r = await client.get( "https://www.googleapis.com/youtube/v3/channels", params={"part": "snippet,statistics", "mine": "true"}, headers={"Authorization": f"Bearer {token}"}, ) data = r.json() if r.content else {} items = data.get("items") or [] err = (data.get("error") or {}).get("message") or "" print(" channels mine=true:", r.status_code, "items", len(items)) if err: print(" err:", err[:240]) if r.status_code >= 400 or not items: hint = ( "Google 项目未启用 YouTube Data API v3,请到 Cloud Console 启用后重授权" if _yt_api_disabled(err) else (err or "channels API 无数据") ) db.update_shorts_channel(pk, oauth_ok=1, oauth_error=hint) print(" set oauth_error:", hint[:120]) return for it in items[:5]: sn = it.get("snippet") or {} st = it.get("statistics") or {} print( " ", it.get("id"), sn.get("title"), sn.get("customUrl"), "videos", st.get("videoCount"), ) try: prof = await youtube_manage.fetch_channel_profile(pk) db.update_shorts_channel(pk, oauth_error="") print( " fetch_profile OK:", prof.get("title"), prof.get("youtube_channel_id"), "subs", prof.get("subscriber_count"), ) except Exception as exc: db.update_shorts_channel(pk, oauth_error=str(exc)) print(" fetch_profile FAIL:", exc) async def main() -> None: rows = [ c for c in db.list_shorts_channels(enabled=None) if str(c.get("oauth_profile_id") or "").upper() == "B" ] print("B channels:", len(rows)) for ch in rows: await probe(str(ch.get("id") or "")) asyncio.run(main()) PY """ def main() -> None: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, 22, USER, PASSWORD, timeout=45, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() for rel in FILES: local = ROOT / rel remote = f"{REPO}/{rel.replace(chr(92), '/')}" print(f"upload {rel} -> {remote}") sftp.put(str(local), remote) sftp.close() print("restart douyinweb-api...") c.exec_command("systemctl restart douyinweb-api", timeout=10) time.sleep(6) _i, o, _e = c.exec_command("systemctl is-active douyinweb-api", timeout=15) print("api:", o.read().decode().strip()) print("\n=== probe + refresh B channels ===") _i, o, e = c.exec_command(REMOTE_PROBE, timeout=180) print(o.read().decode("utf-8", "replace")) err = e.read().decode("utf-8", "replace").strip() if err: print("STDERR:", err[:2000]) c.close() if __name__ == "__main__": main()