# -*- coding: utf-8 -*- """Dedupe shorts_oauth_profiles ids on dy and rebind channels.""" from __future__ import annotations import sys import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") FIX = r""" import json, sqlite3, time conn=sqlite3.connect('/opt/douyinWEB/backend/data/monitor.db') conn.row_factory=sqlite3.Row raw=conn.execute("SELECT value FROM kv_settings WHERE key='shorts_oauth_profiles'").fetchone() profiles=json.loads(raw[0] if raw else '[]') # known mapping from prior deploys: # A = dy primary 690835... # B = dy secondary 315927... (was wrongly labeled A after merge) # C = ypp 806475... KNOWN = { '690835662011-tcdieulllihgqn1plsb6d2ha73d81hk6.apps.googleusercontent.com': ('A', '谷歌账号 A'), '315927588394-f82pghoggn9p7hp8qmklat8e979ni60k.apps.googleusercontent.com': ('B', '谷歌账号 B'), '806475240702-gom5ikarjjlkejnrrf29m73chtei9tm8.apps.googleusercontent.com': ('C', '谷歌账号 C (ypp)'), } by_cid = {} for p in profiles: cid = str((p or {}).get('client_id') or '').strip() if not cid: continue prev = by_cid.get(cid) or {} merged = {**prev, **{k:v for k,v in (p or {}).items() if v not in (None, '')}} if cid in KNOWN: merged['id'], merged['label'] = KNOWN[cid] by_cid[cid] = merged # assign leftover unique letters used = {str(p.get('id') or '').upper() for p in by_cid.values()} for cid, p in list(by_cid.items()): pid = str(p.get('id') or '').upper() if not pid or list(used).count(pid) > 1: # recompute uniqueness pass # rebuild unique final = [] used = set() for cid, p in by_cid.items(): if cid in KNOWN: p['id'], p['label'] = KNOWN[cid] pid = str(p.get('id') or '').upper() if pid in used: for cand in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': if cand not in used: pid = cand break p['id'] = pid p['label'] = p.get('label') or f'谷歌账号 {pid}' else: p['id'] = pid used.add(pid) final.append(p) final.sort(key=lambda x: str(x.get('id') or '')) conn.execute( "INSERT INTO kv_settings(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value", ('shorts_oauth_profiles', json.dumps(final, ensure_ascii=False)) ) # keep default client as A a = next((p for p in final if str(p.get('id')).upper()=='A'), final[0] if final else None) if a: conn.execute("INSERT INTO kv_settings(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value", ('shorts_oauth_client_id', str(a.get('client_id') or ''))) conn.execute("INSERT INTO kv_settings(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value", ('shorts_oauth_client_secret', str(a.get('client_secret') or ''))) conn.execute("INSERT INTO kv_settings(key,value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value", ('shorts_oauth_redirect_uri', 'https://dy.nomadro.live/api/shorts/oauth/callback')) cid_to_pid = {str(p.get('client_id') or ''): str(p.get('id') or '') for p in final} now=time.time() n=0 for table in ('shorts_channels','yt_long_channels'): for r in conn.execute(f'SELECT id,name,client_id,oauth_profile_id FROM {table}').fetchall(): want = cid_to_pid.get(str(r['client_id'] or '').strip(), '') if want and want != str(r['oauth_profile_id'] or ''): conn.execute(f'UPDATE {table} SET oauth_profile_id=?, updated_at=? WHERE id=?', (want, now, r['id'])) print('REBIND', table, r['name'], r['oauth_profile_id'], '->', want) n += 1 conn.commit() print('PROFILES', [{k:p.get(k) for k in ('id','label','client_id')} for p in final]) print('rebound', n) # counts print('rooms', conn.execute('SELECT count(*) FROM rooms').fetchone()[0]) print('shorts_oauth_ok', conn.execute('SELECT count(*) FROM shorts_channels WHERE oauth_ok=1').fetchone()[0], '/', conn.execute('SELECT count(*) FROM shorts_channels').fetchone()[0]) for r in conn.execute('SELECT name, youtube_channel_id, oauth_profile_id, oauth_ok, length(refresh_token) rt FROM shorts_channels ORDER BY oauth_profile_id, name').fetchall(): print('SC', dict(r)) conn.close() """ 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) sftp = c.open_sftp() with sftp.file("/tmp/_dedupe_profiles.py", "w") as f: f.write(FIX) sftp.close() _, o, e = c.exec_command("cd /opt/douyinWEB/backend && .venv/bin/python /tmp/_dedupe_profiles.py 2>&1", timeout=60) print(o.read().decode("utf-8", "replace")) err = e.read().decode("utf-8", "replace") if err.strip(): print(err) c.close()