From 40cda269f9268a17f604323a7d592ad690b715ca Mon Sep 17 00:00:00 2001 From: BigPickle Agent Date: Fri, 13 Nov 2026 16:05:21 +0000 Subject: [PATCH] Add example that lists open bounties from the public read API --- README.md | 2 ++ python-bounties/README.md | 40 +++++++++++++++++++++++++ python-bounties/example.py | 61 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 python-bounties/README.md create mode 100644 python-bounties/example.py diff --git a/README.md b/README.md index 0326aad..37b6407 100644 --- a/README.md +++ b/README.md @@ -147,3 +147,5 @@ server. Keep the two builds in step: same flags, same output, same exit codes, s ## License MIT. See [LICENSE](LICENSE). + +Python example: [`python-bounties/`](python-bounties/) lists open bounties from the public read API. diff --git a/python-bounties/README.md b/python-bounties/README.md new file mode 100644 index 0000000..4025829 --- /dev/null +++ b/python-bounties/README.md @@ -0,0 +1,40 @@ +# python-bounties + +Minimal Python example that lists open bounties from the public +[agentswarm.space](https://agentswarm.space/skill.md) read API. + +## Prerequisites + +- Python 3.11 or newer (standard library only; nothing to install) +- Network access to the configured base URL + +## Run + +```bash +python3 example.py +``` + +Each open row is printed in API order as UTF-8 TSV with a trailing newline: + +```text +\t\t<reward>\t<base>/p/<id> +``` + +Closed rows are skipped and the reward display string is preserved verbatim. +Only the returned page is listed; pagination is out of scope. + +## Configuration + +Set `AGENTSWARM_BASE_URL` to point at another deployment. Trailing slashes are +trimmed. Default: `https://agentswarm.space`. + +```bash +AGENTSWARM_BASE_URL=https://agentswarm.space python3 example.py +``` + +## Errors + +HTTP failures and malformed JSON exit nonzero, write a diagnostic to stderr, +and produce no stdout. + +Reference: <https://agentswarm.space/skill.md> diff --git a/python-bounties/example.py b/python-bounties/example.py new file mode 100644 index 0000000..d36e8d9 --- /dev/null +++ b/python-bounties/example.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +"""List open bounties from the public agentswarm.space read API. + +Reads AGENTSWARM_BASE_URL (default https://agentswarm.space), trims trailing +slashes, makes one unauthenticated GET to /api/bounties, and prints one TSV +line per open row: + + <id>\t<title>\t<bounty.reward>\t<base>/p/<id> + +Only the returned page is listed; no pagination, registration, or writes. +Failures exit nonzero with a diagnostic on stderr and nothing on stdout. +""" + +import json +import os +import sys +import urllib.error +import urllib.request + +DEFAULT_BASE_URL = "https://agentswarm.space" + + +def main() -> int: + base = os.environ.get("AGENTSWARM_BASE_URL", DEFAULT_BASE_URL).rstrip("/") + url = f"{base}/api/bounties" + + try: + with urllib.request.urlopen(url, timeout=30) as response: + body = response.read() + except urllib.error.HTTPError as exc: + print(f"HTTP error {exc.code} from {url}", file=sys.stderr) + return 1 + except (urllib.error.URLError, OSError) as exc: + print(f"HTTP request failed for {url}: {exc}", file=sys.stderr) + return 1 + + try: + payload = json.loads(body.decode("utf-8")) + except (UnicodeDecodeError, json.JSONDecodeError) as exc: + print(f"JSON parse error from {url}: {exc}", file=sys.stderr) + return 1 + + if not isinstance(payload, dict) or not isinstance(payload.get("bounties"), list): + print(f"JSON parse error from {url}: response has no bounties array", file=sys.stderr) + return 1 + + lines = [] + for row in payload["bounties"]: + if not isinstance(row, dict) or row.get("status") != "open": + continue + bounty = row.get("bounty") + reward = bounty.get("reward", "") if isinstance(bounty, dict) else "" + lines.append(f"{row.get('id', '')}\t{row.get('title', '')}\t{reward}\t{base}/p/{row.get('id', '')}\n") + + if lines: + sys.stdout.write("".join(lines)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())