|
| 1 | +import os |
| 2 | +import json |
| 3 | +import requests |
| 4 | + |
| 5 | +YIELD_API_URL = "https://yields.llama.fi/pools" |
| 6 | +TOKEN_API_URL = "https://starknet.api.avnu.fi/v1/starknet/tokens" |
| 7 | + |
| 8 | +DATA_DIR = "./data" |
| 9 | +YIELD_DIR = f"{DATA_DIR}/yields" |
| 10 | + |
| 11 | +os.makedirs(YIELD_DIR, exist_ok=True) |
| 12 | + |
| 13 | +def save_json(data, filepath): |
| 14 | + try: |
| 15 | + with open(filepath, "w") as file: |
| 16 | + json.dump(data, file, indent=4) |
| 17 | + print(f"✅ Saved: {filepath}") |
| 18 | + except IOError as e: |
| 19 | + print(f"❌ Error saving {filepath}: {e}") |
| 20 | + |
| 21 | +def fetch_yield_data(): |
| 22 | + |
| 23 | + try: |
| 24 | + response = requests.get(YIELD_API_URL, timeout=10) |
| 25 | + response.raise_for_status() |
| 26 | + yield_data = response.json().get("data", []) |
| 27 | + chain_data = {} |
| 28 | + for pool in yield_data: |
| 29 | + chain = pool.get("chain", "unknown").lower() |
| 30 | + if chain not in chain_data: |
| 31 | + chain_data[chain] = [] |
| 32 | + chain_data[chain].append(pool) |
| 33 | + |
| 34 | + for chain, pools in chain_data.items(): |
| 35 | + save_json(pools, f"{YIELD_DIR}/{chain}.json") |
| 36 | + |
| 37 | + print(f"✅ Fetched and saved yield data for {len(chain_data)} chains.") |
| 38 | + |
| 39 | + except (requests.exceptions.RequestException, IOError, json.JSONDecodeError) as e: |
| 40 | + print(f"❌ Error fetching yield data: {e}") |
| 41 | + |
| 42 | +def fetch_token_data(): |
| 43 | + try: |
| 44 | + response = requests.get(TOKEN_API_URL, timeout=10) |
| 45 | + response.raise_for_status() |
| 46 | + token_data = response.json().get("content", []) |
| 47 | + |
| 48 | + save_json(token_data, f"{DATA_DIR}/tokens.json") |
| 49 | + print(f"✅ Fetched and saved {len(token_data)} tokens successfully.") |
| 50 | + |
| 51 | + except (requests.exceptions.RequestException, IOError, json.JSONDecodeError) as e: |
| 52 | + print(f"❌ Error fetching token data: {e}") |
| 53 | + |
| 54 | +fetch_yield_data() |
| 55 | +fetch_token_data() |
0 commit comments