Spaces:
Sleeping
Sleeping
File size: 1,430 Bytes
88be3ab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import requests
import csv
import time
from dotenv import load_dotenv
import os
load_dotenv()
API_KEY = os.getenv("DUNE_API_KEY")
QUERY_ID = "6246979"
LIMIT = 1000
offset = 0
output_file = "wallet_dataset.csv"
first_page = True
print("Downloading full dataset from Dune…")
while True:
url = f"https://api.dune.com/api/v1/query/{QUERY_ID}/results/csv?limit={LIMIT}&offset={offset}"
headers = {"X-Dune-API-Key": API_KEY}
print(f"Fetching offset {offset} ...")
try:
r = requests.get(url, headers=headers, timeout=40)
except requests.exceptions.Timeout:
print("Timeout. Retrying in 5 seconds…")
time.sleep(5)
continue
except Exception as e:
print(f"Network error: {e}. Retrying…")
time.sleep(5)
continue
if len(r.text.strip()) == 0 or r.status_code != 200:
print("No more rows or bad response. Finished.")
break
lines = r.text.splitlines()
if len(lines) <= 1:
print("Reached the last partial page. Stopping.")
break
mode = 'w' if first_page else 'a'
with open(output_file, mode, newline='') as f:
writer = csv.writer(f)
for i, line in enumerate(lines):
if not first_page and i == 0:
continue
writer.writerow(line.split(','))
first_page = False
offset += LIMIT
time.sleep(0.3)
print(f"Saved CSV to: {output_file}")
|