WIP: Fix Aggregazione market Product #67
@@ -13,38 +13,27 @@ logging = logging.getLogger("crypto_symbols")
|
|||||||
|
|
||||||
|
|
|||||||
BASE_URL = "https://finance.yahoo.com/markets/crypto/all/"
|
BASE_URL = "https://finance.yahoo.com/markets/crypto/all/"
|
||||||
|
|
||||||
|
|
||||||
class CryptoSymbolsTools(Toolkit):
|
class CryptoSymbolsTools(Toolkit):
|
||||||
"""
|
"""
|
||||||
Class for obtaining cryptocurrency symbols via Yahoo Finance.
|
Class for obtaining cryptocurrency symbols via Yahoo Finance.
|
||||||
(This class-level docstring is for developers).
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, cache_file: str = 'resources/cryptos.csv'):
|
def __init__(self, cache_file: str = 'resources/cryptos.csv'):
|
||||||
self.cache_file = cache_file
|
self.cache_file = cache_file
|
||||||
try:
|
self.final_table = pd.read_csv(self.cache_file) if os.path.exists(self.cache_file) else pd.DataFrame() # type: ignore
|
||||||
self.final_table = pd.read_csv(self.cache_file) if os.path.exists(self.cache_file) else pd.DataFrame(
|
|
||||||
columns=['Symbol', 'Name'])
|
|
||||||
except Exception:
|
|
||||||
self.final_table = pd.DataFrame(columns=['Symbol', 'Name'])
|
|
||||||
|
|
||||||
Toolkit.__init__(self, # type: ignore
|
Toolkit.__init__(self, # type: ignore
|
||||||
name="Crypto Symbols Tool",
|
name="Crypto Symbols Tool",
|
||||||
instructions="A utility tool to find and verify the correct cryptocurrency symbols (tickers). " \
|
instructions="Tool to get cryptocurrency symbols and search them by name.",
|
||||||
"Use this to translate a cryptocurrency name (e.g., 'Bitcoin') into its official symbol " \
|
tools=[
|
||||||
"(e.g., 'BTC-USD') *before* delegating tasks to the MarketAgent.",
|
self.get_all_symbols,
|
||||||
tools=[
|
self.get_symbols_by_name,
|
||||||
self.get_all_symbols,
|
],
|
||||||
self.get_symbols_by_name,
|
)
|
||||||
],
|
|
||||||
)
|
|
||||||
|
|
||||||
def get_all_symbols(self) -> list[str]:
|
def get_all_symbols(self) -> list[str]:
|
||||||
"""
|
"""
|
||||||
Returns a complete list of all available cryptocurrency symbols (tickers).
|
Returns a complete list of all available cryptocurrency symbols (tickers).
|
||||||
|
The list could be very long, prefer using 'get_symbols_by_name' for specific searches.
|
||||||
Warning: This list can be very long. Prefer 'get_symbols_by_name'
|
|
||||||
if you are searching for a specific asset.
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list[str]: A comprehensive list of all supported crypto symbols (e.g., "BTC-USD", "ETH-USD").
|
list[str]: A comprehensive list of all supported crypto symbols (e.g., "BTC-USD", "ETH-USD").
|
||||||
@@ -54,32 +43,19 @@ class CryptoSymbolsTools(Toolkit):
|
|||||||
def get_symbols_by_name(self, query: str) -> list[tuple[str, str]]:
|
def get_symbols_by_name(self, query: str) -> list[tuple[str, str]]:
|
||||||
"""
|
"""
|
||||||
Searches the cryptocurrency database for assets matching a name or symbol.
|
Searches the cryptocurrency database for assets matching a name or symbol.
|
||||||
|
|
||||||
Use this to find the exact, correct symbol for a cryptocurrency name.
|
Use this to find the exact, correct symbol for a cryptocurrency name.
|
||||||
(e.g., query="Bitcoin" might return [("BTC-USD", "Bitcoin USD")]).
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
query (str): The name, partial name, or symbol to search for (e.g., "Bitcoin", "ETH").
|
query (str): The name, partial name, or symbol to search for (e.g., "Bitcoin", "ETH").
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list[tuple[str, str]]: A list of tuples, where each tuple contains
|
list[tuple[str, str]]: A list of tuples, where each tuple contains
|
||||||
the (symbol, full_name) of a matching asset.
|
the (symbol, full_name) of a matching asset.
|
||||||
Returns an empty list if no matches are found.
|
Returns an empty list if no matches are found.
|
||||||
"""
|
"""
|
||||||
if self.final_table.empty or 'Name' not in self.final_table.columns or 'Symbol' not in self.final_table.columns:
|
query_lower = query.lower()
|
||||||
return []
|
positions = self.final_table['Name'].str.lower().str.contains(query_lower) | \
|
||||||
|
self.final_table['Symbol'].str.lower().str.contains(query_lower)
|
||||||
try:
|
filtered_df = self.final_table[positions]
|
||||||
# Cerca sia nel nome che nel simbolo, ignorando maiuscole/minuscole
|
return list(zip(filtered_df['Symbol'], filtered_df['Name']))
|
||||||
mask = self.final_table['Name'].str.contains(query, case=False, na=False) | \
|
|
||||||
self.final_table['Symbol'].str.contains(query, case=False, na=False)
|
|
||||||
|
|
||||||
filtered_df = self.final_table[mask]
|
|
||||||
|
|
||||||
# Converte il risultato in una lista di tuple
|
|
||||||
return list(zip(filtered_df['Symbol'], filtered_df['Name']))
|
|
||||||
except Exception:
|
|
||||||
return []
|
|
||||||
|
|
||||||
async def fetch_crypto_symbols(self, force_refresh: bool = False) -> None:
|
async def fetch_crypto_symbols(self, force_refresh: bool = False) -> None:
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user
Italian comment in English codebase. Should be translated to 'Search both in name and symbol, ignoring case'.
Italian comment in English codebase. Should be translated to 'Convert the result to a list of tuples'.
Non mi ricordo, non ho memoria di quella cosa
Non mi ricordo, non ho memoria di quella cosa