Repository navigation
Expand file tree
/
Copy pathupdate_crypto_prices.py
More file actions
349 lines (290 loc) · 12 KB
/
Copy pathupdate_crypto_prices.py
File metadata and controls
349 lines (290 loc) · 12 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env python3
"""
Direct database updater for crypto prices
Fetches latest prices from CoinMarketCap and CoinGecko
and updates the database directly
"""
import argparse
import logging
import os
import sys
import time
from datetime import datetime
import requests
from dotenv import load_dotenv
# Add project to path
project_root_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(project_root_path)
# Moved imports to top (E402 fix)
from src.database.client import DatabaseClient
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", # Line break (E501 fix)
)
logger = logging.getLogger(__name__)
# CoinMarketCap API Configuration
CMC_API_KEY = os.getenv(
"CMC_API_KEY", "6e1f5ef3-47fc-4288-9b3e-f02a7493be6a"
) # Default to provided key
CMC_BASE_URL = "https://pro-api.coinmarketcap.com/v1"
# CoinGecko API Configuration
COINGECKO_BASE_URL = "https://api.coingecko.com/api/v3"
# Asset mapping between database ID, symbol, and CoinGecko ID
ASSET_MAPPING = [
{"db_id": 1, "symbol": "BTC", "coingecko_id": "bitcoin"},
{"db_id": 2, "symbol": "ETH", "coingecko_id": "ethereum"},
{"db_id": 3, "symbol": "SOL", "coingecko_id": "solana"},
{"db_id": 4, "symbol": "BNB", "coingecko_id": "binancecoin"},
{"db_id": 5, "symbol": "XRP", "coingecko_id": "ripple"},
]
def fetch_coinmarketcap_data(symbol="BTC"):
"""Fetch cryptocurrency data from CoinMarketCap API."""
logger.info(f"Fetching {symbol} data from CoinMarketCap...")
url = f"{CMC_BASE_URL}/cryptocurrency/quotes/latest"
headers = {"X-CMC_PRO_API_KEY": CMC_API_KEY, "Accept": "application/json"}
params = {"symbol": symbol, "convert": "USD"}
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
data = response.json()
if "data" in data and symbol in data["data"]:
coin_data = data["data"][symbol]
quote = coin_data["quote"]["USD"]
result = {
"source": "coinmarketcap",
"timestamp": datetime.now().isoformat(),
"symbol": symbol,
"name": coin_data["name"],
"price_usd": quote["price"],
"market_cap_usd": quote["market_cap"],
"volume_24h_usd": quote["volume_24h"],
"price_change_24h_pct": quote["percent_change_24h"],
"additional_data": {
"circulating_supply": coin_data["circulating_supply"],
"total_supply": coin_data["total_supply"],
"max_supply": coin_data["max_supply"],
"price_change_1h": quote["percent_change_1h"],
"price_change_7d": quote["percent_change_7d"],
"price_change_30d": quote["percent_change_30d"],
},
}
return result
else:
logger.error(f"Unexpected response structure from CMC: {data}")
return None
except requests.exceptions.RequestException as e:
logger.error(f"Error fetching data from CMC: {e}")
if hasattr(e, "response") and hasattr(e.response, "text"):
logger.error(f"Response: {e.response.text}") # Line break (E501 fix)
return None
def fetch_coingecko_data(coin_id="bitcoin"):
"""Fetch cryptocurrency data from CoinGecko API."""
logger.info(f"Fetching {coin_id} data from CoinGecko...")
url = f"{COINGECKO_BASE_URL}/coins/{coin_id}"
params = {
"localization": "false",
"tickers": "false",
"market_data": "true",
"community_data": "false",
"developer_data": "false",
}
try:
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
if "market_data" in data:
market_data = data["market_data"]
result = {
"source": "coingecko",
"timestamp": datetime.now().isoformat(),
"symbol": data["symbol"].upper(),
"name": data["name"],
"price_usd": market_data["current_price"]["usd"],
"market_cap_usd": market_data["market_cap"]["usd"],
"volume_24h_usd": market_data["total_volume"]["usd"],
"price_change_24h_pct": market_data["price_change_percentage_24h"],
"additional_data": {
"circulating_supply": market_data.get("circulating_supply"),
"total_supply": market_data.get("total_supply"),
"max_supply": market_data.get("max_supply"),
"price_change_1h": market_data.get(
"price_change_percentage_1h_in_currency", {}
).get(
"usd"
), # Line break (E501 fix)
"price_change_7d": market_data.get(
"price_change_percentage_7d"
), # Line break (E501 fix)
"price_change_30d": market_data.get(
"price_change_percentage_30d"
), # Line break (E501 fix)
"ath": market_data.get("ath", {}).get("usd"),
"low_24h": market_data.get("low_24h", {}).get("usd"),
"high_24h": market_data.get("high_24h", {}).get("usd"),
},
}
return result
else:
logger.error(f"Unexpected response structure from CG: {data}")
return None
except requests.exceptions.RequestException as e:
logger.error(f"Error fetching data from CG: {e}")
if hasattr(e, "response") and hasattr(e.response, "text"):
logger.error(f"Response: {e.response.text}") # Line break (E501 fix)
return None
def update_database(db_client, asset_id, data, primary_source="coinmarketcap"):
"""
Update the database with the latest cryptocurrency data.
Args:
db_client: DatabaseClient instance
asset_id: Database ID for the asset
data: Dictionary containing data from both CoinMarketCap and CoinGecko
primary_source: Which source to prioritize ('coinmarketcap' or 'coingecko')
"""
if not data.get(primary_source):
logger.warning(f"No {primary_source} data for asset_id={asset_id}")
# Try fallback source
fallback = (
"coingecko" if primary_source == "coinmarketcap" else "coinmarketcap"
) # Line break (E501 fix)
if not data.get(fallback):
logger.error(f"No data from either source for asset_id={asset_id}")
return False
primary_source = fallback
# Get data from the selected source
source_data = data[primary_source]
# Create a record to insert into the database
record = {
"asset_id": asset_id,
"timestamp": source_data["timestamp"],
"price_usd": source_data["price_usd"],
"market_cap_usd": source_data["market_cap_usd"],
"volume_24h_usd": source_data["volume_24h_usd"],
"price_change_24h_pct": source_data["price_change_24h_pct"],
"source": source_data["source"],
"additional_data": source_data["additional_data"],
}
# Insert the record into the database
logger.info(
f"Inserting record for asset_id={asset_id} " f"from source={primary_source}"
)
try:
# Use the database client to insert the record
response = db_client.client.table("market_data").insert(record).execute()
if hasattr(response, "data") and response.data:
logger.info(f"Successfully inserted record for asset_id={asset_id}")
return True
else:
logger.warning(
f"Insertion for asset_id={asset_id} returned no data"
) # Line break (E501 fix)
return False
except Exception as e:
logger.error(f"Failed to insert record for asset_id={asset_id}: {e}")
return False
def fetch_and_update_asset(db_client, asset, primary_source="coinmarketcap"):
"""Fetch data for a single asset and update the database."""
symbol = asset["symbol"]
coingecko_id = asset["coingecko_id"]
asset_id = asset["db_id"]
# Fetch data from both sources
data = {}
# Try CoinMarketCap
try:
data["coinmarketcap"] = fetch_coinmarketcap_data(symbol)
except Exception as e:
logger.error(f"Failed to fetch {symbol} from CoinMarketCap: {e}")
# Add delay to avoid rate limits
time.sleep(1)
# Try CoinGecko
try:
data["coingecko"] = fetch_coingecko_data(coingecko_id)
except Exception as e:
logger.error(f"Failed to fetch {coingecko_id} from CoinGecko: {e}")
# Update database with the data
success = update_database(db_client, asset_id, data, primary_source)
# Print a comparison if both sources were successful
if data.get("coinmarketcap") and data.get("coingecko"):
cmc_price = data["coinmarketcap"]["price_usd"]
cg_price = data["coingecko"]["price_usd"]
diff = ((cg_price - cmc_price) / cmc_price) * 100 if cmc_price else 0
logger.info(
f"{symbol} Price Comparison: CMC=${cmc_price:.2f}, "
f"CG=${cg_price:.2f} (Diff: {diff:.2f}%)"
)
return success
def update_all_assets(primary_source="coinmarketcap"):
"""Update all assets in the database with latest prices."""
# Initialize database client
logger.info("Initializing database client...")
db_client = DatabaseClient()
if not db_client.client:
logger.error("Failed to initialize database client!")
return False
logger.info("Database client initialized successfully.")
# Get list of assets from the database to ensure we only update active ones
try:
db_assets = db_client.get_assets()
if not db_assets:
logger.warning("No assets found in the database!")
return False
# Create a mapping of assets to update
assets_to_update = []
for mapping in ASSET_MAPPING:
for db_asset in db_assets:
if db_asset["symbol"] == mapping["symbol"]:
assets_to_update.append(
{
"db_id": db_asset["id"],
"symbol": mapping["symbol"],
"coingecko_id": mapping["coingecko_id"],
}
)
break
if not assets_to_update:
logger.warning("No matching assets to update!")
return False
# Update each asset
success_count = 0
for asset in assets_to_update:
logger.info(f"Processing {asset['symbol']}...")
if fetch_and_update_asset(db_client, asset, primary_source):
success_count += 1
# Add delay to avoid rate limits
time.sleep(2)
logger.info(
f"Successfully updated {success_count}/{len(assets_to_update)} assets."
)
return success_count > 0
except Exception as e:
logger.error(f"Error updating assets: {e}")
return False
def main():
"""Run the price updater."""
parser = argparse.ArgumentParser(description="Update crypto prices in the database")
parser.add_argument(
"--source",
choices=["coinmarketcap", "coingecko"],
default="coinmarketcap",
help="Primary data source",
)
parser.add_argument("--debug", action="store_true", help="Enable debug logging")
args = parser.parse_args()
# Set logging level
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
logger.info(
f"Starting crypto price updater using {args.source} as " f"primary source..."
)
success = update_all_assets(primary_source=args.source)
if success:
logger.info("Price update completed successfully.")
return 0
else:
logger.error("Price update failed!")
return 1
if __name__ == "__main__":
sys.exit(main())