Compare commits

...

3 Commits

Author SHA1 Message Date
salt 42d9758ecc Make scrape fails on the AI rate limit dash non-critical 2026-06-19 17:12:59 -05:00
salt b34d5b1d97 Trim script 2026-06-15 00:08:47 -05:00
salt ff86eeff4b Ignore pycache 2026-06-15 00:08:41 -05:00
2 changed files with 12 additions and 76 deletions
@@ -0,0 +1 @@
__pycache__
+11 -76
View File
@@ -17,6 +17,10 @@ def waybar(text, klass="good", tooltip=None):
sys.exit(0)
def unknown(tooltip):
waybar('unk <span font-size="7pt">--</span>', "good", tooltip)
def load_auth():
auth_path = Path(os.environ.get("OPENAI_AUTH_FILE", "~/.codex/auth.json")).expanduser()
auth = {}
@@ -69,11 +73,11 @@ def fetch_usage(token, account_id):
return json.load(res)
except urllib.error.HTTPError as exc:
detail = exc.read().decode("utf-8", "replace")[:240]
waybar("AI rate failed", "critical", f"Usage endpoint returned HTTP {exc.code}\n{detail}")
unknown(f"Usage endpoint returned HTTP {exc.code}\n{detail}")
except (OSError, TimeoutError) as exc:
waybar("AI rate failed", "critical", f"Could not reach Codex usage endpoint\n{exc}")
unknown(f"Could not reach Codex usage endpoint\n{exc}")
except json.JSONDecodeError as exc:
waybar("AI rate unreadable", "critical", f"Usage endpoint returned invalid JSON\n{exc}")
unknown(f"Usage endpoint returned invalid JSON\n{exc}")
def window_label(seconds):
@@ -136,47 +140,12 @@ def format_window(name, window):
return {
"label": label,
"left": left,
"text": f"{left}% {label}",
"used": used,
"tooltip": tooltip,
}
def credit_text(credits):
if not isinstance(credits, dict) or not credits.get("has_credits"):
return None
if credits.get("unlimited"):
return "credits unlimited"
balance = credits.get("balance")
if balance is None:
return "credits enabled"
try:
return f"credits {round(float(balance))}"
except (TypeError, ValueError):
return f"credits {balance}"
def spend_control_text(spend_control):
limit = (spend_control or {}).get("individual_limit")
if not isinstance(limit, dict):
return None
remaining = limit.get("remaining_percent")
used = limit.get("used")
cap = limit.get("limit")
reset = reset_text(limit.get("reset_after_seconds"))
if not isinstance(remaining, int):
return None
text = f"monthly credits: {100 - remaining}% used"
if used and cap:
text += f" ({used}/{cap})"
if reset:
text += f", resets in {reset}"
return text
def selected_window_output(data, source, selector):
def output_window(data, source, selector):
rate_limit = data.get("rate_limit") or {}
window = format_window(selector, rate_limit.get(f"{selector}_window"))
if not window:
@@ -187,42 +156,12 @@ def selected_window_output(data, source, selector):
waybar(f"{window['left']}% <span font-size=\"7pt\">{window['label']}</span>", klass, "\n".join(tooltip))
def combined_output(data, source):
rate_limit = data.get("rate_limit") or {}
windows = [
format_window("primary", rate_limit.get("primary_window")),
format_window("secondary", rate_limit.get("secondary_window")),
]
windows = [window for window in windows if window]
parts = [window["text"] for window in windows]
credits = credit_text(data.get("credits"))
if credits:
parts.append(credits)
if not parts:
waybar("AI n/a", "warning", f"No displayable usage data\nsource: {source}")
worst_used = max(window["used"] for window in windows) if windows else 0
klass = usage_class(worst_used, rate_limit.get("limit_reached"))
tooltip = [window["tooltip"] for window in windows]
spend = spend_control_text(data.get("spend_control"))
if credits:
tooltip.append(credits)
if spend:
tooltip.append(spend)
tooltip.append(f"source: {source}")
waybar(f"AI {' '.join(parts)}", klass, "\n".join(tooltip))
def parse_args():
parser = ArgumentParser(description="Waybar Codex rate-limit widget")
parser.add_argument(
"--window",
choices=("primary", "secondary", "all"),
default="all",
choices=("primary", "secondary"),
required=True,
help="usage window to display",
)
return parser.parse_args()
@@ -232,11 +171,7 @@ def main():
args = parse_args()
token, account_id, source = load_auth()
data = fetch_usage(token, account_id)
if args.window == "all":
combined_output(data, source)
else:
selected_window_output(data, source, args.window)
output_window(data, source, args.window)
if __name__ == "__main__":