#!/usr/bin/env python3 """UI-логин chat.co в headful Chrome (CDP) + скрин раздела моделей.""" import imaplib, email, json, os, re, subprocess, sys, time from playwright.sync_api import sync_playwright sys.stdout.reconfigure(encoding="utf-8", errors="replace") PORT = os.environ.get("CDP_PORT", "9244") EMAIL = os.environ.get("CC_EMAIL", "252d4f@rambler.ru") SHOT = os.environ.get("CC_SHOT", r"C:\Users\User\tmp\chatco\card\site_models.png") def otp(mail, pw, since_ts, timeout=240): """Читает код из последнего письма chat.co, пришедшего после since_ts.""" deadline = time.time() + timeout while time.time() < deadline: try: M = imaplib.IMAP4_SSL("imap.rambler.ru", 993, timeout=30) M.login(mail, pw) M.select("INBOX", readonly=True) typ, d = M.search(None, "FROM", '"chat.co"') ids = [int(x) for x in (d[0].split() if d and d[0] else [])] for i in reversed(ids[-8:]): _, dd = M.fetch(str(i), "(RFC822)") m = email.message_from_bytes(dd[0][1]) try: ts = time.mktime(email.utils.parsedate(m["Date"])) except Exception: ts = 0 if ts < since_ts - 120: continue body = "" for p in m.walk(): if p.get_content_type() in ("text/html", "text/plain"): body += p.get_payload(decode=True).decode("utf-8", "ignore") txt = re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", re.sub(r"", " ", body))) mm = re.search(r"verification code\s*(\d{6})", txt, re.I) or re.search(r"\b(\d{6})\b", txt) if mm: M.logout() return mm.group(1) M.logout() except Exception as e: print("imap", type(e).__name__, str(e)[:60], flush=True) time.sleep(8) return None def main(): pw = None for line in open(r"C:\Users\User\tmp\chatco\pool.jsonl", encoding="utf-8"): try: r = json.loads(line) except Exception: continue if r.get("email") == EMAIL and r.get("pw"): pw = r["pw"] break print("pw?", bool(pw), flush=True) with sync_playwright() as p: b = p.chromium.connect_over_cdp("http://127.0.0.1:%s" % PORT) ctx = b.contexts[0] pg = [x for x in ctx.pages if "chat.co" in (x.url or "")] or [ctx.new_page()] pg = pg[0] pg.goto("https://chat.co/auth/login", wait_until="domcontentloaded", timeout=90000) time.sleep(4) print("title:", pg.title(), flush=True) # cookie banner for sel in ["text=Accept", "button:has-text('Accept')"]: try: pg.click(sel, timeout=2500) break except Exception: pass pg.fill("input[name=email]", EMAIL) t0 = time.time() tok = "" for _ in range(45): tok = pg.eval_on_selector("input[name=cf-turnstile-response]", "e => e.value") if tok and len(tok) > 20: print("turnstile solved in %.0fs" % (time.time() - t0), flush=True) break time.sleep(2) if not tok: print("TURNSTILE FAIL", flush=True) return 2 pg.click("button:has-text('Proceed')") time.sleep(5) print("after proceed:", pg.url, flush=True) code = otp(EMAIL, pw, t0) print("otp:", code, flush=True) if not code: return 3 # поле кода: ищем любой видимый input, кроме email inputs = pg.eval_on_selector_all("input", "els => els.map(e => ({name:e.name, type:e.type, ph:e.placeholder}))") print("inputs:", inputs, flush=True) filled = False for sel in ["input[name=otp]", "input[autocomplete='one-time-code']", "input[inputmode='numeric']", "input[type=text]"]: try: pg.fill(sel, code, timeout=3000) filled = True print("filled via", sel, flush=True) break except Exception: continue if not filled: print("no otp field", flush=True) pg.screenshot(path=SHOT.replace(".png", "_otpstep.png")) return 4 for sel in ["button:has-text('Verify')", "button:has-text('Continue')", "button:has-text('Proceed')", "button[type=submit]"]: try: pg.click(sel, timeout=2500) break except Exception: continue time.sleep(10) print("url after otp:", pg.url, flush=True) pg.screenshot(path=SHOT.replace(".png", "_dash.png"), full_page=False) print("nav:", pg.eval_on_selector_all("a", "els => els.map(e => e.getAttribute('href')).filter(Boolean).slice(0,40)"), flush=True) return 0 if __name__ == "__main__": sys.exit(main())