douyinWEB/scripts/_fix_yt_profile_proxy.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

107 lines
3.8 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy YouTube profile proxy fix + refresh all channel stats."""
from __future__ import annotations
import json
import sys
import time
from pathlib import Path
import httpx
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
FILES = [
("backend/app/egress_hub.py", "/tmp/_up_egress_hub.py"),
("backend/app/youtube_manage.py", "/tmp/_up_youtube_manage.py"),
("backend/app/shorts_upload.py", "/tmp/_up_shorts_upload.py"),
("backend/app/shorts_oauth.py", "/tmp/_up_shorts_oauth.py"),
]
def main():
g = paramiko.SSHClient()
g.set_missing_host_key_policy(paramiko.AutoAddPolicy())
g.connect("107.173.30.245", 4014, "eric", "4669805", timeout=40, banner_timeout=80, allow_agent=False, look_for_keys=False)
sftp = g.open_sftp()
for rel, remote in FILES:
sftp.put(str(ROOT / rel), remote)
sftp.close()
cmd = r"""
echo '4669805' | sudo -S -p '' bash -lc '
set -e
install -m 644 /tmp/_up_egress_hub.py /opt/douyinWEB/backend/app/egress_hub.py
install -m 644 /tmp/_up_youtube_manage.py /opt/douyinWEB/backend/app/youtube_manage.py
install -m 644 /tmp/_up_shorts_upload.py /opt/douyinWEB/backend/app/shorts_upload.py
install -m 644 /tmp/_up_shorts_oauth.py /opt/douyinWEB/backend/app/shorts_oauth.py
cd /opt/douyinWEB/backend
.venv/bin/python -m py_compile app/egress_hub.py app/youtube_manage.py app/shorts_upload.py app/shorts_oauth.py
# ensure egress_mode=shellcrash
.venv/bin/python - <<PY
from app import db, egress_hub
db.update_settings_map({"egress_mode": "shellcrash"})
print("mode", egress_hub.get_mode())
print("google_proxy", egress_hub.google_http_proxy())
PY
systemctl restart douyinweb-api
for i in $(seq 1 20); do
c=$(curl -sS -m 3 -o /dev/null -w "%{http_code}" http://127.0.0.1:8014/api/health || echo x)
echo health $c
[ "$c" = "200" ] && break
sleep 2
done
# refresh all profiles
.venv/bin/python - <<PY
import asyncio
from app import db, youtube_manage
chs = db.list_shorts_channels()
print("channels", len(chs))
async def run():
for c in chs:
pk = c["id"]
name = c.get("name")
try:
p = await youtube_manage.fetch_channel_profile(pk)
st = (p.get("statistics") or p.get("stats") or {})
if not st and isinstance(p.get("snippet"), dict):
st = p.get("statistics") or {}
# profile shape from fetch_channel_profile
print("OK", name, p.get("title") or p.get("name"), "subs", p.get("subscriberCount") or (p.get("statistics") or {}).get("subscriberCount"), "vids", p.get("videoCount") or (p.get("statistics") or {}).get("videoCount"))
except Exception as e:
print("FAIL", name, type(e).__name__, e)
asyncio.run(run())
PY
'
"""
_, o, e = g.exec_command(cmd, timeout=300)
print((o.read() + e.read()).decode("utf-8", "replace"), flush=True)
g.close()
with httpx.Client(base_url="https://dy.nomadro.com", timeout=60) as c:
c.post("/api/auth/login", json={"username": "hackrobot", "password": "Xiao4669805@"})
# also hit refresh endpoint
r = c.post("/api/youtube/profiles/refresh?max_age_sec=0&limit=20")
print("refresh_api", r.status_code, r.text[:500], flush=True)
st = c.get("/api/shorts/status").json()
for ch in (st.get("board") or {}).get("channels") or []:
yp = ch.get("yt_profile") or {}
stats = yp.get("statistics") or yp.get("stats") or {}
print(
ch.get("name"),
"subs",
stats.get("subscriberCount") or yp.get("subscriberCount"),
"vids",
stats.get("videoCount") or yp.get("videoCount"),
"views",
stats.get("viewCount") or yp.get("viewCount"),
flush=True,
)
if __name__ == "__main__":
main()