"""Probe OpenDota + Steam for a player (no secret echo).""" from __future__ import annotations import json import os import urllib.error import urllib.parse import urllib.request AID = 143712136 SID = AID + 76561197960265728 UA = "climperor-probe" def _get(url: str) -> tuple[int, object]: req = urllib.request.Request(url, headers={"User-Agent": UA}) try: with urllib.request.urlopen(req, timeout=25) as resp: return resp.status, json.loads(resp.read().decode()) except urllib.error.HTTPError as e: body = e.read().decode(errors="replace")[:200] return e.code, {"error": body} except Exception as e: # noqa: BLE001 return 0, {"error": str(e)} def main() -> None: key = ( os.environ.get("STEAM_API_KEY") or os.environ.get("KEYZOO_ASSET_SECRET_WEB_API_KEY") or "" ).strip() od_key = (os.environ.get("OPENDOTA_API_KEY") or "").strip() print(f"steam_key={bool(key)} opendota_key={bool(od_key)}") od = f"https://api.opendota.com/api/players/{AID}" if od_key: od += f"?api_key={urllib.parse.quote(od_key)}" code, data = _get(od) if isinstance(data, dict) and "error" not in data: profile = data.get("profile") if isinstance(data.get("profile"), dict) else {} print( "opendota player", code, "name=", profile.get("personaname") or data.get("personaname"), "rank_tier=", data.get("rank_tier"), "lb=", data.get("leaderboard_rank"), ) else: print("opendota player", code, data) od_r = f"https://api.opendota.com/api/players/{AID}/recentMatches" if od_key: od_r += f"?api_key={urllib.parse.quote(od_key)}" code, data = _get(od_r) if isinstance(data, list): print("opendota recent", code, "n=", len(data)) if data: print(" first", data[0].get("match_id"), data[0].get("start_time")) else: print("opendota recent", code, data) if key: q = urllib.parse.urlencode({"key": key, "steamids": str(SID)}) code, data = _get( f"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?{q}" ) players = (((data or {}).get("response") or {}).get("players")) or [] if players: p = players[0] print( "steam summary", code, "name=", p.get("personaname"), "loc=", p.get("loccountrycode"), ) else: print("steam summary", code, data) q = urllib.parse.urlencode( { "key": key, "account_id": AID, "matches_requested": 10, } ) code, data = _get( "https://api.steampowered.com/IDOTA2Match_570/GetMatchHistory/v1/?" + q ) if isinstance(data, dict): result = (data.get("result") or {}) print( "steam match_history", code, "status=", result.get("status"), "statusDetail=", result.get("statusDetail"), "num=", result.get("num_results"), "total=", result.get("total_results"), ) matches = result.get("matches") or [] if matches: m0 = matches[0] print(" first match_id", m0.get("match_id"), "start", m0.get("start_time")) else: print("steam match_history", code, data) if __name__ == "__main__": main()