Skip to content

Commit

Permalink
Use DuneClient instead of Raw Request (#3)
Browse files Browse the repository at this point in the history
## Test Plan:

```sh
docker-compose up -d
python -m src.main
```

If you have your .env set with a valid `DUNE_API_KEY`, you should see
something like:

```
2024-10-08 23:16:23,020 INFO dune_client.api.base executing 4132129 on medium cluster
2024-10-08 23:16:23,254 INFO dune_client.api.base waiting for query execution 01J9PZV08G4FD64YVFV6NFZHPE to complete: ExecutionState.PENDING (queue position: 1)
Data saved to PostgreSQL successfully!
```
  • Loading branch information
bh2smith authored Oct 13, 2024
1 parent a390670 commit c7ab5dd
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 32 deletions.
6 changes: 5 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
DB_URL=postgresql://postgres:postgres@localhost:5432/postgres
DUNE_API_KEY=
DB_URL=postgresql://postgres:postgres@localhost:5432/postgres

QUERY_ID=
POLL_FREQUENCY=10
QUERY_ENGINE=medium
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VENV = venv
VENV = .venv
PYTHON = $(VENV)/bin/python3
PIP = $(VENV)/bin/pip
PROJECT_ROOT = src
Expand All @@ -25,7 +25,6 @@ lint:
check:
make fmt
make lint
make types

run:
python -m src.main
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Dune Sync V2




## Local Development

Fill out the empty fields in [Sample Env](.env.sample) (`DUNE_API_KEY` and `QUERY_ID`)

```shell
docker-compose up -d
python -m src.main
```
5 changes: 0 additions & 5 deletions requirements.txt

This file was deleted.

2 changes: 1 addition & 1 deletion requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
requests
dune-client>=1.7.7
pandas
sqlalchemy
python-dotenv
Expand Down
48 changes: 25 additions & 23 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import os
import requests
import pandas as pd
from dune_client.client import DuneClient
from dune_client.query import QueryBase
from sqlalchemy import create_engine
from dotenv import load_dotenv

from src.types import DUNE_TO_PG

load_dotenv()
DUNE_API_KEY = os.getenv("DUNE_API_KEY")
DB_URL = os.getenv("DB_URL")
TABLE_NAME = "dune_data"
DUNE_API_KEY = os.environ.get("DUNE_API_KEY")
DB_URL = os.environ.get("DB_URL")

API_URL = "https://api.dune.com/api/v1/query/4132129/results"
HEADERS = {"x-dune-api-key": DUNE_API_KEY}
# TODO(bh2smith): parse config file for most of the following stuff
QUERY_ID = int(os.environ.get("QUERY_ID"))
POLL_FREQUENCY = int(os.environ.get("POLL_FREQUENCY"))
QUERY_ENGINE = os.environ.get("QUERY_ENGINE")
TABLE_NAME = f"dune_data_{QUERY_ID}"


def reformat_varbinary_columns(df, varbinary_columns):
Expand All @@ -22,23 +25,22 @@ def reformat_varbinary_columns(df, varbinary_columns):


def fetch_dune_data():
response = requests.get(API_URL, headers=HEADERS, timeout=10)
if response.status_code == 200:
data = response.json()
result = data["result"]
metadata = result["metadata"]
dtypes, varbinary_columns = {}, []
for name, d_type in zip(metadata["column_names"], metadata["column_types"]):
dtypes[name] = DUNE_TO_PG[d_type]
if d_type == "varbinary":
varbinary_columns.append(name)
df = pd.DataFrame(result["rows"])
# escape bytes
df = reformat_varbinary_columns(df, varbinary_columns)
return df, dtypes

print(f"Error fetching data: {response.status_code}")
return None
result = (
DuneClient(DUNE_API_KEY, performance=QUERY_ENGINE)
.run_query(query=QueryBase(QUERY_ID), ping_frequency=POLL_FREQUENCY)
.result
)

metadata = result.metadata
dtypes, varbinary_columns = {}, []
for name, d_type in zip(metadata.column_names, metadata.column_types):
dtypes[name] = DUNE_TO_PG[d_type]
if d_type == "varbinary":
varbinary_columns.append(name)
df = pd.DataFrame(result.rows)
# escape bytes
df = reformat_varbinary_columns(df, varbinary_columns)
return df, dtypes


def save_to_postgres(df, dtypes):
Expand Down

0 comments on commit c7ab5dd

Please sign in to comment.