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

165 lines
5.7 KiB
Python

#!/usr/bin/env python3
"""Create missing assign groups on dylive from dy (china/nice street food)."""
from __future__ import annotations
import json
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
SSH_PASS = "Xiao4669805"
NEED = {"china street food", "nice street food"}
def ssh(host: str):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(8):
try:
c.connect(
host,
22,
"root",
SSH_PASS,
timeout=50,
allow_agent=False,
look_for_keys=False,
banner_timeout=180,
)
return c
except Exception as e:
print("retry", host, i, e, flush=True)
time.sleep(2 + i)
raise SystemExit("ssh fail")
def run(c, cmd: str, t: int = 180) -> str:
_i, o, e = c.exec_command(cmd, timeout=t)
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out[-8000:] if len(out) > 8000 else out, flush=True)
return out
dy = ssh("108.174.50.68")
out = run(
dy,
r"""
cd /opt/douyinWEB/backend
./.venv/bin/python - <<'PY'
import json
from app import yt_assign_db
gs = yt_assign_db.list_groups(include_urls=True, include_secret=True)
need = {'china street food', 'nice street food'}
picked = []
for g in gs:
if str(g.get('name') or '') in need:
picked.append({
'name': g.get('name'),
'channel_pk': g.get('channel_pk') or '',
'youtube_channel_id': g.get('youtube_channel_id') or '',
'stream_key': g.get('stream_key') or '',
'note': g.get('note') or '',
'enabled': bool(g.get('enabled', True)),
'category': g.get('category') or 'normal',
'pool': [{'url': p.get('url'), 'title': p.get('title') or '', 'room_id': p.get('room_id') or ''} for p in (g.get('pool') or []) if p.get('url')],
'featured': [{'url': p.get('url'), 'title': p.get('title') or '', 'room_id': p.get('room_id') or ''} for p in (g.get('featured') or []) if p.get('url')],
'slots': [{
'url': p.get('url'), 'title': p.get('title') or '', 'room_id': p.get('room_id') or '',
'weekday': p.get('weekday'), 'start_minute': p.get('start_minute'), 'end_minute': p.get('end_minute'),
} for p in (g.get('slots') or [])],
})
print('PICK_START')
print(json.dumps(picked, ensure_ascii=False))
print('PICK_END')
print('picked', len(picked))
PY
""",
)
dy.close()
a, b = out.find("PICK_START"), out.find("PICK_END")
assert a >= 0 and b > a
picked = json.loads(out[a + len("PICK_START") : b].strip())
print(f"got {len(picked)} groups", flush=True)
dl = ssh("107.173.30.245")
sftp = dl.open_sftp()
with sftp.file("/tmp/dy_missing_assign.json", "w") as f:
f.write(json.dumps(picked, ensure_ascii=False))
sftp.close()
run(
dl,
r"""
cd /opt/douyinWEB/backend
./.venv/bin/python - <<'PY'
import json
from app import yt_assign_db
with open('/tmp/dy_missing_assign.json', encoding='utf-8') as f:
picked = json.load(f)
existing = {str(g.get('name') or ''): g for g in yt_assign_db.list_groups(include_urls=False, include_secret=True)}
by_cid = {str(g.get('youtube_channel_id') or ''): g for g in existing.values()}
for src in picked:
name = src['name']
ycid = src.get('youtube_channel_id') or ''
if name in existing or (ycid and ycid in by_cid):
print('SKIP_EXISTS', name, ycid)
# still fill stream key if empty
g = existing.get(name) or by_cid.get(ycid)
if g and len(str(g.get('stream_key') or '')) < 12 and len(str(src.get('stream_key') or '')) >= 12:
yt_assign_db.update_group(g['id'], stream_key=src['stream_key'])
print(' filled_key', name)
continue
try:
g = yt_assign_db.create_group(
name=name,
channel_pk=src.get('channel_pk') or '',
youtube_channel_id=ycid,
stream_key=src.get('stream_key') or '',
note=src.get('note') or '',
enabled=bool(src.get('enabled', True)),
category=src.get('category') or 'normal',
)
print('CREATED', name, g.get('id'), 'neko', g.get('neko_instance_id'))
gid = g['id']
pool_urls = [p['url'] for p in (src.get('pool') or []) if p.get('url')]
if pool_urls:
try:
r = yt_assign_db.add_pool_urls(gid, urls=pool_urls)
print(' pool', r)
except Exception as e:
print(' pool_fail', e)
feat_urls = [p['url'] for p in (src.get('featured') or []) if p.get('url')]
if feat_urls:
# featured needs pool ids — re-list and match
try:
g2 = yt_assign_db.get_group(gid) or {}
pool = g2.get('pool') or []
ids = []
want = set(feat_urls)
for p in pool:
if p.get('url') in want:
ids.append(p['id'])
if ids:
print(' featured', yt_assign_db.add_featured(gid, ids))
except Exception as e:
print(' feat_fail', e)
# slots: skip structured copy (needs pool_url_ids); operator can re-fill
if src.get('slots'):
print(' slots_src', len(src['slots']), '(not auto-copied)')
except Exception as e:
print('CREATE_FAIL', name, e)
gs = yt_assign_db.list_groups(include_urls=False, include_secret=True)
print('TOTAL', len(gs))
for g in gs:
print('-', g.get('name'), 'sk', len(str(g.get('stream_key') or '')), 'neko', g.get('neko_instance_id'), 'en', g.get('neko_enabled'))
PY
""",
)
dl.close()
print("DONE")