-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.py
More file actions
67 lines (58 loc) · 2.16 KB
/
usage.py
File metadata and controls
67 lines (58 loc) · 2.16 KB
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
61
62
63
64
65
66
67
import os
import requests
from dotenv import load_dotenv
from datetime import datetime, timedelta
from typing import Optional, Tuple
load_dotenv()
dune_api_key = os.getenv("DEFI_JOSH_DUNE_QUERY_API_KEY")
url = "https://api.dune.com/api/v1/usage"
headers = {
"X-DUNE-API-KEY": dune_api_key,
"Content-Type": "application/json"
}
def prompt_date(prompt_text: str, default: Optional[str] = None) -> str:
"""Prompt the user for a date in YYYY-MM-DD format. If default is provided and
the user enters nothing, return the default.
"""
while True:
prompt = f"{prompt_text} (YYYY-MM-DD)"
if default:
prompt += f" [{default}]"
prompt += ": "
s = input(prompt).strip()
if not s:
if default:
return default
print("Please enter a date in YYYY-MM-DD format.")
continue
try:
datetime.strptime(s, "%Y-%m-%d")
return s
except ValueError:
print("Invalid date format. Use YYYY-MM-DD.")
def prompt_date_range() -> Tuple[str, str]:
"""Prompt for start and end dates, validate format and that start <= end.
Returns a tuple (start_date, end_date) as YYYY-MM-DD strings.
"""
default_end = datetime.utcnow().strftime("%Y-%m-%d")
default_start = (datetime.utcnow() - timedelta(days=2)).strftime("%Y-%m-%d")
while True:
start = prompt_date("Enter start date", default=default_start)
end = prompt_date("Enter end date", default=default_end)
try:
dt_start = datetime.strptime(start, "%Y-%m-%d")
dt_end = datetime.strptime(end, "%Y-%m-%d")
except ValueError:
# prompt_date already validates format, but keep this guard
print("One of the dates was invalid; please try again.")
continue
if dt_start <= dt_end:
return start, end
print("start_date must be on or before end_date. Please enter the dates again.")
start_date, end_date = prompt_date_range()
data = {
"start_date": start_date,
"end_date": end_date
}
response = requests.post(url, json=data, headers=headers)
print(response.json())