-
-
Save rafaelugolini/063360764295b31b34e176de33e80171 to your computer and use it in GitHub Desktop.
dune to csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathlib | |
import os | |
import sys | |
import pandas as pd | |
from duneanalytics import DuneAnalytics | |
USERNAME = os.getenv("DUNE_USERNAME") | |
PASSWORD = os.getenv("DUNE_PASSWORD") | |
# dune client | |
def _get_client() -> DuneAnalytics: | |
# initialize client | |
dune = DuneAnalytics(USERNAME, PASSWORD) | |
# try to login | |
dune.login() | |
# fetch token | |
dune.fetch_auth_token() | |
return dune | |
client = _get_client() | |
# save query to csv | |
def _save_query_to_csv(name: str, query_id: int): | |
print(f"save_query: {name}") | |
# creates file path | |
file_path = os.path.join(pathlib.Path(__file__).parent.absolute(), f"./{name}.csv") | |
# retrieve data | |
result_id = client.query_result_id(query_id=query_id) | |
data = client.query_result(result_id) | |
# check if data is ok | |
try: | |
data = [d["data"] for d in data["data"]["get_result_by_result_id"]] | |
except Exception as e: | |
print(f"ERROR: {name}:{query_id}") | |
print(e) | |
print("Error getting data, check if the authentication is working") | |
sys.exit(-1) | |
# create csv | |
columns = data[0].keys() | |
values = [d.values() for d in data] | |
df = pd.DataFrame(values, columns=columns) | |
df.to_csv(file_path, index=False) | |
if __name__ == "__main__": | |
# export DUNE_USERNAME="..." | |
# export DUNE_PASSWORD="..." | |
_save_query_to_csv("cvx_pool_info", 1180002) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice nice