Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -28,16 +28,15 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's important to specify the ret
|
|
28 |
return "What magic will you build ?"
|
29 |
|
30 |
@tool
|
31 |
-
def fetch_active_crypto(currency: str = 'USD') -> Optional[Dict[str, Any]]:
|
32 |
"""A tool that fetches and reverse sorts by market_cap all active crypto in currency.
|
33 |
Args:
|
34 |
currency: A string representing the currency the value is returned in (default: 'USD').
|
35 |
-
|
36 |
Returns:
|
37 |
-
Optional[Dict[str, Any]]: A
|
38 |
-
|
39 |
"""
|
40 |
-
# url = 'https://sandbox-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
|
41 |
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
|
42 |
parameters = {
|
43 |
'start': '1',
|
@@ -47,7 +46,6 @@ def fetch_active_crypto(currency: str = 'USD') -> Optional[Dict[str, Any]]:
|
|
47 |
headers = {
|
48 |
'Accepts': 'application/json',
|
49 |
'X-CMC_PRO_API_KEY': 'e375c697-e504-464e-b800-2b8cf9c67765',
|
50 |
-
# 'X-CMC_PRO_API_KEY': 'b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c',
|
51 |
}
|
52 |
|
53 |
session = requests.Session()
|
@@ -58,10 +56,20 @@ def fetch_active_crypto(currency: str = 'USD') -> Optional[Dict[str, Any]]:
|
|
58 |
response.raise_for_status() # Raise an exception for HTTP errors
|
59 |
data = json.loads(response.text)
|
60 |
|
61 |
-
# Extract
|
62 |
if 'data' in data:
|
63 |
sorted_crypto = sorted(data['data'], key=lambda x: x['quote'][currency]['market_cap'], reverse=True)
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
else:
|
66 |
print("No data found in the response.")
|
67 |
return None
|
|
|
28 |
return "What magic will you build ?"
|
29 |
|
30 |
@tool
|
31 |
+
def fetch_active_crypto(currency: str = 'USD', chunk_size: int = 100) -> Optional[List[Dict[str, Any]]]:
|
32 |
"""A tool that fetches and reverse sorts by market_cap all active crypto in currency.
|
33 |
Args:
|
34 |
currency: A string representing the currency the value is returned in (default: 'USD').
|
35 |
+
chunk_size: The number of cryptocurrencies to process in each chunk (default: 100).
|
36 |
Returns:
|
37 |
+
Optional[List[Dict[str, Any]]]: A list of dictionaries containing the top cryptocurrencies by market cap,
|
38 |
+
chunked into smaller pieces, or None if an error occurs.
|
39 |
"""
|
|
|
40 |
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
|
41 |
parameters = {
|
42 |
'start': '1',
|
|
|
46 |
headers = {
|
47 |
'Accepts': 'application/json',
|
48 |
'X-CMC_PRO_API_KEY': 'e375c697-e504-464e-b800-2b8cf9c67765',
|
|
|
49 |
}
|
50 |
|
51 |
session = requests.Session()
|
|
|
56 |
response.raise_for_status() # Raise an exception for HTTP errors
|
57 |
data = json.loads(response.text)
|
58 |
|
59 |
+
# Extract and sort cryptocurrencies by market cap
|
60 |
if 'data' in data:
|
61 |
sorted_crypto = sorted(data['data'], key=lambda x: x['quote'][currency]['market_cap'], reverse=True)
|
62 |
+
|
63 |
+
# Chunk the sorted data into smaller pieces
|
64 |
+
chunks = [sorted_crypto[i:i + chunk_size] for i in range(0, len(sorted_crypto), chunk_size)]
|
65 |
+
|
66 |
+
# Convert each chunk into a dictionary
|
67 |
+
result = []
|
68 |
+
for chunk in chunks:
|
69 |
+
chunk_dict = {crypto['name']: crypto['quote'][currency] for crypto in chunk}
|
70 |
+
result.append(chunk_dict)
|
71 |
+
|
72 |
+
return result
|
73 |
else:
|
74 |
print("No data found in the response.")
|
75 |
return None
|