|
| 1 | +import json |
| 2 | +import re |
| 3 | + |
| 4 | +from user_scanner.core.helpers import get_random_user_agent |
| 5 | +from user_scanner.core.orchestrator import generic_validate |
| 6 | +from user_scanner.core.result import Result |
| 7 | + |
| 8 | + |
| 9 | +NEXT_DATA_PATTERN = re.compile( |
| 10 | + r'<script id="__NEXT_DATA__" type="application/json">(.*?)</script>', |
| 11 | + re.DOTALL, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def _extract_next_data(html: str) -> dict | None: |
| 16 | + match = NEXT_DATA_PATTERN.search(html) |
| 17 | + if not match: |
| 18 | + return None |
| 19 | + |
| 20 | + try: |
| 21 | + data = json.loads(match.group(1)) |
| 22 | + except json.JSONDecodeError: |
| 23 | + return None |
| 24 | + |
| 25 | + return data if isinstance(data, dict) else None |
| 26 | + |
| 27 | + |
| 28 | +def validate_daily_dev(user): |
| 29 | + url = f"https://app.daily.dev/{user}" |
| 30 | + show_url = f"https://app.daily.dev/{user}" |
| 31 | + |
| 32 | + headers = { |
| 33 | + "User-Agent": get_random_user_agent(), |
| 34 | + } |
| 35 | + |
| 36 | + def process(response): |
| 37 | + next_data = _extract_next_data(response.text) |
| 38 | + if next_data is None: |
| 39 | + return Result.error( |
| 40 | + "Could not read __NEXT_DATA__ payload, report it via GitHub issues." |
| 41 | + ) |
| 42 | + |
| 43 | + page_props = next_data.get("props", {}).get("pageProps", {}) |
| 44 | + user_data = page_props.get("user") |
| 45 | + |
| 46 | + if isinstance(user_data, dict): |
| 47 | + if user_data.get("id") and user_data.get("name"): |
| 48 | + return Result.taken() |
| 49 | + |
| 50 | + if page_props.get("noindex") is True: |
| 51 | + return Result.available() |
| 52 | + |
| 53 | + return Result.error( |
| 54 | + "Unexpected daily.dev payload shape, report it via GitHub issues." |
| 55 | + ) |
| 56 | + |
| 57 | + return generic_validate(url, process, show_url=show_url, headers=headers) |
0 commit comments