49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
"""
|
|
Тест получения числа зрителей через Twitch GQL.
|
|
Запуск: python test_viewers.py <канал>
|
|
"""
|
|
|
|
import asyncio
|
|
import sys
|
|
import aiohttp
|
|
|
|
GQL_URL = "https://gql.twitch.tv/gql"
|
|
HEADERS = {
|
|
"Client-ID": "kimne78kx3ncx6brgo4mv6wki5h1ko",
|
|
"Content-Type": "application/json",
|
|
}
|
|
QUERY = """
|
|
query ($login: String!) {
|
|
user(login: $login) {
|
|
stream {
|
|
viewersCount
|
|
title
|
|
game { name }
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
|
|
|
|
async def main():
|
|
channel = sys.argv[1] if len(sys.argv) > 1 else "shroud"
|
|
payload = {"query": QUERY, "variables": {"login": channel.lower()}}
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.post(
|
|
GQL_URL, json=payload, headers=HEADERS,
|
|
timeout=aiohttp.ClientTimeout(total=8)
|
|
) as resp:
|
|
data = await resp.json()
|
|
|
|
stream = data.get("data", {}).get("user", {}).get("stream")
|
|
if stream:
|
|
print(f"Зрителей: {stream['viewersCount']}")
|
|
print(f"Игра: {stream.get('game', {}).get('name', '—')}")
|
|
print(f"Тайтл: {stream.get('title', '—')}")
|
|
else:
|
|
print(f"Канал {channel} оффлайн или не найден")
|
|
|
|
|
|
asyncio.run(main())
|