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

123 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""Follow-up: yt_long OAuth probe + neko/status + redirect URI reminder."""
from __future__ import annotations
import json
import sys
import urllib.parse
import urllib.request
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
def ssh():
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"108.174.50.68",
22,
"root",
"Xiao4669805",
timeout=45,
allow_agent=False,
look_for_keys=False,
banner_timeout=120,
)
return c
c = ssh()
_, o, _ = c.exec_command(
r"""
set -euo pipefail
cd /opt/douyinWEB/backend
set -a; source /etc/douyinweb.env; set +a
./.venv/bin/python - <<'PY'
import json, urllib.parse, urllib.request, urllib.error
from app import db, yt_long_db, yt_assign_db
from app import youtube_neko as yn
def probe(label, cid, sec, rt):
rt = (rt or '').strip()
if not rt:
return {'label': label, 'ok': False, 'err': 'no_rt'}
data = urllib.parse.urlencode({
'client_id': (cid or '').strip(),
'client_secret': (sec or '').strip(),
'refresh_token': rt,
'grant_type': 'refresh_token',
}).encode()
req = urllib.request.Request('https://oauth2.googleapis.com/token', data=data, method='POST')
try:
with urllib.request.urlopen(req, timeout=25) as r:
body = json.loads(r.read().decode())
return {'label': label, 'ok': bool(body.get('access_token')), 'exp': body.get('expires_in')}
except Exception as e:
err = str(e)
if isinstance(e, urllib.error.HTTPError):
err = e.read().decode('utf-8','replace')[:180]
return {'label': label, 'ok': False, 'err': err}
# yt_long
chs = []
for name in ('list_channels', 'list_yt_long_channels', 'list_all'):
fn = getattr(yt_long_db, name, None)
if callable(fn):
try:
chs = fn() or []
print('long_via', name, len(chs))
break
except Exception as e:
print('long_fail', name, e)
ok = bad = 0
for ch in chs:
r = probe(f"long:{ch.get('name')}", ch.get('client_id'), ch.get('client_secret'), ch.get('refresh_token'))
print(json.dumps(r, ensure_ascii=False))
ok += int(r['ok']); bad += int(not r['ok'])
print('long_summary', 'ok', ok, 'bad', bad, 'total', len(chs))
# assign oauth fields presence
groups = yt_assign_db.list_groups(include_urls=False) or []
print('assign', len(groups))
for g in groups:
print(json.dumps({
'name': g.get('name'),
'neko': g.get('neko_instance_id'),
'yt': (g.get('youtube_channel_id') or '')[:24],
'has_rt': bool(str(g.get('refresh_token') or '').strip()),
'prefer': g.get('neko_open_prefer'),
'enabled': g.get('enabled'),
'neko_enabled': g.get('neko_enabled'),
}, ensure_ascii=False))
# oauth client ids for console reminder
sm = db.get_settings_map()
print('redirect', sm.get('shorts_oauth_redirect_uri'))
profiles = sm.get('shorts_oauth_profiles') or []
if isinstance(profiles, str):
profiles = json.loads(profiles)
for p in profiles:
if not isinstance(p, dict):
continue
cid = str(p.get('client_id') or '').strip()
if cid:
print('console_client', p.get('name') or p.get('id'), cid)
# neko status
try:
snap = yn.list_instances() if hasattr(yn, 'list_instances') else None
except Exception:
snap = None
import subprocess
print(subprocess.check_output("docker ps -a --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}'", shell=True).decode())
print('mem'); print(subprocess.check_output('free -h', shell=True).decode())
PY
""",
timeout=180,
)
print(o.read().decode("utf-8", "replace"))
c.close()