Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

6.7 KB
Raw Blame
import re
import hmac
import hashlib
from collections.abc import Iterator
from datetime import UTC, datetime
from typing import Any

from django.conf import settings

from requests import HTTPError, RequestException, Session
from structlog.types import FilteringBoundLogger

from products.warehouse_sources.sources.sdk import (
    SourceResponse,
    make_tracked_session,
    redact_literal_values,
    validate_via_probe,
)
from products.warehouse_sources.sources.steam.settings import OWNED_GAMES, PLAYERS, PLAYTIME_SNAPSHOTS, PRIMARY_KEYS

STEAM_API_URL = "https://api.steampowered.com"
REQUEST_TIMEOUT_SECONDS = 30
# GetPlayerSummaries accepts at most this many ids in one request.
PLAYER_SUMMARIES_BATCH = 100

_STEAM_ID_RE = re.compile(r"^[0-9]{17}$")


def split_steam_ids(raw: str) -> list[str]:
    return list(dict.fromkeys(part for part in re.split(r"[\s,]+", raw.strip()) if part))


def parse_steam_ids(raw: str) -> list[str]:
    """The 64-bit Steam IDs in a comma or whitespace separated list, without duplicates."""
    steam_ids = split_steam_ids(raw)
    invalid = [steam_id for steam_id in steam_ids if not _STEAM_ID_RE.match(steam_id)]
    if invalid:
        raise ValueError(f"Not a 17-digit Steam ID: {', '.join(invalid)}")
    if not steam_ids:
        raise ValueError("Add at least one Steam ID.")
    return steam_ids


def player_key(team_id: int, steam_id: str) -> str:
    """An opaque key for a player. It is stable across syncs and different in every project."""
    # A Steam ID is public and opens the player's profile, so the tables must not hold it.
    # The secret stops anyone from hashing every Steam ID to reverse a key.
    message = f"{team_id}:{steam_id}".encode()
    return hmac.new(settings.SECRET_KEY.encode(), message, hashlib.sha256).hexdigest()[:32]


def probe_api_key(api_key: str) -> int:
    """The HTTP status of a minimal request with this key, or -1 when Steam cannot be reached."""
    _ok, status = validate_via_probe(
        lambda: make_tracked_session(redact_values=(api_key,)),
        f"{STEAM_API_URL}/ISteamUser/GetPlayerSummaries/v2/?key={api_key}&steamids=0",
        timeout=10,
    )
    return status if status is not None else -1


def _get(session: Session, api_key: str, path: str, params: dict[str, Any]) -> dict[str, Any]:
    # The request URL carries the API key, and `requests` puts the URL in its error messages.
    try:
        response = session.get(
            f"{STEAM_API_URL}{path}", params={"key": api_key, **params}, timeout=REQUEST_TIMEOUT_SECONDS
        )
    except RequestException as error:
        raise type(error)(redact_literal_values(str(error), (api_key,))) from None
    if not response.ok:
        kind = "Client" if response.status_code < 500 else "Server"
        raise HTTPError(f"{response.status_code} {kind} Error: {response.reason}", response=response)
    return response.json().get("response") or {}


def _timestamp(value: int | None) -> datetime | None:
    return datetime.fromtimestamp(value, tz=UTC) if value else None


def _players(session: Session, api_key: str, team_id: int, steam_ids: list[str]) -> Iterator[list[dict[str, Any]]]:
    for start in range(0, len(steam_ids), PLAYER_SUMMARIES_BATCH):
        batch = steam_ids[start : start + PLAYER_SUMMARIES_BATCH]
        body = _get(session, api_key, "/ISteamUser/GetPlayerSummaries/v2/", {"steamids": ",".join(batch)})
        rows = [
            {
                "player_key": player_key(team_id, player["steamid"]),
                # 3 is a public profile. Steam returns no games for any other value.
                "is_public": player.get("communityvisibilitystate") == 3,
            }
            for player in body.get("players") or []
        ]
        if rows:
            yield rows


def _owned_games(
    session: Session, api_key: str, team_id: int, steam_ids: list[str], logger: FilteringBoundLogger
) -> Iterator[list[dict[str, Any]]]:
    for steam_id in steam_ids:
        body = _get(
            session,
            api_key,
            "/IPlayerService/GetOwnedGames/v1/",
            {"steamid": steam_id, "include_appinfo": 1, "include_played_free_games": 1},
        )
        games = body.get("games") or []
        if not games:
            logger.warning(
                "Steam returned no games for a player. Their game details are probably private.",
                player_key=player_key(team_id, steam_id),
            )
            continue
        yield [
            {
                "player_key": player_key(team_id, steam_id),
                "app_id": game["appid"],
                "name": game.get("name"),
                "playtime_forever_minutes": game.get("playtime_forever"),
                "playtime_2weeks_minutes": game.get("playtime_2weeks") or 0,
                "last_played_at": _timestamp(game.get("rtime_last_played")),
            }
            for game in games
        ]


def _playtime_snapshots(
    session: Session, api_key: str, team_id: int, steam_ids: list[str], logger: FilteringBoundLogger
) -> Iterator[list[dict[str, Any]]]:
    snapshot_date = datetime.now(UTC).date()
    for steam_id in steam_ids:
        body = _get(session, api_key, "/IPlayerService/GetRecentlyPlayedGames/v1/", {"steamid": steam_id})
        rows = [
            {
                "player_key": player_key(team_id, steam_id),
                "app_id": game["appid"],
                "name": game.get("name"),
                "snapshot_date": snapshot_date,
                "playtime_forever_minutes": game.get("playtime_forever"),
                "playtime_2weeks_minutes": game.get("playtime_2weeks") or 0,
            }
            for game in body.get("games") or []
        ]
        if rows:
            yield rows


def steam_source(
    api_key: str, team_id: int, steam_ids: list[str], endpoint: str, logger: FilteringBoundLogger
) -> SourceResponse:
    def get_rows() -> Iterator[list[dict[str, Any]]]:
        session = make_tracked_session(redact_values=(api_key,))
        if endpoint == PLAYERS:
            yield from _players(session, api_key, team_id, steam_ids)
        elif endpoint == OWNED_GAMES:
            yield from _owned_games(session, api_key, team_id, steam_ids, logger)
        else:
            yield from _playtime_snapshots(session, api_key, team_id, steam_ids, logger)

    if endpoint == PLAYTIME_SNAPSHOTS:
        # Steam reports running totals and no sessions. One row per game per day lets a query
        # subtract consecutive days to get the minutes played on each day.
        return SourceResponse(
            name=endpoint,
            items=get_rows,
            primary_keys=PRIMARY_KEYS[endpoint],
            partition_mode="datetime",
            partition_format="month",
            partition_keys=["snapshot_date"],
        )
    return SourceResponse(name=endpoint, items=get_rows, primary_keys=PRIMARY_KEYS[endpoint])