"""Reconhece a plataforma e o canal a partir do link colado (Twitch, YouTube, Kick).""" from __future__ import annotations import re from dataclasses import dataclass from urllib.parse import parse_qs, urlparse PLATFORMS = ("twitch", "youtube", "kick") _TWITCH_LOGIN = re.compile(r"^[a-z0-9_]{3,25}$") _KICK_SLUG = re.compile(r"^[a-z0-9_-]{2,40}$") _YT_HANDLE = re.compile(r"^@[A-Za-z0-9._-]{2,40}$") _YT_VIDEO = re.compile(r"^[A-Za-z0-9_-]{11}$") _TWITCH_RESERVED = {"videos", "directory", "settings", "p", "search", "downloads", "jobs", "turbo"} class SourceError(ValueError): """Link que o Clipador não reconhece.""" @dataclass(frozen=True) class Source: platform: str channel: str # login (Twitch), slug (Kick) ou @handle/ID do canal (YouTube); "" quando só há o vídeo url: str # link normalizado que vai para o Streamlink kind: str # "live" (canal) ou "vod" (vídeo gravado) @property def channel_id(self) -> str: return f"{self.platform}-{self.channel.lstrip('@').lower()}" def parse_source(raw: str) -> Source: text = raw.strip() if not text: raise SourceError("Cole o link da live ou do vídeo.") if "://" not in text: text = "https://" + text url = urlparse(text) if url.scheme not in ("http", "https"): raise SourceError("O link precisa começar com https://.") host = (url.hostname or "").lower().removeprefix("www.").removeprefix("m.") parts = [p for p in url.path.split("/") if p] if host in ("twitch.tv", "go.twitch.tv"): if len(parts) >= 2 and parts[0] == "videos" and parts[1].isdigit(): return Source("twitch", "", f"https://www.twitch.tv/videos/{parts[1]}", "vod") if len(parts) >= 3 and parts[1] == "video" and parts[2].isdigit(): return Source("twitch", parts[0].lower(), f"https://www.twitch.tv/videos/{parts[2]}", "vod") if parts and _TWITCH_LOGIN.fullmatch(parts[0].lower()) and parts[0].lower() not in _TWITCH_RESERVED: login = parts[0].lower() return Source("twitch", login, f"https://www.twitch.tv/{login}", "live") raise SourceError("Link da Twitch não reconhecido (use twitch.tv/ ou twitch.tv/videos/).") if host == "kick.com": if len(parts) >= 3 and parts[1] == "videos": return Source("kick", parts[0].lower(), f"https://kick.com/{parts[0].lower()}/videos/{parts[2]}", "vod") if len(parts) == 1 and _KICK_SLUG.fullmatch(parts[0].lower()): slug = parts[0].lower() return Source("kick", slug, f"https://kick.com/{slug}", "live") raise SourceError("Link da Kick não reconhecido (use kick.com/).") if host in ("youtube.com", "youtu.be"): if host == "youtu.be" and parts and _YT_VIDEO.fullmatch(parts[0]): return Source("youtube", "", f"https://www.youtube.com/watch?v={parts[0]}", "vod") video = parse_qs(url.query).get("v", [""])[0] if parts[:1] == ["watch"] and _YT_VIDEO.fullmatch(video): return Source("youtube", "", f"https://www.youtube.com/watch?v={video}", "vod") if len(parts) >= 2 and parts[0] in ("live", "shorts") and _YT_VIDEO.fullmatch(parts[1]): return Source("youtube", "", f"https://www.youtube.com/watch?v={parts[1]}", "vod") if parts and _YT_HANDLE.fullmatch(parts[0]): return Source("youtube", parts[0], f"https://www.youtube.com/{parts[0]}/live", "live") if len(parts) >= 2 and parts[0] == "channel" and re.fullmatch(r"UC[A-Za-z0-9_-]{22}", parts[1]): return Source("youtube", parts[1], f"https://www.youtube.com/channel/{parts[1]}/live", "live") raise SourceError("Link do YouTube não reconhecido (use youtube.com/@canal ou o link do vídeo).") raise SourceError("Plataforma não suportada: use Twitch, YouTube ou Kick.")