Building a Real-Time Twitch Stats Dashboard with Python
Tracking streamer metrics in real-time is one of the most rewarding projects for any developer interested in the live streaming ecosystem. In this guide, I will walk you through building a lightweight Twitch stats dashboard using Python, the Twitch API, and a simple web frontend.
Why Track Streamer Stats?
Whether you are a content creator looking to optimize your schedule, or a viewer curious about growth trends, having access to real-time data like viewer count, follower evolution, and stream uptime is incredibly valuable. Platforms like Optistream already aggregate detailed streamer profiles and analytics โ our goal here is to build something similar from scratch.
Setting Up the Twitch API
First, register an application on the Twitch Developer Console to get your Client ID and Secret.
import requests
import time
CLIENT_ID = "your_client_id"
CLIENT_SECRET = "your_client_secret"
def get_oauth_token():
url = "https://id.twitch.tv/oauth2/token"
params = {
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"grant_type": "client_credentials"
}
resp = requests.post(url, params=params)
return resp.json()["access_token"]
Fetching Streamer Data
With the token in hand, we can query any streamer's live status and channel info:
def get_stream_info(username, token):
headers = {
"Client-ID": CLIENT_ID,
"Authorization": f"Bearer {token}"
}
url = f"https://api.twitch.tv/helix/streams?user_login={username}"
resp = requests.get(url, headers=headers)
data = resp.json()["data"]
if data:
stream = data[0]
return {
"title": stream["title"],
"game": stream["game_name"],
"viewers": stream["viewer_count"],
"started_at": stream["started_at"]
}
return None
Building the Dashboard
For the frontend, a simple Flask app with auto-refresh works well:
from flask import Flask, render_template_string
app = Flask(__name__)
TEMPLATE = """
<html>
<head><title>Twitch Dashboard</title>
<meta http-equiv="refresh" content="30"></head>
<body>
<h1>{{ streamer }} - Live Stats</h1>
<p>Viewers: {{ viewers }}</p>
<p>Game: {{ game }}</p>
<p>Uptime: {{ uptime }}</p>
</body></html>
"""
@app.route("/dashboard/<username>")
def dashboard(username):
token = get_oauth_token()
info = get_stream_info(username, token)
if info:
return render_template_string(TEMPLATE,
streamer=username, viewers=info["viewers"],
game=info["game"], uptime=info["started_at"])
return f"{username} is offline"
Going Further
To take this further, you could:
- Store historical data in SQLite and plot trends with matplotlib
- Add follower count tracking via the
/helix/channels/followersendpoint - Compare multiple streamers side by side
- Deploy on a Raspberry Pi for a permanent display
If you want to explore streamer profiles without building anything, check out the streamer directory on optistream.fr โ it provides detailed pages for thousands of Twitch streamers with stats, bios, and streaming schedules.
Conclusion
Building a Twitch stats dashboard is a great weekend project that combines API integration, data processing, and web development. The Twitch ecosystem is rich with data, and tools like this help the community better understand streaming trends.
Happy coding! ๐ฎ
Top comments (0)