"""GeckoTerminal: pools, hourly 8 d from launch, minute first 5 h, for candidates then controls. Read-only, cached."""
import json, urllib.request, time, os, sys, datetime
L = json.load(open('/tmp/rl/r20/list.json'))
OUT = '/tmp/rl/r20/gecko.json'
G = json.load(open(OUT)) if os.path.exists(OUT) else {}
H = {'User-Agent': 'Mozilla/5.0', 'Accept': 'application/json'}
def get(u):
    for k in range(6):
        try:
            return json.loads(urllib.request.urlopen(urllib.request.Request(u, headers=H), timeout=30).read())
        except Exception as e:
            if '404' in str(e): return None
            time.sleep(8 * (k + 1))
    return None
def ts(s): return int(datetime.datetime.fromisoformat(s.replace('Z', '+00:00')).timestamp())
items = L['cand'] + L['ctl']
for i, it in enumerate(items):
    t = it['tok']
    if t in G: continue
    rec = dict(src=it['src'], sym=it['sym'])
    j = get('https://api.geckoterminal.com/api/v2/networks/robinhood/tokens/%s/pools?page=1&include=dex' % t); time.sleep(2.1)
    pools = []
    for p in (j or {}).get('data', []):
        a = p['attributes']
        pools.append(dict(pool=a['address'], dex=p['relationships']['dex']['data']['id'], created=ts(a['pool_created_at']) if a.get('pool_created_at') else None,
                          reserve=float(a.get('reserve_in_usd') or 0), vol24=float((a.get('volume_usd') or {}).get('h24') or 0), name=a.get('name')))
    rec['pools'] = pools
    if pools:
        early = min((p for p in pools if p['created']), key=lambda p: p['created'], default=pools[0])
        main = max(pools, key=lambda p: p['reserve'])
        rec['early'] = early['pool']; rec['main'] = main['pool']; t0 = early['created'] or 0
        rec['launch'] = t0
        for nm, pool in (('h_early', early['pool']), ('h_main', main['pool'])):
            if nm == 'h_main' and main['pool'] == early['pool']: rec['h_main'] = None; continue
            jj = get('https://api.geckoterminal.com/api/v2/networks/robinhood/pools/%s/ohlcv/hour?aggregate=1&limit=1000&currency=usd&token=%s&before_timestamp=%d' % (pool, t, t0 + 8 * 86400)); time.sleep(2.1)
            rec[nm] = (jj or {}).get('data', {}).get('attributes', {}).get('ohlcv_list') if jj else None
        jj = get('https://api.geckoterminal.com/api/v2/networks/robinhood/pools/%s/ohlcv/minute?aggregate=1&limit=1000&currency=usd&token=%s&before_timestamp=%d' % (early['pool'], t, t0 + 5 * 3600)); time.sleep(2.1)
        rec['m_early'] = (jj or {}).get('data', {}).get('attributes', {}).get('ohlcv_list') if jj else None
    G[t] = rec
    if i % 10 == 0:
        json.dump(G, open(OUT, 'w')); print(i, len(items), time.strftime('%H:%M:%S'), flush=True)
json.dump(G, open(OUT, 'w')); print('done', len(G), flush=True)
