90 lines
3.0 KiB
Python
90 lines
3.0 KiB
Python
|
|
import logging
|
||
|
|
import os
|
||
|
|
import re
|
||
|
|
|
||
|
|
import aiohttp
|
||
|
|
|
||
|
|
from yuxi.channel.extensions.twitch.config import TwitchAccountConfig
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
def resolve_twitch_token(account: TwitchAccountConfig, account_id: str = "default") -> tuple[str, str]:
|
||
|
|
token = account.access_token
|
||
|
|
if token:
|
||
|
|
return normalize_twitch_token(token), "config"
|
||
|
|
|
||
|
|
if account_id == "default":
|
||
|
|
env_token = os.getenv("OPENCLAW_TWITCH_ACCESS_TOKEN") or os.getenv("TWITCH_ACCESS_TOKEN")
|
||
|
|
if env_token:
|
||
|
|
return normalize_twitch_token(env_token), "env"
|
||
|
|
|
||
|
|
return "", "none"
|
||
|
|
|
||
|
|
|
||
|
|
def normalize_twitch_token(token: str) -> str:
|
||
|
|
token = token.strip()
|
||
|
|
if not token.lower().startswith("oauth:"):
|
||
|
|
token = f"oauth:{token}"
|
||
|
|
return token
|
||
|
|
|
||
|
|
|
||
|
|
def denormalize_token(token: str) -> str:
|
||
|
|
if token.lower().startswith("oauth:"):
|
||
|
|
return token[6:]
|
||
|
|
return token
|
||
|
|
|
||
|
|
|
||
|
|
def is_oauth_token(token: str) -> bool:
|
||
|
|
return bool(token) and token.lower().startswith("oauth:")
|
||
|
|
|
||
|
|
|
||
|
|
def extract_access_token(account: TwitchAccountConfig) -> str:
|
||
|
|
token = account.access_token
|
||
|
|
return denormalize_token(token)
|
||
|
|
|
||
|
|
|
||
|
|
def detect_mentions(text: str, bot_username: str) -> bool:
|
||
|
|
mentions = re.findall(r"@(\w+)", text)
|
||
|
|
bot_lower = bot_username.lower()
|
||
|
|
return any(m.lower() == bot_lower for m in mentions)
|
||
|
|
|
||
|
|
|
||
|
|
async def validate_twitch_token(account: TwitchAccountConfig) -> dict:
|
||
|
|
token, _ = resolve_twitch_token(account)
|
||
|
|
bare = denormalize_token(token)
|
||
|
|
if not bare:
|
||
|
|
return {"valid": False, "reason": "no token configured"}
|
||
|
|
url = "https://id.twitch.tv/oauth2/validate"
|
||
|
|
headers = {"Authorization": f"OAuth {bare}"}
|
||
|
|
try:
|
||
|
|
async with aiohttp.ClientSession() as session:
|
||
|
|
async with session.get(url, headers=headers, timeout=aiohttp.ClientTimeout(total=10)) as resp:
|
||
|
|
if resp.status == 200:
|
||
|
|
data = await resp.json()
|
||
|
|
return {"valid": True, **data}
|
||
|
|
return {"valid": False, "status": resp.status, "message": await resp.text()}
|
||
|
|
except Exception as e:
|
||
|
|
return {"valid": False, "error": str(e)}
|
||
|
|
|
||
|
|
|
||
|
|
async def refresh_twitch_token(account: TwitchAccountConfig) -> dict:
|
||
|
|
if not account.refresh_token or not account.client_id or not account.client_secret:
|
||
|
|
return {"ok": False, "error": "missing refresh_token, client_id, or client_secret"}
|
||
|
|
url = "https://id.twitch.tv/oauth2/token"
|
||
|
|
params = {
|
||
|
|
"grant_type": "refresh_token",
|
||
|
|
"refresh_token": account.refresh_token,
|
||
|
|
"client_id": account.client_id,
|
||
|
|
"client_secret": account.client_secret,
|
||
|
|
}
|
||
|
|
try:
|
||
|
|
async with aiohttp.ClientSession() as session:
|
||
|
|
async with session.post(url, json=params, timeout=aiohttp.ClientTimeout(total=10)) as resp:
|
||
|
|
data = await resp.json()
|
||
|
|
if resp.status == 200:
|
||
|
|
return {"ok": True, **data}
|
||
|
|
return {"ok": False, "error": data.get("message", f"HTTP {resp.status}")}
|
||
|
|
except Exception as e:
|
||
|
|
return {"ok": False, "error": str(e)}
|