Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project : KA Apify Actors9# File : ka-twitch/src/main.py10# Module : ka-twitch11# Repository : https://git.spboucher.ai/ka-apify-actors12# Created : 2026-08-1813# Last Modified : 2026-08-1814#15# =============================================================================16# AUTHOR & PROFESSIONAL AFFILIATION17# =============================================================================18#19# Author : Simon-Pierre Boucher20# Position : Professor of Finance21# Institution : Université du Québec en Outaouais (UQO)22# Department : Département des sciences administratives23# Organization : SP Boucher24#25# Website : https://spboucher.ai26# Git : https://git.spboucher.ai27#28# =============================================================================29# OFFICIAL CONTACT DIRECTORY30# =============================================================================31#32# General : info@spboucher.ai33# Professional : contact@spboucher.ai34# Direct : simon@spboucher.ai35# Research : research@spboucher.ai36# Development : dev@spboucher.ai37# Support : support@spboucher.ai38# Security/Admin : admin@spboucher.ai39# Notifications : notifications@spboucher.ai40#41# Preferred Contact: contact@spboucher.ai42#43# =============================================================================44# COPYRIGHT & INTELLECTUAL PROPERTY45# =============================================================================46#47# Copyright (c) 202648# Simon-Pierre Boucher49#50# All rights reserved where applicable.51#52# This file is governed by the applicable project license,53# copyright notices, institutional agreements and relevant law.54#55# Unless expressly authorized by the applicable license or56# rights holder, copying, redistribution, sublicensing,57# publication and commercial exploitation are not permitted.58#59# Existing third-party rights and notices remain unaffected.60#61# =============================================================================62# CONFIDENTIALITY & SECURITY63# =============================================================================64#65# Classification : Public66# Distribution : Public repository; use is governed by the project license67# Security Contact : admin@spboucher.ai68#69# Access to this source does not independently grant permission70# to redistribute or disclose confidential materials.71#72# Do not embed credentials, private keys, access tokens,73# certificates or confidential personal information.74#75# =============================================================================76# LICENSE & MAINTENANCE77# =============================================================================78#79# License : None specified (no license file in this repository)80# Maintainer : Simon-Pierre Boucher81# Technical Contact: dev@spboucher.ai82#83# =============================================================================84# END OF SP BOUCHER SOURCE HEADER85# =============================================================================8687# ==============================================================================88# Author: Simon-Pierre Boucher <contact@spboucher.ai>89# File: src/main.py (ka-twitch)90# Desc: Profils Twitch détaillés via le GQL PUBLIC du web (Client-ID public91# kimne78kx3ncx6brgo4mv6wki5h1ko, celui du site twitch.tv) : abonnés,92# partenaire/affilié, live en cours, dernière diffusion, jeux récents,93# vidéos récentes avec vues. Pas de clé requise.94# ==============================================================================95from __future__ import annotations9697import asyncio98import json99100from apify import Actor101102from .net import Fetcher, UA103104GQL_URL = "https://gql.twitch.tv/gql"105CLIENT_ID = "kimne78kx3ncx6brgo4mv6wki5h1ko"106107QUERY = """108query($login: String!) {109 user(login: $login) {110 id111 login112 displayName113 description114 createdAt115 profileImageURL(width: 300)116 bannerImageURL117 followers { totalCount }118 roles { isPartner isAffiliate }119 primaryTeam { displayName }120 channel { chanlets { id } }121 lastBroadcast { startedAt title game { displayName } }122 stream { viewersCount createdAt game { displayName } }123 videos(first: 10, sort: TIME) {124 edges { node {125 id title viewCount lengthSeconds publishedAt game { displayName }126 } }127 }128 }129}130"""131132133async def main() -> None:134 async with Actor:135 inp = await Actor.get_input() or {}136 usernames = [u.strip().lstrip("@").lower()137 for u in (inp.get("usernames") or []) if u and u.strip()]138 proxy = await Actor.create_proxy_configuration(139 actor_proxy_input=inp.get("proxyConfiguration"))140 fetcher = Fetcher(proxy, delay=float(inp.get("requestDelay") or 1))141 sem = asyncio.Semaphore(int(inp.get("concurrency") or 4))142143 import random144 from curl_cffi import requests as cffi145146 async def gql(login: str) -> dict | None:147 body = json.dumps({"query": QUERY,148 "variables": {"login": login}})149 p = None150 if fetcher.proxy_configuration:151 p = await fetcher.proxy_configuration.new_url(152 session_id=f"tw{random.randint(1, 999_999)}")153 await fetcher._throttle()154 resp = await asyncio.to_thread(155 cffi.post, GQL_URL, data=body,156 headers={"Client-ID": CLIENT_ID, "User-Agent": UA,157 "Content-Type": "application/json"},158 impersonate="chrome", timeout=45,159 proxies={"http": p, "https": p} if p else None)160 if resp.status_code != 200:161 raise RuntimeError(f"gql http_{resp.status_code}")162 return (resp.json().get("data") or {}).get("user")163164 async def one(u: str) -> None:165 async with sem:166 miss = {"kind": "profile", "platform": "twitch",167 "username": u, "found": False}168 try:169 user = await gql(u)170 except Exception as exc:171 await Actor.push_data({**miss, "error": str(exc)[:200]})172 return173 if not user:174 await Actor.push_data({**miss, "error": "not_found"})175 return176 vids = []177 for e in ((user.get("videos") or {}).get("edges") or []):178 n = e.get("node") or {}179 vids.append({180 "id": n.get("id"),181 "url": f"https://www.twitch.tv/videos/{n.get('id')}",182 "title": (n.get("title") or "")[:200],183 "views": n.get("viewCount"),184 "duration_s": n.get("lengthSeconds"),185 "published_at": n.get("publishedAt"),186 "game": (n.get("game") or {}).get("displayName"),187 })188 views = [v["views"] for v in vids189 if isinstance(v["views"], int)]190 games = [v["game"] for v in vids if v["game"]]191 stream = user.get("stream") or {}192 last = user.get("lastBroadcast") or {}193 roles = user.get("roles") or {}194 await Actor.push_data({195 "kind": "profile",196 "platform": "twitch",197 "found": True,198 "id": user.get("id"),199 "username": user.get("login"),200 "full_name": user.get("displayName"),201 "biography": user.get("description"),202 "followers": ((user.get("followers") or {})203 .get("totalCount")),204 "is_partner": roles.get("isPartner"),205 "is_affiliate": roles.get("isAffiliate"),206 "is_verified": bool(roles.get("isPartner")),207 "team": ((user.get("primaryTeam") or {})208 .get("displayName")),209 "created_at": user.get("createdAt"),210 "avatar": user.get("profileImageURL"),211 "banner": user.get("bannerImageURL"),212 "is_live_now": bool(stream),213 "live_viewers": stream.get("viewersCount"),214 "live_game": (stream.get("game") or {}).get("displayName"),215 "last_broadcast_at": last.get("startedAt"),216 "last_broadcast_title": (last.get("title") or "")[:200]217 or None,218 "last_broadcast_game": ((last.get("game") or {})219 .get("displayName")),220 "avg_video_views": (round(sum(views) / len(views))221 if views else None),222 "recent_games": list(dict.fromkeys(games))[:5],223 "recent_videos": vids,224 })225226 await asyncio.gather(*[one(u) for u in usernames])227 Actor.log.info(f"terminé : {len(usernames)} profils")228