A Kick passou a escolher o provedor de tempo real por canal (POST web.kick.com/api/v1/realtime/channels/<id>/chat/
connection) e hoje responde Centrifugo; o Pusher com a chave antiga recusava com "App key not in this cluster" e o
chat de toda fonte da Kick ficava em erro. Agora: token de convidado (/v1/realtime/auth/connection, sem login) →
connect → subscribe chatrooms.<sala>.v2; ping {} respondido; token expira e o laço reconecta. Se a Kick indicar
Pusher, usa a chave e o cluster que ela informar. Testado ao vivo no chat de kick.com/peusantista.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
248 lines
13 KiB
Python
248 lines
13 KiB
Python
"""Leitura do chat ao vivo, em tempo real, para medir a reação do público.
|
|
|
|
- Twitch: IRC anônimo (irc.chat.twitch.tv:6697, apelido `justinfanNNNN`): só leitura, sem conta nem token.
|
|
- Kick: o mesmo tempo real que o site usa (não é API oficial: pode mudar sem aviso). Desde 11/2026 a Kick
|
|
escolhe o provedor por canal (`POST web.kick.com/api/v1/realtime/channels/<id>/chat/connection`): hoje responde
|
|
**Centrifugo** (token de convidado em `/v1/realtime/auth/connection`, sem login) e o Pusher com a chave fixa antiga
|
|
passou a recusar ("App key not in this cluster"). O canal continua `chatrooms.<sala>.v2` e a mensagem tem o mesmo
|
|
formato. O Cloudflare às vezes bloqueia a consulta do id da sala.
|
|
- YouTube: ler o chat exige a API do YouTube (cota) — ainda não suportado; a sessão segue pelo áudio.
|
|
|
|
Cada leitor roda numa thread, reconecta sozinho e entrega lotes de (instante, usuário, texto) com o relógio
|
|
de parede; quem chama converte para o tempo da gravação.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import random
|
|
import re
|
|
import socket
|
|
import ssl
|
|
import threading
|
|
import time
|
|
import uuid
|
|
from typing import Callable
|
|
|
|
import httpx
|
|
|
|
from .emotes import parse_twitch_tag
|
|
|
|
log = logging.getLogger("cenaforge_clipper.chat")
|
|
# (hora real, usuário, texto, emotes nativos recortados do texto)
|
|
OnMessages = Callable[[list[tuple[float, str, str, list[str]]]], None]
|
|
|
|
_PRIVMSG = re.compile(r"^(?:@(\S+) )?:([^!\s]+)!\S+ PRIVMSG #\S+ :(.*)$")
|
|
_ROOM_ID = re.compile(r"(?:^@|;)room-id=(\d+)")
|
|
KICK_API = "https://web.kick.com/api/v1/realtime"
|
|
KICK_HEADERS = {"User-Agent": "Mozilla/5.0", "Accept": "application/json", "Origin": "https://kick.com"}
|
|
# Pedido do site ao escolher o provedor (os dois que ele aceita). Medido 18/11/2026: a resposta é Centrifugo.
|
|
KICK_CAPABILITIES = {"accepted_providers": [{"provider": "pusher"}, {"provider": "centrifugo"}]}
|
|
KICK_CENTRIFUGO_DEFAULT = "wss://realtime.us-west-2.platform.kick.com/connection/websocket"
|
|
_KICK_EMOTE = re.compile(r"\[emote:\d+:([^\]]+)\]")
|
|
|
|
|
|
def kick_message(event: str, payload: object) -> tuple[str, str, list[str]] | None:
|
|
"""Mensagem de chat da Kick → (usuário, texto, emotes), ou None se o evento não é mensagem. O `data` vem como
|
|
texto JSON (Pusher e Centrifugo). Emotes chegam como [emote:123:NOME]: fica só o nome, que carrega a reação."""
|
|
if not str(event).endswith("ChatMessageEvent"):
|
|
return None
|
|
data = json.loads(payload) if isinstance(payload, str) else (payload or {})
|
|
if not isinstance(data, dict):
|
|
return None
|
|
content = str(data.get("content") or "")
|
|
user = str((data.get("sender") or {}).get("username") or "")
|
|
return user, _KICK_EMOTE.sub(r"\1", content)[:300], _KICK_EMOTE.findall(content)
|
|
|
|
|
|
class ChatReader:
|
|
def __init__(self, platform: str, channel: str, on_messages: OnMessages):
|
|
self.platform, self.channel, self.on_messages = platform, channel, on_messages
|
|
self.status = "off"
|
|
self.received = 0
|
|
self.room_id: str | None = None # id numérico do canal na Twitch (para emotes 7TV/BTTV/FFZ)
|
|
self._stop = threading.Event()
|
|
self._thread: threading.Thread | None = None
|
|
|
|
def start(self) -> None:
|
|
if self.platform == "youtube" or not self.channel:
|
|
self.status = "unsupported"
|
|
return
|
|
target = {"twitch": self._run_twitch, "kick": self._run_kick}[self.platform]
|
|
self._thread = threading.Thread(target=self._loop, args=(target,), name=f"chat-{self.channel}", daemon=True)
|
|
self._thread.start()
|
|
|
|
def stop(self) -> None:
|
|
self._stop.set()
|
|
|
|
def _loop(self, target: Callable[[], None]) -> None:
|
|
delay = 2.0
|
|
while not self._stop.is_set():
|
|
try:
|
|
self.status = "connecting"
|
|
target()
|
|
delay = 2.0
|
|
except Exception as exc: # noqa: BLE001 - rede caiu: reconecta com espera crescente
|
|
self.status = f"error: {type(exc).__name__}: {str(exc)[:120]}"
|
|
log.warning("chat %s/%s: %s", self.platform, self.channel, self.status)
|
|
self._stop.wait(delay)
|
|
delay = min(60.0, delay * 2)
|
|
self.status = "off"
|
|
|
|
def _deliver(self, batch: list[tuple[float, str, str]]) -> None:
|
|
if batch:
|
|
self.received += len(batch)
|
|
self.on_messages(batch)
|
|
|
|
# ---------- Twitch (IRC) ----------
|
|
def _run_twitch(self) -> None:
|
|
context = ssl.create_default_context()
|
|
with socket.create_connection(("irc.chat.twitch.tv", 6697), timeout=30) as raw:
|
|
with context.wrap_socket(raw, server_hostname="irc.chat.twitch.tv") as sock:
|
|
sock.settimeout(1.0)
|
|
nick = f"justinfan{random.randint(10000, 99999)}"
|
|
# Tags trazem os emotes nativos (com posição no texto) e o room-id do canal.
|
|
sock.sendall(f"CAP REQ :twitch.tv/tags\r\nNICK {nick}\r\nJOIN #{self.channel}\r\n".encode())
|
|
buffer, batch, last_flush, last_data = b"", [], time.monotonic(), time.monotonic()
|
|
while not self._stop.is_set():
|
|
try:
|
|
chunk = sock.recv(65536)
|
|
if not chunk:
|
|
raise ConnectionError("conexão fechada pela Twitch")
|
|
buffer += chunk
|
|
last_data = time.monotonic()
|
|
except TimeoutError:
|
|
pass
|
|
except socket.timeout:
|
|
pass
|
|
*lines, buffer = buffer.split(b"\r\n")
|
|
for line in lines:
|
|
text = line.decode("utf-8", errors="replace")
|
|
if text.startswith("PING"):
|
|
sock.sendall(("PONG" + text[4:] + "\r\n").encode())
|
|
elif " 366 " in text or " JOIN #" in text:
|
|
self.status = "connected"
|
|
elif match := _PRIVMSG.match(text):
|
|
tags, body = _parse_tags(match.group(1) or ""), match.group(3)
|
|
self.room_id = self.room_id or tags.get("room-id")
|
|
batch.append((time.time(), match.group(2), body[:300],
|
|
parse_twitch_tag(tags.get("emotes", ""), body)))
|
|
elif room := _ROOM_ID.search(text):
|
|
self.room_id = self.room_id or room.group(1)
|
|
if time.monotonic() - last_flush >= 1.0:
|
|
self._deliver(batch)
|
|
batch, last_flush = [], time.monotonic()
|
|
if time.monotonic() - last_data > 360: # a Twitch manda PING a cada ~5 min
|
|
raise ConnectionError("sem dados há 6 min")
|
|
|
|
# ---------- Kick (tempo real do site, não oficial) ----------
|
|
def _run_kick(self) -> None:
|
|
response = httpx.get(f"https://kick.com/api/v2/channels/{self.channel}", headers=KICK_HEADERS, timeout=20,
|
|
follow_redirects=True)
|
|
if response.status_code != 200:
|
|
raise ConnectionError(f"kick.com respondeu {response.status_code} ao buscar a sala do chat")
|
|
channel = response.json()
|
|
room, channel_id = channel["chatroom"]["id"], channel["id"]
|
|
client_id = str(uuid.uuid4())
|
|
provider = self._kick_provider(channel_id, client_id)
|
|
if provider.get("provider") == "pusher":
|
|
self._run_kick_pusher(room, provider)
|
|
else:
|
|
self._run_kick_centrifugo(room, client_id, (provider.get("credentials") or {}).get("url") or KICK_CENTRIFUGO_DEFAULT)
|
|
|
|
def _kick_provider(self, channel_id: int, client_id: str) -> dict:
|
|
"""Provedor de tempo real que a Kick indica para o chat deste canal ({provider, credentials}). Falha na consulta
|
|
→ Centrifugo no endereço padrão (o que a Kick respondeu em todos os canais testados)."""
|
|
try:
|
|
r = httpx.post(f"{KICK_API}/channels/{channel_id}/chat/connection", headers=KICK_HEADERS, timeout=20,
|
|
json={"client": {"id": client_id, "type": "web"}, "capabilities": KICK_CAPABILITIES})
|
|
r.raise_for_status()
|
|
connections = (r.json().get("data") or {}).get("connections") or []
|
|
return connections[0] if connections else {"provider": "centrifugo"}
|
|
except (httpx.HTTPError, ValueError, KeyError, IndexError) as exc:
|
|
log.info("chat kick/%s: provedor não informado (%s); usando Centrifugo", self.channel, exc)
|
|
return {"provider": "centrifugo"}
|
|
|
|
def _run_kick_centrifugo(self, room: int, client_id: str, url: str) -> None:
|
|
"""Centrifugo (protocolo JSON): token de convidado → connect → subscribe `chatrooms.<sala>.v2`. O servidor manda
|
|
`{}` como ping (responde `{}`); o token expira (~45 min) e o servidor fecha: o laço reconecta com token novo."""
|
|
from websockets.sync.client import connect # import tardio: só quem usa Kick precisa
|
|
|
|
r = httpx.post(f"{KICK_API}/auth/connection", headers=KICK_HEADERS, json={"client_id": client_id}, timeout=20)
|
|
r.raise_for_status()
|
|
token = (r.json().get("data") or {}).get("token")
|
|
if not token:
|
|
raise ConnectionError("Kick não devolveu o token do chat")
|
|
with connect(url, open_timeout=20, additional_headers={"Origin": "https://kick.com", "User-Agent": "Mozilla/5.0"}) as ws:
|
|
ws.send(json.dumps({"connect": {"token": token, "name": "js"}, "id": 1}))
|
|
ws.send(json.dumps({"subscribe": {"channel": f"chatrooms.{room}.v2"}, "id": 2}))
|
|
batch, last_flush = [], time.monotonic()
|
|
while not self._stop.is_set():
|
|
try:
|
|
raw = ws.recv(timeout=1.0)
|
|
except TimeoutError:
|
|
raw = None
|
|
for part in (raw or "").split("\n"):
|
|
part = part.strip()
|
|
if not part:
|
|
continue
|
|
if part == "{}":
|
|
ws.send("{}") # ping do Centrifugo
|
|
continue
|
|
msg = json.loads(part)
|
|
if msg.get("error"):
|
|
raise ConnectionError(f"Kick (Centrifugo): {msg['error']}")
|
|
if msg.get("id") == 2 and "subscribe" in msg:
|
|
self.status = "connected"
|
|
pub = (msg.get("push") or {}).get("pub") or {}
|
|
data = pub.get("data") or {}
|
|
item = kick_message(data.get("event", ""), data.get("data")) if isinstance(data, dict) else None
|
|
if item:
|
|
batch.append((time.time(), *item))
|
|
if time.monotonic() - last_flush >= 1.0:
|
|
self._deliver(batch)
|
|
batch, last_flush = [], time.monotonic()
|
|
|
|
def _run_kick_pusher(self, room: int, provider: dict) -> None:
|
|
"""Pusher com a chave e o cluster que a Kick informar (a chave fixa antiga parou de valer em 11/2026)."""
|
|
from websockets.sync.client import connect
|
|
|
|
creds = provider.get("credentials") or {}
|
|
key, cluster = creds.get("appKey") or creds.get("key"), creds.get("cluster") or "us2"
|
|
if not key:
|
|
raise ConnectionError("Kick indicou Pusher sem a chave")
|
|
url = f"wss://ws-{cluster}.pusher.com/app/{key}?protocol=7&client=js&version=8.4.0&flash=false"
|
|
with connect(url, open_timeout=20) as ws:
|
|
ws.send(json.dumps({"event": "pusher:subscribe", "data": {"auth": "", "channel": f"chatrooms.{room}.v2"}}))
|
|
batch, last_flush = [], time.monotonic()
|
|
while not self._stop.is_set():
|
|
try:
|
|
raw = ws.recv(timeout=1.0)
|
|
except TimeoutError:
|
|
raw = None
|
|
if raw:
|
|
event = json.loads(raw)
|
|
name = event.get("event", "")
|
|
if name == "pusher_internal:subscription_succeeded":
|
|
self.status = "connected"
|
|
elif name == "pusher:ping":
|
|
ws.send(json.dumps({"event": "pusher:pong", "data": {}}))
|
|
else:
|
|
item = kick_message(name, event.get("data") or "{}")
|
|
if item:
|
|
batch.append((time.time(), *item))
|
|
if time.monotonic() - last_flush >= 1.0:
|
|
self._deliver(batch)
|
|
batch, last_flush = [], time.monotonic()
|
|
|
|
|
|
def _parse_tags(raw: str) -> dict[str, str]:
|
|
"""`color=#F00;emotes=25:0-4;room-id=123` → dict. Só usamos ids e posições (sem desescapar valores)."""
|
|
tags: dict[str, str] = {}
|
|
for item in raw.split(";"):
|
|
if "=" in item:
|
|
key, value = item.split("=", 1)
|
|
tags[key] = value
|
|
return tags
|