|
| 1 | +import pandas as pd |
| 2 | +import cloudscraper |
| 3 | +from urllib.parse import urlparse |
| 4 | + |
| 5 | +def _download(url: str) -> pd.DataFrame: |
| 6 | + # Parse the query ID from URL |
| 7 | + parsed = urlparse(url) |
| 8 | + parts = [part for part in parsed.path.split("/") if part] |
| 9 | + if len(parts) < 2 or parts[0] != "queries": |
| 10 | + raise ValueError("URL is not a valid Dune query URL. Follow the guide at: https://chaindl.readthedocs.io/#dune-dune-com") |
| 11 | + query_id = int(parts[1]) |
| 12 | + |
| 13 | + scraper = cloudscraper.create_scraper() |
| 14 | + |
| 15 | + # Get latest result set ID |
| 16 | + graphql_url = "https://dune.com/public/graphql" |
| 17 | + payload = { |
| 18 | + "operationName": "GetLatestResultSetIds", |
| 19 | + "variables": {"queryId": query_id, "parameters": [], "canRefresh": True}, |
| 20 | + "query": """ |
| 21 | + query GetLatestResultSetIds($canRefresh: Boolean!, $queryId: Int!, $parameters: [ExecutionParameterInput!]) { |
| 22 | + resultSetForQuery(canRefresh: $canRefresh, queryId: $queryId, parameters: $parameters) { |
| 23 | + completedExecutionId |
| 24 | + failedExecutionId |
| 25 | + pendingExecutionId |
| 26 | + __typename |
| 27 | + } |
| 28 | + } |
| 29 | + """ |
| 30 | + } |
| 31 | + |
| 32 | + response = scraper.post(graphql_url, json=payload) |
| 33 | + if response.status_code != 200: |
| 34 | + raise ConnectionError(f"Failed to get result set ID: {response.status_code}") |
| 35 | + json_data = response.json() |
| 36 | + execution_id = json_data["data"]["resultSetForQuery"]["completedExecutionId"] |
| 37 | + if execution_id is None: |
| 38 | + raise ValueError("No completed execution found for this query.") |
| 39 | + |
| 40 | + # Fetch all execution data with pagination |
| 41 | + all_data = [] |
| 42 | + offset = 0 |
| 43 | + limit = 9999999 |
| 44 | + |
| 45 | + execution_url = "https://core-api.dune.com/public/execution" |
| 46 | + headers = { |
| 47 | + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:142.0) Gecko/20100101 Firefox/142.0", |
| 48 | + "Accept": "*/*", |
| 49 | + "Accept-Language": "en-US,en;q=0.5", |
| 50 | + "Content-Type": "application/json", |
| 51 | + "Origin": "https://dune.com", |
| 52 | + "Referer": "https://dune.com/", |
| 53 | + "Connection": "keep-alive", |
| 54 | + "Pragma": "no-cache", |
| 55 | + "Cache-Control": "no-cache", |
| 56 | + } |
| 57 | + |
| 58 | + while True: |
| 59 | + payload = { |
| 60 | + "execution_id": execution_id, |
| 61 | + "query_id": query_id, |
| 62 | + "parameters": [], |
| 63 | + "pagination": {"limit": limit, "offset": offset} |
| 64 | + } |
| 65 | + response = scraper.post(execution_url, headers=headers, json=payload) |
| 66 | + if response.status_code != 200: |
| 67 | + raise ConnectionError(f"Failed to fetch execution data: {response.status_code}") |
| 68 | + json_data = response.json() |
| 69 | + execution_result = json_data.get('execution_succeeded') |
| 70 | + if not execution_result: |
| 71 | + raise ValueError("Execution failed or no data returned.") |
| 72 | + |
| 73 | + data = execution_result['data'] |
| 74 | + total_row_count = execution_result['total_row_count'] |
| 75 | + all_data.extend(data) |
| 76 | + |
| 77 | + if len(all_data) >= total_row_count: |
| 78 | + break |
| 79 | + offset = len(all_data) |
| 80 | + |
| 81 | + # Convert to DataFrame |
| 82 | + df = pd.DataFrame(all_data) |
| 83 | + return df |
0 commit comments