def is_instagram_story_url(url: str) -> bool: return "instagram.com/stories" in url VIDEO_SUFFIXES = ('.mp4', '.mov', '.mkv') IMAGE_SUFFIXES = ('.jpg', '.jpeg', '.png', '.webp') from typing import Optional def extract_instagram_username(url: Optional[str]) -> Optional[str]: # Поддержка: # https://www.instagram.com/stories/USERNAME/... # https://www.instagram.com/USERNAME/ try: base = url.split("?")[0].strip("/") parts = base.split("/") if "instagram.com" not in url: return None if "stories" in parts: i = parts.index("stories") if i + 1 < len(parts): return parts[i + 1] # профиль host_idx = 0 for i, p in enumerate(parts): if "instagram.com" in p: host_idx = i break if host_idx + 1 < len(parts): candidate = parts[host_idx + 1] if candidate and candidate not in ("stories", "reel", "p", "tv"): return candidate except Exception: return None return None