#!/usr/bin/env python3
import json, os, sys, time, datetime as dt, urllib.request

ROOT = os.path.expanduser("~/kleiner")
DATA = os.path.join(ROOT, "data")
INTRA = os.path.join(DATA, "intraday")
ENVF = os.path.join(ROOT, ".env.json")
WATCHLIST = os.path.join(DATA, "watchlist.json")

def load_env():
    with open(ENVF,"r",encoding="utf-8") as f: return json.load(f)

def http_get(url):
    req = urllib.request.Request(url, headers={"Accept":"application/json"})
    with urllib.request.urlopen(req, timeout=30) as r:
        return json.loads(r.read().decode("utf-8"))

def read_watchlist():
    with open(WATCHLIST,"r",encoding="utf-8") as f:
        w=json.load(f)
    if isinstance(w, list) and w and isinstance(w[0], dict):
        return [x.get("symbol","").upper() for x in w if x.get("symbol")]
    return [str(x).upper() for x in w]

def fetch_intra(tk, key, tf_minutes, days=7):
    base="https://api.polygon.io/v2/aggs/ticker"
    end=dt.datetime.utcnow().date()
    start=(end - dt.timedelta(days=days))
    url=f"{base}/{tk}/range/{tf_minutes}/minute/{start.isoformat()}/{end.isoformat()}?adjusted=true&sort=asc&limit=50000&apiKey={key}"
    js=http_get(url)
    out=[]
    for r in js.get("results",[]) or []:
        ts=int(r.get("t",0))/1000.0
        iso=dt.datetime.utcfromtimestamp(ts).replace(microsecond=0).isoformat()+"Z"
        out.append({
            "ts": iso,
            "o": float(r.get("o",0)),
            "h": float(r.get("h",0)),
            "l": float(r.get("l",0)),
            "c": float(r.get("c",0)),
            "v": int(r.get("v",0))
        })
    return out

def main():
    key=load_env()["POLYGON_KEY"]
    os.makedirs(INTRA, exist_ok=True)
    tks=read_watchlist()
    for tk in tks:
        for tf, label in [(15,"15m"), (60,"1h")]:
            try:
                arr=fetch_intra(tk, key, tf)
                path=os.path.join(INTRA, f"{tk}_{label}.json")
                tmp=path+".tmp"
                with open(tmp,"w",encoding="utf-8") as f: json.dump(arr,f,ensure_ascii=False)
                os.replace(tmp,path)
                print("intra ok:", tk, label, len(arr))
                time.sleep(0.2)
            except Exception as e:
                print("intra err:", tk, label, e)
                time.sleep(0.6)

if __name__=="__main__":
    main()
