#!/usr/bin/env python3 """Diagnose why assign 'new group' picker is empty on dy + dylive.""" from __future__ import annotations import json import ssl import sys import time import urllib.request from http.cookiejar import CookieJar from collections import Counter sys.stdout.reconfigure(encoding="utf-8", errors="replace") CTX = ssl.create_default_context() APP = ("hackrobot", "Xiao4669805@") SITES = [ ("dy", "https://dy.nomadro.live"), ("dylive", "https://dylive.nomadro.com"), ] def op_for(base: str): cj = CookieJar() op = urllib.request.build_opener( urllib.request.HTTPCookieProcessor(cj), urllib.request.HTTPSHandler(context=CTX), ) req = urllib.request.Request( f"{base}/api/auth/login", data=json.dumps({"username": APP[0], "password": APP[1]}).encode(), headers={"Content-Type": "application/json"}, method="POST", ) op.open(req, timeout=45).read() return op def get(op, url: str): with op.open(url, timeout=60) as r: return json.loads(r.read().decode()) for name, base in SITES: print(f"\n======== {name} {base} ========", flush=True) try: op = op_for(base) except Exception as e: print("login fail", e) continue shorts = get(op, f"{base}/api/shorts/channels") chans = shorts.get("channels") or shorts if isinstance(shorts, dict) else shorts if isinstance(shorts, dict) and "channels" not in shorts: # maybe list at root for k, v in shorts.items(): if isinstance(v, list) and v and isinstance(v[0], dict) and "id" in v[0]: chans = v break if not isinstance(chans, list): print("shorts unexpected", type(shorts), list(shorts)[:10] if isinstance(shorts, dict) else shorts) chans = [] auth = [c for c in chans if c.get("authorized") or c.get("has_refresh_token") or str(c.get("refresh_token") or "").strip()] # authorized field from public API auth2 = [c for c in chans if c.get("authorized")] if auth2: auth = auth2 print(f"shorts total={len(chans)} authorized={len(auth)}") ag = get(op, f"{base}/api/youtube/assign/groups") groups = ag.get("groups") or [] print(f"assign groups={len(groups)}") used_pk = set() used_yt = set() for g in groups: pk = str(g.get("channel_pk") or "").strip() yt = str(g.get("youtube_channel_id") or "").strip() if pk: used_pk.add(pk) if yt: used_yt.add(yt) avail = [] blocked = [] for c in auth: cid = str(c.get("id") or "") yt = str(c.get("youtube_channel_id") or "").trim() if False else str(c.get("youtube_channel_id") or "").strip() why = [] if cid in used_pk: why.append("pk") if yt and yt in used_yt: why.append("yt") if why: blocked.append((c.get("name"), cid[:8], yt[:20], "+".join(why))) else: avail.append(c.get("name")) print(f"available_for_new={len(avail)} -> {avail[:10]}") print(f"blocked={len(blocked)}") # duplicates by yt / name / pk yt_c = Counter(str(g.get("youtube_channel_id") or "") for g in groups if g.get("youtube_channel_id")) name_c = Counter(str(g.get("name") or "") for g in groups) pk_c = Counter(str(g.get("channel_pk") or "") for g in groups if g.get("channel_pk")) print("dup yt:", {k: v for k, v in yt_c.items() if v > 1}) print("dup name:", {k: v for k, v in name_c.items() if v > 1}) print("dup pk:", {k: v for k, v in pk_c.items() if v > 1}) print("--- groups ---") for g in groups: print( f" {(g.get('name') or '')[:28]:28} pk={(g.get('channel_pk') or '-')[:8]} yt={(g.get('youtube_channel_id') or '-')[:22]} sk={len(str(g.get('stream_key') or ''))} neko={g.get('neko_instance_id')} en={g.get('neko_enabled')}" ) print("--- authorized shorts not matching any group name ---") gnames = {str(g.get("name") or "").lower() for g in groups} for c in auth: nm = str(c.get("name") or "") if nm.lower() not in gnames: print(" orphan shorts?", nm, c.get("youtube_channel_id"), "blocked" if any(b[0]==nm for b in blocked) else "AVAIL") print("\nDONE")