|
| 1 | +import httpx |
| 2 | +from user_scanner.core.result import Result |
| 3 | + |
| 4 | + |
| 5 | +async def _check(email: str) -> Result: |
| 6 | + headers = { |
| 7 | + 'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36", |
| 8 | + 'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8", |
| 9 | + 'Accept-Language': "en-US,en;q=0.9", |
| 10 | + 'Referer': "https://leetcode.com/accounts/login/", |
| 11 | + 'Origin': "https://leetcode.com", |
| 12 | + } |
| 13 | + |
| 14 | + try: |
| 15 | + async with httpx.AsyncClient(timeout=5.0, follow_redirects=True) as client: |
| 16 | + await client.get("https://leetcode.com/accounts/login/", headers=headers) |
| 17 | + csrf_token = client.cookies.get("csrftoken") |
| 18 | + |
| 19 | + if not csrf_token: |
| 20 | + return Result.error("CSRF token not found, possible rate-limit") |
| 21 | + |
| 22 | + headers.update({ |
| 23 | + 'x-requested-with': "XMLHttpRequest", |
| 24 | + 'referer': "https://leetcode.com/accounts/password/reset/", |
| 25 | + }) |
| 26 | + |
| 27 | + payload = { |
| 28 | + 'next': 'undefined', |
| 29 | + 'userName': '', |
| 30 | + 'email': email, |
| 31 | + 'csrfmiddlewaretoken': csrf_token |
| 32 | + } |
| 33 | + |
| 34 | + response = await client.post( |
| 35 | + "https://leetcode.com/accounts/password/reset/", |
| 36 | + headers=headers, |
| 37 | + data=payload |
| 38 | + ) |
| 39 | + |
| 40 | + if response.status_code in [200, 400]: |
| 41 | + data = response.json() |
| 42 | + |
| 43 | + if data.get("location") == "/accounts/password/reset/done/": |
| 44 | + return Result.taken() |
| 45 | + |
| 46 | + email_field = data.get("form", {}).get( |
| 47 | + "fields", {}).get("email", {}) |
| 48 | + errors = email_field.get("errors", []) |
| 49 | + |
| 50 | + if any("not assigned to any user account" in err for err in errors): |
| 51 | + return Result.available() |
| 52 | + |
| 53 | + return Result.error("Unexpected response data") |
| 54 | + |
| 55 | + return Result.error(f"HTTP {response.status_code}") |
| 56 | + |
| 57 | + except httpx.TimeoutException: |
| 58 | + return Result.error("Connection timed out") |
| 59 | + except Exception as e: |
| 60 | + return Result.error(e) |
| 61 | + |
| 62 | + |
| 63 | +async def validate_leetcode(email: str) -> Result: |
| 64 | + return await _check(email) |
0 commit comments