Created
February 13, 2026 20:49
-
-
Save ahhh/8ceb0ea235c8a25937a0ae884e517f4c to your computer and use it in GitHub Desktop.
metactf scoreboard service checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import sys | |
| import requests | |
| from typing import Any, Dict, List, Optional | |
| API_URL = "https://api.ad.mctf.io/status" | |
| TEAM_TOKEN = "" | |
| ACTIONS = ["check", "put", "get_last", "get_random"] | |
| def fetch_status() -> Dict[str, Any]: | |
| r = requests.get( | |
| API_URL, | |
| headers={"accept": "application/json", "team-token": TEAM_TOKEN}, | |
| timeout=10, | |
| ) | |
| r.raise_for_status() | |
| return r.json() | |
| def find_team(data: Dict[str, Any], team_id: int) -> Optional[Dict[str, Any]]: | |
| for t in data.get("teams", []): | |
| if t.get("team_id") == team_id: | |
| return t | |
| return None | |
| def latest_value(arr: List[Any]) -> Any: | |
| return arr[-1] if arr else None | |
| def format_action_result(action_obj: Dict[str, Any]) -> str: | |
| # action_obj looks like {"success": bool|null, "message": "..."} | |
| success = action_obj.get("success") | |
| msg = (action_obj.get("message") or "").strip() | |
| if success is True: | |
| return "OK" | |
| if success is False: | |
| return f"FAIL: {msg}" if msg else "FAIL" | |
| # success can be None | |
| return "N/A" | |
| def summarize_team5(team: Dict[str, Any], tick: Any) -> str: | |
| lines = [] | |
| lines.append(f"Tick: {tick}") | |
| lines.append(f"Team: {team.get('team_name')} (id={team.get('team_id')})") | |
| lines.append("") | |
| for svc in team.get("services", []): | |
| svc_name = svc.get("service_name") | |
| svc_id = svc.get("service_id") | |
| host = svc.get("hostname") | |
| availability = svc.get("availability", []) | |
| avail_latest = latest_value(availability) | |
| messages = svc.get("messages", []) | |
| msg_latest = latest_value(messages) or {} | |
| lines.append(f"=== Service {svc_id}: {svc_name} ===") | |
| lines.append(f"Host: {host}") | |
| lines.append(f"Availability (latest): {avail_latest}") | |
| lines.append(f"Availability (history): {availability}") | |
| # Latest action results | |
| lines.append("Latest results:") | |
| for action in ACTIONS: | |
| result = format_action_result(msg_latest.get(action, {})) | |
| lines.append(f" - {action}: {result}") | |
| # Quick failure summary for latest message | |
| failed_actions = [] | |
| for action in ACTIONS: | |
| a = msg_latest.get(action, {}) | |
| if a.get("success") is False: | |
| failed_actions.append((action, (a.get("message") or "").strip())) | |
| if failed_actions: | |
| lines.append("Failures (latest):") | |
| for action, msg in failed_actions: | |
| lines.append(f" * {action}: {msg if msg else '(no message)'}") | |
| else: | |
| lines.append("Failures (latest): none") | |
| lines.append("") # spacer | |
| return "\n".join(lines) | |
| def main() -> int: | |
| try: | |
| data = fetch_status() | |
| except requests.RequestException as e: | |
| print(f"ERROR: failed to fetch status: {e}", file=sys.stderr) | |
| return 2 | |
| tick = data.get("tick") | |
| team5 = find_team(data, team_id=5) | |
| if not team5: | |
| print("ERROR: Team 5 not found in response", file=sys.stderr) | |
| return 1 | |
| print(summarize_team5(team5, tick)) | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment