Files
autoreg-skills-max/skills/lobehub-pool/scripts/lobehub_merge_restart.py

114 lines
3.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Merge new LobeHub accounts into main pool and restart proxy.
Source: lobehub_new_accounts.json (from autoreg)
Target: lobehub_accounts.json (main pool, 841+ accounts)
Then: restart proxy on :20140
"""
import json
import shutil
import os
import sys
import subprocess
import time
from datetime import datetime
MAIN_POOL = r"C:\Users\User\lobehub_accounts.json"
NEW_ACCOUNTS = r"C:\Users\User\AppData\Local\hermes\lobehub_new_accounts.json"
BACKUP = r"C:\Users\User\AppData\Local\hermes\lobehub_accounts_backup_{}.json"
def main():
# 1. Load new accounts
if not os.path.exists(NEW_ACCOUNTS):
print(f"[ERROR] New accounts file not found: {NEW_ACCOUNTS}")
sys.exit(1)
with open(NEW_ACCOUNTS) as f:
new_accounts = json.load(f)
print(f"[INFO] New accounts: {len(new_accounts)}")
if len(new_accounts) == 0:
print("[ERROR] No new accounts to merge")
sys.exit(1)
# 2. Load main pool
with open(MAIN_POOL) as f:
main_pool = json.load(f)
print(f"[INFO] Main pool before: {len(main_pool)}")
# 3. Backup main pool
backup_path = BACKUP.format(datetime.now().strftime("%Y%m%d_%H%M%S"))
shutil.copy2(MAIN_POOL, backup_path)
print(f"[INFO] Backup: {backup_path}")
# 4. Merge (avoid duplicates by email)
existing_emails = {a["email"] for a in main_pool}
new_count = 0
for acc in new_accounts:
if acc["email"] not in existing_emails:
main_pool.append(acc)
existing_emails.add(acc["email"])
new_count += 1
print(f"[INFO] Added {new_count} new accounts (skipped {len(new_accounts) - new_count} duplicates)")
print(f"[INFO] Main pool after: {len(main_pool)}")
# 5. Save
with open(MAIN_POOL, "w") as f:
json.dump(main_pool, f, indent=2)
print(f"[OK] Saved to {MAIN_POOL}")
# 6. Kill old proxy on :20140
print("[INFO] Killing old proxy on :20140...")
try:
result = subprocess.run(
"netstat -ano | grep 20140 | grep LISTENING | awk '{print $5}'",
shell=True, capture_output=True, text=True
)
pids = result.stdout.strip().split("\n")
for pid in pids:
if pid:
subprocess.run(f"taskkill //F //PID {pid}", shell=True, capture_output=True)
print(f" Killed PID {pid}")
except Exception as e:
print(f" [WARN] Kill error: {e}")
time.sleep(2)
# 7. Verify port is free
result = subprocess.run(
"netstat -ano | grep 20140 | grep LISTENING",
shell=True, capture_output=True, text=True
)
if result.stdout.strip():
print("[WARN] Port 20140 still in use!")
else:
print("[OK] Port 20140 free")
# 8. Start new proxy
print("[INFO] Starting lobehub proxy v2 ...")
subprocess.Popen(
["bash", "--noprofile", "--norc", "-c",
"cd /c/Users/User/Desktop/tg_content_agent && exec python -u lobehub_proxy_v2.py"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
time.sleep(4)
# 9. Verify
try:
import urllib.request
r = urllib.request.urlopen("http://127.0.0.1:20140/health", timeout=5)
data = json.loads(r.read())
print(f"[OK] Proxy alive: {json.dumps(data)}")
except Exception as e:
print(f"[ERROR] Proxy health check failed: {e}")
print("[DONE]")
if __name__ == "__main__":
main()