-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
109 lines (91 loc) · 3.27 KB
/
api.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Headers } from "node-fetch";
import fetch from "node-fetch";
import fs from "fs";
// Get the query ID from the command line arguments
const queryID = process.argv[2]; // Example Query ID: 1258228
console.log("retrieving data for query " + queryID + "..."); // Check that Query ID is passing through
// Add the API key to header object
const meta = {
"x-dune-api-key": "[INSERT_YOUR_API_KEY]",
};
const header = new Headers(meta);
// Delay function
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Async function to fetch query data and loop until completed
async function fetchQueryData(queryID) {
try {
// Execute the query and get the execution_id
const executionResponse = await fetch(
`https://api.dune.com/api/v1/query/${queryID}/execute`,
{
method: "POST",
headers: header,
}
);
// Check if the execution response is not ok and throw an error
if (!executionResponse.ok) {
throw new Error(`Error executing query: ${executionResponse.statusText}`);
}
const executionData = await executionResponse.json();
const executionID = executionData.execution_id;
let isCompleted = false;
let attempts = 0;
const timeout = Date.now() + 10 * 60 * 1000; // 5 minutes timeout
// Loop until the query is completed or the maximum number of attempts is reached
while (!isCompleted && Date.now() < timeout) {
// Check the query execution status using the execution_id
const statusResponse = await fetch(
`https://api.dune.com/api/v1/execution/${executionID}/status`,
{
method: "GET",
headers: header,
}
);
// Check if the status response is not ok and throw an error
if (!statusResponse.ok) {
throw new Error(`Error fetching status: ${statusResponse.statusText}`);
}
const statusData = await statusResponse.json();
console.log(statusData);
// Check if the query state is completed
if (statusData.state === "QUERY_STATE_COMPLETED") {
isCompleted = true;
} else {
attempts++;
await delay(5000); // Wait for 5 seconds before trying again
}
}
// Throw an error if the query is not completed within the timeout
if (!isCompleted) {
throw new Error("Unable to fetch query results after multiple attempts");
}
// Fetch the query results in CSV format
const resultsResponse = await fetch(
`https://api.dune.com/api/v1/execution/${executionID}/results/csv`,
{
method: "GET",
headers: header,
}
);
// Check if the results response is not ok and throw an error
if (!resultsResponse.ok) {
throw new Error(`Error fetching results: ${resultsResponse.statusText}`);
}
const resultsData = await resultsResponse.text();
// Save the CSV data to a file
const fileName = `results_${queryID}.csv`;
fs.writeFile(fileName, resultsData, (err) => {
if (err) {
console.error(`Error writing the CSV file: ${fileName}`, err);
} else {
console.log(`CSV file has been saved as ${fileName}`);
}
});
} catch (error) {
// Log the error message if an error occurs
console.error("Error:", error.message);
}
}
fetchQueryData(queryID);