#!/usr/bin/env python3
# © 2026 Osama Mohamed. All rights reserved.
"""Send a CSV or JSON file to Calerio. Python 3 standard library only.
Usage: python3 calerio_push.py FILE KEY [DEPARTMENT]
Run it from cron (Linux/Mac) or Task Scheduler (Windows) on your own server.
Limit: 100 requests per key per day."""
import sys, urllib.request, urllib.error, os
URL = "https://calerio.wazayef-callcenter.workers.dev/api/ingest"
if len(sys.argv) < 3:
    sys.exit("usage: calerio_push.py FILE KEY [DEPARTMENT]")
path, key = sys.argv[1], sys.argv[2]
dept = sys.argv[3] if len(sys.argv) > 3 else "General"
ctype = "application/json" if path.lower().endswith(".json") else "text/csv"
with open(path, "rb") as f:
    data = f.read()
req = urllib.request.Request(URL + "?dept=" + urllib.request.quote(dept), data=data, method="POST",
    headers={"Authorization": "Bearer " + key, "Content-Type": ctype, "User-Agent": "calerio-push/1.0"})
try:
    with urllib.request.urlopen(req, timeout=30) as r:
        print(r.status, r.read().decode()[:300])
except urllib.error.HTTPError as e:
    print(e.code, e.read().decode()[:300]); sys.exit(1)
