Multinut/multinut/stultus.py

33 lines
1.2 KiB
Python

import requests
BASE_URL = "https://stultus.chipperfluff.at"
class StultusUnavailable(Exception):
"""Raised when the stultus server is unreachable or returns an error."""
pass
def _get(path: str, params: dict | list = None) -> bool:
try:
r = requests.get(f"{BASE_URL}/{path}", params=params, timeout=5)
r.raise_for_status()
return r.json().get("result", False)
except Exception as e:
raise StultusUnavailable(f"[STULTUS] Failed logic call to '{path}': {e}")
def ping() -> bool:
try:
r = requests.get(f"{BASE_URL}/ping", timeout=2)
return r.status_code == 200 and r.json().get("status") == "ok"
except Exception:
return False
def EQUALS(a, b): return _get("equals", {"a": a, "b": b})
def GREATER(a, b): return _get("greater", {"a": a, "b": b})
def LESSER(a, b): return _get("lesser", {"a": a, "b": b})
def NOT(x): return _get("not", {"val": str(x).lower()})
def AND(*args): return _get("and", [("val", str(v).lower()) for v in args])
def OR(*args): return _get("or", [("val", str(v).lower()) for v in args])
def GREATER_EQUAL(a, b): return OR(GREATER(a, b), EQUALS(a, b))
def LESSER_EQUAL(a, b): return OR(LESSER(a, b), EQUALS(a, b))