3
3
Run this directly to see the output in your terminal.
4
4
5
5
Usage:
6
- python test_coingecko.py --symbol BTC --coin-id bitcoin --num-tokens 10
7
- python test_coingecko.py -s ETH -c ethereum -n 5
6
+ python test_coingecko.py --symbol BTC --coin-id bitcoin --num-tokens 10 --days 30
7
+ python test_coingecko.py -s ETH -c ethereum -n 5 -d 7
8
8
"""
9
9
10
- import asyncio
11
- import aiohttp
12
10
import argparse
13
11
import sys
14
12
from pathlib import Path
@@ -31,65 +29,67 @@ def parse_arguments():
31
29
help = 'CoinGecko coin ID (e.g., bitcoin, ethereum)' )
32
30
parser .add_argument ('-n' , '--num-tokens' , type = int , default = 10 ,
33
31
help = 'Number of top tokens to fetch (default: 10, max: 250)' )
32
+ parser .add_argument ('-d' , '--days' , type = int , default = 30 ,
33
+ help = 'Number of days for historical volume data (default: 30)' )
34
34
return parser .parse_args ()
35
35
36
- async def run_tests (symbol : str , coin_id : str , num_tokens : int ):
36
+ def run_tests (target_symbol : str , target_coin_id : str , num_tokens : int , days : int ):
37
37
"""Run tests for CoinGecko API functions."""
38
38
print ("\n === Starting CoinGecko API Tests ===\n " )
39
39
40
- async with aiohttp .ClientSession () as session :
41
- print (f"Fetching data for { num_tokens } top tokens by market cap..." )
42
- market_data = await fetch_all_coingecko_market_data (session , num_tokens )
40
+ print (f"Fetching data for { num_tokens } top tokens by market cap..." )
41
+ market_data = fetch_all_coingecko_market_data (num_tokens )
42
+
43
+ if not market_data :
44
+ print ("Error: No market data received" )
45
+ return
46
+
47
+ total_tokens = len (market_data )
48
+ print (f"\n Received data for { total_tokens } tokens" )
49
+ if total_tokens < num_tokens :
50
+ print (f"Note: Requested { num_tokens } tokens but only received { total_tokens } ." )
51
+ print ("This is normal if you requested more tokens than are available on CoinGecko." )
52
+
53
+ print ("\n Top 10 tokens by market cap (or all if less than 10):" )
54
+ tokens_to_show = sorted (
55
+ market_data .items (),
56
+ key = lambda x : x [1 ].get ('market_cap_rank' , float ('inf' ))
57
+ )[:min (10 , total_tokens )]
58
+
59
+ for current_coin_id , data in tokens_to_show :
60
+ symbol = data .get ('symbol' , '???' )
61
+ rank = data .get ('market_cap_rank' , 'N/A' )
62
+ price = data .get ('current_price' , 'N/A' )
63
+ print (f"#{ rank } : { symbol } @ ${ price } " )
64
+
65
+ # Test specific coin data if provided
66
+ if target_coin_id and target_symbol :
67
+ print (f"\n Detailed data for { target_symbol } :" )
68
+ coin_data = market_data .get (target_coin_id )
43
69
44
- if not market_data :
45
- print ("Error: No market data received " )
70
+ if not coin_data :
71
+ print (f "Error: Could not find data for { target_symbol } ( { target_coin_id } ) " )
46
72
return
47
73
48
- total_tokens = len (market_data )
49
- print (f"\n Received data for { total_tokens } tokens" )
50
- if total_tokens < num_tokens :
51
- print (f"Note: Requested { num_tokens } tokens but only received { total_tokens } ." )
52
- print ("This is normal if you requested more tokens than are available on CoinGecko." )
53
-
54
- print ("\n Top 10 tokens by market cap (or all if less than 10):" )
55
- tokens_to_show = sorted (
56
- market_data .items (),
57
- key = lambda x : x [1 ].get ('market_cap_rank' , float ('inf' ))
58
- )[:min (10 , total_tokens )]
59
-
60
- for coin_id , data in tokens_to_show :
61
- symbol = data .get ('symbol' , '???' )
62
- rank = data .get ('market_cap_rank' , 'N/A' )
63
- price = data .get ('current_price' , 'N/A' )
64
- print (f"#{ rank } : { symbol } @ ${ price } " )
74
+ # Display detailed coin information
75
+ print (f"Name: { coin_data .get ('name' )} " )
76
+ print (f"Current Price: ${ coin_data .get ('current_price' )} " )
77
+ print (f"Market Cap Rank: #{ coin_data .get ('market_cap_rank' )} " )
78
+ print (f"Market Cap: ${ coin_data .get ('market_cap' ):,} " )
79
+ print (f"Circulating Supply: { coin_data .get ('circulating_supply' ):,.2f} { target_symbol } " )
80
+ print (f"All-Time High: ${ coin_data .get ('ath_price' )} " )
65
81
66
- # Test specific coin data if provided
67
- if coin_id and symbol :
68
- print (f"\n Detailed data for { symbol } :" )
69
- coin_data = market_data .get (coin_id )
70
-
71
- if not coin_data :
72
- print (f"Error: Could not find data for { symbol } ({ coin_id } )" )
73
- return
74
-
75
- # Display detailed coin information
76
- print (f"Name: { coin_data .get ('name' )} " )
77
- print (f"Current Price: ${ coin_data .get ('current_price' )} " )
78
- print (f"Market Cap Rank: #{ coin_data .get ('market_cap_rank' )} " )
79
- print (f"Market Cap: ${ coin_data .get ('market_cap' ):,} " )
80
- print (f"Circulating Supply: { coin_data .get ('circulating_supply' ):,.2f} { symbol } " )
81
- print (f"All-Time High: ${ coin_data .get ('ath_price' )} " )
82
-
83
- print ("\n Fetching historical volume data..." )
84
- volume_data = await fetch_coingecko_historical_volume (coin_id )
85
- if volume_data :
86
- print (f"30-day Volume: ${ volume_data :,.2f} " )
87
- else :
88
- print ("Error: Could not fetch volume data" )
82
+ print (f"\n Fetching { days } -day historical volume data..." )
83
+ volume_data = fetch_coingecko_historical_volume (target_coin_id , number_of_days = days )
84
+ if volume_data :
85
+ print (f"{ days } -day Volume: ${ volume_data :,.2f} " )
86
+ else :
87
+ print ("Error: Could not fetch volume data" )
89
88
90
89
if __name__ == "__main__" :
91
90
args = parse_arguments ()
92
91
print (f"Starting CoinGecko API tests for { args .symbol } ({ args .coin_id } )..." )
93
92
print (f"Will fetch top { args .num_tokens } tokens by market cap..." )
94
- asyncio .run (run_tests (args .symbol , args .coin_id , args .num_tokens ))
93
+ print (f"Historical volume period: { args .days } days" )
94
+ run_tests (args .symbol , args .coin_id , args .num_tokens , args .days )
95
95
print ("\n Tests completed!" )
0 commit comments