Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.3 kB
2
Indexable
Never
def get_historical_onchain(coin, metricID):
    df = pd.DataFrame(columns=['timestamp'])
    
    url = "https://data.messari.io/api/v1/assets/{assetKey}/metrics/{metricID}/time-series?start=2017-01-01&end=2022-03-28&interval=1d&format=json&timestamp-format=rfc3339".format(assetKey=coin,metricID=metricID)
    r = requests.get(url, headers={"x-messari-api-key":messari_api_key})
    if r.status_code != 200:
        print(r.json()['status']['error_message'])
    else:
        df = pd.DataFrame(r.json()['data']['values'], columns=r.json()['data']['parameters']['columns'])
    return df

def get_historical_df(coin_list, metric_id_list):
    for coin in coin_list:
        df = pd.DataFrame(columns=['timestamp'])
        print(coin)
        
        for metric_id in tqdm.tqdm(metric_id_list):
            new_df = get_historical_onchain(coin, metric_id)
            time.sleep(2)
            df = df.merge(new_df, how='outer',on='timestamp')
            df = df.sort_values(by='timestamp')
        # df.to_csv("E:/onchain/"+coin+'_onchain.csv', index=False)
        df.to_csv(coin, index=False)

btc = get_historical_onchain('BTC','mcap.dom')
btc.to_csv('./btc_dominance.csv')

# If you want to aggregate multiple coins' data into one csv
coin_list = ['BTC','ETH','SOL']
get_historical_df(coin_list. ['mcap.dom'])