Files
treblo-mcp/cap_check.py
T

65 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
import os
import json
import datetime
from zoneinfo import ZoneInfo
TUNIS_TZ = ZoneInfo("Africa/Tunis")
def get_tunis_date(dt_str: str) -> str:
try:
dt = datetime.datetime.fromisoformat(dt_str)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=datetime.timezone.utc)
return dt.astimezone(TUNIS_TZ).strftime("%Y-%m-%d")
except Exception:
return dt_str[:10]
def count_today(sphere: str, journal_path: str) -> int:
if not os.path.exists(journal_path):
return 0
today = datetime.datetime.now(TUNIS_TZ).strftime("%Y-%m-%d")
count = 0
try:
with open(journal_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
entry = json.loads(line)
if entry.get("sphere") == sphere:
entry_date = entry.get("date", "")
if get_tunis_date(entry_date) == today:
count += 1
except Exception:
continue
except Exception:
pass
return count
def main():
sphere = sys.argv[1] if len(sys.argv) > 1 else "nyora"
sphere = sphere.lower().strip()
cap_str = os.environ.get("TREBLO_DAILY_CAP", "5")
try:
daily_cap = int(cap_str)
except ValueError:
daily_cap = 5
journal_path = "/app/data/journal.jsonl"
if not os.path.exists(journal_path):
local_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "journal.jsonl")
if os.path.exists(local_path):
journal_path = local_path
current_count = count_today(sphere, journal_path)
if current_count >= daily_cap:
print("CAP_REFUSED")
else:
print("CAP_OK")
if __name__ == "__main__":
main()