import pandas as pd, json, urllib.request, time, os, sys
sys.path.insert(0,'/tmp/rl'); from pool_of import pool_of
d=pd.read_pickle('clean.pkl')
x=d[(d.start=='small')&(d.fam!='campaign_weth')&(d['ts_500000'].notna())]
OUT='gt7.json'; got=json.load(open(OUT)) if os.path.exists(OUT) else {}
print('todo',len(x)-len(got),flush=True)
for i,(_,r) in enumerate(x.iterrows()):
    if r.tok in got: continue
    pool=pool_of(r.tok,r.day)
    u='https://api.geckoterminal.com/api/v2/networks/robinhood/pools/%s/ohlcv/hour?aggregate=1&limit=200&currency=usd&token=%s&before_timestamp=%d'%(pool,r.tok,int(r.t0)+7*86400+3600)
    for k in range(4):
        try:
            j=json.loads(urllib.request.urlopen(urllib.request.Request(u,headers={'User-Agent':'Mozilla/5.0','Accept':'application/json'}),timeout=25).read())
            got[r.tok]=dict(pool=pool,ohlcv=j['data']['attributes']['ohlcv_list']); break
        except Exception as e:
            if '404' in str(e): got[r.tok]=dict(pool=pool,ohlcv=None,err='404'); break
            time.sleep(10*(k+1))
    if i%25==0: json.dump(got,open(OUT,'w')); print(i,time.strftime('%H:%M:%S'),flush=True)
    time.sleep(2.1)
json.dump(got,open(OUT,'w')); print('done',len(got),flush=True)
