From c82f10b32c712681f23c69ec1f0798d8d819cc42 Mon Sep 17 00:00:00 2001 From: trojanhorse47 Date: Mon, 29 Sep 2025 12:22:02 +0200 Subject: [PATCH 01/22] Creazione branch tool, refactor degli import e soppressione dei warning --- pyproject.toml | 2 +- src/app/agents/market.py | 2 +- src/app/agents/predictor.py | 4 ++-- src/app/markets/__init__.py | 14 +++++++----- src/app/markets/coinbase.py | 2 +- src/app/markets/cryptocompare.py | 4 ++-- src/app/models.py | 36 +++++++++++++++++++++--------- src/app/tool.py | 27 ++++++++++++++-------- src/app/utils/market_aggregator.py | 5 +++-- tests/agents/test_market.py | 10 ++++----- tests/agents/test_predictor.py | 12 +++++----- 11 files changed, 72 insertions(+), 46 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 35a3b6e..9822cbd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,7 +16,7 @@ dependencies = [ "dotenv", # 🟡 per fare scraping di pagine web #"bs4", - # ✅ per fare una UI web semplice con input e output + # ✅ per fare una UI web semplice con user_input e output "gradio", # ✅ per costruire agenti (ovvero modelli che possono fare più cose tramite tool) https://github.com/agno-agi/agno diff --git a/src/app/agents/market.py b/src/app/agents/market.py index affa466..a561cb6 100644 --- a/src/app/agents/market.py +++ b/src/app/agents/market.py @@ -1,5 +1,5 @@ from agno.tools import Toolkit -from app.markets import MarketAPIs +from src.app.markets import MarketAPIs # TODO (?) in futuro fare in modo che la LLM faccia da sé per il mercato # Non so se può essere utile, per ora lo lascio qui diff --git a/src/app/agents/predictor.py b/src/app/agents/predictor.py index e811846..09d7a26 100644 --- a/src/app/agents/predictor.py +++ b/src/app/agents/predictor.py @@ -1,5 +1,5 @@ from enum import Enum -from app.markets.base import ProductInfo +from src.app.markets.base import ProductInfo from pydantic import BaseModel, Field class PredictorStyle(Enum): @@ -23,7 +23,7 @@ class PredictorOutput(BaseModel): PREDICTOR_INSTRUCTIONS = """ You are an **Allocation Algorithm (Crypto-Algo)** specialized in analyzing market data and sentiment to generate an investment strategy and a target portfolio. -Your sole objective is to process the input data and generate the strictly structured output as required by the response format. **You MUST NOT provide introductions, preambles, explanations, conclusions, or any additional comments that are not strictly required.** +Your sole objective is to process the user_input data and generate the strictly structured output as required by the response format. **You MUST NOT provide introductions, preambles, explanations, conclusions, or any additional comments that are not strictly required.** ## Processing Instructions (Absolute Rule) diff --git a/src/app/markets/__init__.py b/src/app/markets/__init__.py index 4bb3e9e..cb27b23 100644 --- a/src/app/markets/__init__.py +++ b/src/app/markets/__init__.py @@ -1,6 +1,6 @@ -from app.markets.base import BaseWrapper -from app.markets.coinbase import CoinBaseWrapper -from app.markets.cryptocompare import CryptoCompareWrapper +from src.app.markets.base import BaseWrapper +from src.app.markets.coinbase import CoinBaseWrapper +from src.app.markets.cryptocompare import CryptoCompareWrapper from agno.utils.log import log_warning @@ -30,8 +30,8 @@ class MarketAPIs(BaseWrapper): for wrapper in wrapper_builders: try: result.append(wrapper(currency=currency)) - except Exception as _: - log_warning(f"{wrapper} cannot be initialized, maybe missing API key?") + except Exception as e: + log_warning(f"{wrapper} cannot be initialized: {e}") assert result, "No market API keys set in environment variables." return result @@ -39,7 +39,9 @@ class MarketAPIs(BaseWrapper): def __init__(self, currency: str = "USD"): """ Inizializza la classe con la valuta di riferimento e la priorità dei provider. - :param currency: Valuta di riferimento (default "USD") + + Args: + currency: Valuta di riferimento (default "USD") """ self.currency = currency self.wrappers = MarketAPIs.get_list_available_market_apis(currency=currency) diff --git a/src/app/markets/coinbase.py b/src/app/markets/coinbase.py index aac556d..2e0e779 100644 --- a/src/app/markets/coinbase.py +++ b/src/app/markets/coinbase.py @@ -1,6 +1,6 @@ import os from coinbase.rest import RESTClient -from app.markets.base import ProductInfo, BaseWrapper, Price +from src.app.markets.base import ProductInfo, BaseWrapper, Price class CoinBaseWrapper(BaseWrapper): """ diff --git a/src/app/markets/cryptocompare.py b/src/app/markets/cryptocompare.py index 188a2c2..a6e50a0 100644 --- a/src/app/markets/cryptocompare.py +++ b/src/app/markets/cryptocompare.py @@ -1,6 +1,6 @@ import os import requests -from app.markets.base import ProductInfo, BaseWrapper, Price +from src.app.markets.base import ProductInfo, BaseWrapper, Price BASE_URL = "https://min-api.cryptocompare.com" @@ -8,7 +8,7 @@ class CryptoCompareWrapper(BaseWrapper): """ Wrapper per le API pubbliche di CryptoCompare. La documentazione delle API è disponibile qui: https://developers.coindesk.com/documentation/legacy/Price/SingleSymbolPriceEndpoint - !!ATTENZIONE!! sembra essere una API legacy e potrebbe essere deprecata in futuro. + !ATTENZIONE! Sembra essere una API legacy e potrebbe essere deprecata in futuro. """ def __init__(self, api_key:str = None, currency:str='USD'): if api_key is None: diff --git a/src/app/models.py b/src/app/models.py index 12aae9c..94f7da2 100644 --- a/src/app/models.py +++ b/src/app/models.py @@ -41,6 +41,7 @@ class AppModels(Enum): availables.append(AppModels.OLLAMA_QWEN) return availables + @staticmethod def availables_online() -> list['AppModels']: """ Controlla quali provider di modelli LLM online hanno le loro API keys disponibili @@ -49,9 +50,7 @@ class AppModels(Enum): if not os.getenv("GOOGLE_API_KEY"): log_warning("No GOOGLE_API_KEY set in environment variables.") return [] - availables = [] - availables.append(AppModels.GEMINI) - availables.append(AppModels.GEMINI_PRO) + availables = [AppModels.GEMINI, AppModels.GEMINI_PRO] return availables @staticmethod @@ -75,9 +74,13 @@ class AppModels(Enum): def extract_json_str_from_response(response: str) -> str: """ Estrae il JSON dalla risposta del modello. - response: risposta del modello (stringa). - Ritorna la parte JSON della risposta come stringa. - Se non viene trovato nessun JSON, ritorna una stringa vuota. + Args: + response: risposta del modello (stringa). + + Returns: + La parte JSON della risposta come stringa. + Se non viene trovato nessun JSON, ritorna una stringa vuota. + ATTENZIONE: questa funzione è molto semplice e potrebbe non funzionare in tutti i casi. Si assume che il JSON sia ben formato e che inizi con '{' e finisca con '}'. Quindi anche solo un json array farà fallire questa funzione. @@ -98,9 +101,15 @@ class AppModels(Enum): def get_model(self, instructions:str) -> Model: """ Restituisce un'istanza del modello specificato. - instructions: istruzioni da passare al modello (system prompt). - Ritorna un'istanza di BaseModel o una sua sottoclasse. - Raise ValueError se il modello non è supportato. + + Args: + instructions: istruzioni da passare al modello (system prompt). + + Returns: + Un'istanza di BaseModel o una sua sottoclasse. + + Raise: + ValueError se il modello non è supportato. """ name = self.value if self in {AppModels.GEMINI, AppModels.GEMINI_PRO}: @@ -113,8 +122,13 @@ class AppModels(Enum): def get_agent(self, instructions: str, name: str = "", output: BaseModel | None = None) -> Agent: """ Costruisce un agente con il modello e le istruzioni specificate. - instructions: istruzioni da passare al modello (system prompt). - Ritorna un'istanza di Agent. + Args: + instructions: istruzioni da passare al modello (system prompt) + name: nome dell'agente (opzionale) + output: schema di output opzionale (Pydantic BaseModel) + + Returns: + Un'istanza di Agent. """ return Agent( model=self.get_model(instructions), diff --git a/src/app/tool.py b/src/app/tool.py index d0b3ca0..f1ca560 100644 --- a/src/app/tool.py +++ b/src/app/tool.py @@ -1,8 +1,8 @@ -from app.agents.news_agent import NewsAgent -from app.agents.social_agent import SocialAgent -from app.agents.predictor import PredictorStyle, PredictorInput, PredictorOutput, PREDICTOR_INSTRUCTIONS -from app.markets import MarketAPIs -from app.models import AppModels +from src.app.agents.news_agent import NewsAgent +from src.app.agents.social_agent import SocialAgent +from src.app.agents.predictor import PredictorStyle, PredictorInput, PredictorOutput, PREDICTOR_INSTRUCTIONS +from src.app.markets import MarketAPIs +from src.app.models import AppModels from agno.utils.log import log_info class ToolAgent: @@ -14,6 +14,10 @@ class ToolAgent: """ Inizializza l'agente con i modelli disponibili, gli stili e l'API di mercato. """ + self.social_agent = None + self.news_agent = None + self.predictor = None + self.chosen_model = None self.available_models = AppModels.availables() self.all_styles = list(PredictorStyle) self.style = self.all_styles[0] # Default to the first style @@ -24,7 +28,9 @@ class ToolAgent: def choose_provider(self, index: int): """ Sceglie il modello LLM da utilizzare in base all'indice fornito. - index: indice del modello nella lista available_models. + + Args: + index: indice del modello nella lista available_models. """ # TODO Utilizzare AGNO per gestire i modelli... è molto più semplice e permette di cambiare modello facilmente # TODO https://docs.agno.com/introduction @@ -37,15 +43,18 @@ class ToolAgent: def choose_style(self, index: int): """ Sceglie lo stile di previsione da utilizzare in base all'indice fornito. - index: indice dello stile nella lista all_styles. + + Args: + index: indice dello stile nella lista all_styles. """ self.style = self.all_styles[index] def interact(self, query: str) -> str: """ Funzione principale che coordina gli agenti per rispondere alla richiesta dell'utente. - query: richiesta dell'utente (es. "Qual è la previsione per Bitcoin?") - style_index: indice dello stile di previsione nella lista all_styles. + + Args: + query: richiesta dell'utente (es. "Qual è la previsione per Bitcoin?") """ log_info(f"[model={self.chosen_model.name}] [style={self.style.name}] [query=\"{query.replace('"', "'")}\"]") diff --git a/src/app/utils/market_aggregator.py b/src/app/utils/market_aggregator.py index 2e89e7f..639bb9b 100644 --- a/src/app/utils/market_aggregator.py +++ b/src/app/utils/market_aggregator.py @@ -1,5 +1,5 @@ import statistics -from typing import Dict, List, Any +from typing import Dict, Any class MarketAggregator: """ @@ -65,6 +65,7 @@ class MarketAggregator: return float(v[:-1]) * 1_000 try: return float(v) - except Exception: + except Exception as e: + print(f"Errore nel parsing del volume: {e}") return 0.0 return 0.0 diff --git a/tests/agents/test_market.py b/tests/agents/test_market.py index 56931b3..cb6c390 100644 --- a/tests/agents/test_market.py +++ b/tests/agents/test_market.py @@ -1,10 +1,10 @@ import os import pytest -from app.agents.market import MarketToolkit -from app.markets.base import BaseWrapper -from app.markets.coinbase import CoinBaseWrapper -from app.markets.cryptocompare import CryptoCompareWrapper -from app.markets import MarketAPIs +from src.app.agents.market import MarketToolkit +from src.app.markets.base import BaseWrapper +from src.app.markets.coinbase import CoinBaseWrapper +from src.app.markets.cryptocompare import CryptoCompareWrapper +from src.app.markets import MarketAPIs class TestMarketSystem: """Test suite per il sistema di mercato (wrappers + toolkit)""" diff --git a/tests/agents/test_predictor.py b/tests/agents/test_predictor.py index c99104b..bf10837 100644 --- a/tests/agents/test_predictor.py +++ b/tests/agents/test_predictor.py @@ -1,11 +1,11 @@ import pytest -from app.agents.predictor import PREDICTOR_INSTRUCTIONS, PredictorInput, PredictorOutput, PredictorStyle -from app.markets.base import ProductInfo -from app.models import AppModels +from src.app.agents.predictor import PREDICTOR_INSTRUCTIONS, PredictorInput, PredictorOutput, PredictorStyle +from src.app.markets.base import ProductInfo +from src.app.models import AppModels -def unified_checks(model: AppModels, input): - llm = model.get_agent(PREDICTOR_INSTRUCTIONS, output=PredictorOutput) - result = llm.run(input) +def unified_checks(model: AppModels, user_input): + llm = model.get_agent(PREDICTOR_INSTRUCTIONS, output=PredictorOutput) # type: ignore[arg-type] + result = llm.run(user_input) content = result.content assert isinstance(content, PredictorOutput) From 2be9e0f319b2bb7e34e4ec5dea647b750bc1dd07 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Mon, 29 Sep 2025 14:39:31 +0200 Subject: [PATCH 02/22] Update pytest configuration and dependencies in pyproject.toml --- pyproject.toml | 4 ++++ tests/conftest.py | 8 +------- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 35a3b6e..d7caff6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,3 +32,7 @@ dependencies = [ "coinbase-advanced-py", "python-binance", ] + +[tool.pytest.ini_options] +pythonpath = ["src"] +testpaths = ["tests"] diff --git a/tests/conftest.py b/tests/conftest.py index cfe3606..2b6c16f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,16 +2,10 @@ Configurazione pytest per i test del progetto upo-appAI. """ -import sys import pytest -from pathlib import Path - -# Aggiungi il path src al PYTHONPATH per tutti i test -src_path = Path(__file__).parent.parent / "src" -sys.path.insert(0, str(src_path)) +from dotenv import load_dotenv # Carica le variabili d'ambiente per tutti i test -from dotenv import load_dotenv load_dotenv() From badd3e2a6cf5c41e3216693340dae38007404cdc Mon Sep 17 00:00:00 2001 From: Berack96 Date: Mon, 29 Sep 2025 15:15:18 +0200 Subject: [PATCH 03/22] Add news API integration and related configurations - Update .env.example to include NEWS_API_KEY configuration - Add newsapi-python dependency in pyproject.toml - Implement NewsAPI class for fetching news articles - Create Article model for structured news data - Add tests for NewsAPI functionality in test_news_api.py - Update pytest configuration to include news marker --- .env.example | 9 ++++++++- pyproject.toml | 3 +++ src/app/news/__init__.py | 3 +++ src/app/news/base.py | 8 ++++++++ src/app/news/news_api.py | 34 ++++++++++++++++++++++++++++++++++ tests/api/test_news_api.py | 19 +++++++++++++++++++ tests/conftest.py | 2 ++ uv.lock | 14 ++++++++++++++ 8 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/app/news/__init__.py create mode 100644 src/app/news/base.py create mode 100644 src/app/news/news_api.py create mode 100644 tests/api/test_news_api.py diff --git a/.env.example b/.env.example index 0bef205..d5d5a38 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ -########################################################################### +############################################################################### # Configurazioni per i modelli di linguaggio ############################################################################### @@ -22,3 +22,10 @@ CRYPTOCOMPARE_API_KEY= # Binance API per Market Agent (alternativa) BINANCE_API_KEY= BINANCE_API_SECRET= + +############################################################################### +# Configurazioni per gli agenti di notizie +############################################################################### +# Ottenibile da: https://newsapi.org/docs +NEWS_API_KEY= + diff --git a/pyproject.toml b/pyproject.toml index d7caff6..3ef7154 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,6 +31,9 @@ dependencies = [ # ✅ per interagire con API di exchange di criptovalute "coinbase-advanced-py", "python-binance", + + # ✅ per interagire con API di notizie + "newsapi-python", ] [tool.pytest.ini_options] diff --git a/src/app/news/__init__.py b/src/app/news/__init__.py new file mode 100644 index 0000000..9b88ff6 --- /dev/null +++ b/src/app/news/__init__.py @@ -0,0 +1,3 @@ +from .news_api import NewsAPI + +__all__ = ["NewsAPI"] \ No newline at end of file diff --git a/src/app/news/base.py b/src/app/news/base.py new file mode 100644 index 0000000..0391424 --- /dev/null +++ b/src/app/news/base.py @@ -0,0 +1,8 @@ +from pydantic import BaseModel + +class Article(BaseModel): + source: str = "" + time: str = "" + title: str = "" + description: str = "" + diff --git a/src/app/news/news_api.py b/src/app/news/news_api.py new file mode 100644 index 0000000..54b210a --- /dev/null +++ b/src/app/news/news_api.py @@ -0,0 +1,34 @@ +import os +import newsapi +from .base import Article + + +def result_to_article(result: dict) -> Article: + article = Article() + article.source = result.get("source", {}).get("name", "") + article.time = result.get("publishedAt", "") + article.title = result.get("title", "") + article.description = result.get("description", "") + return article + +class NewsAPI: + def __init__(self): + api_key = os.getenv("NEWS_API_KEY") + assert api_key is not None, "NEWS_API_KEY environment variable not set" + + self.client = newsapi.NewsApiClient(api_key=api_key) + self.category = "business" + self.language = "en" + self.page_size = 100 + + def get_top_headlines(self, query:str, total:int=100) -> list[Article]: + page_size = min(self.page_size, total) + pages = (total // page_size) + (1 if total % page_size > 0 else 0) + articles = [] + for page in range(1, pages + 1): + headlines = self.client.get_top_headlines(category=self.category, language=self.language, page_size=page_size, page=page) + results = [result_to_article(article) for article in headlines.get("articles", [])] + articles.extend(results) + return articles + + diff --git a/tests/api/test_news_api.py b/tests/api/test_news_api.py new file mode 100644 index 0000000..99a3179 --- /dev/null +++ b/tests/api/test_news_api.py @@ -0,0 +1,19 @@ +from app.news import NewsAPI + +class TestNewsAPI: + + def test_news_api_initialization(self): + news_api = NewsAPI() + assert news_api.client is not None + + def test_news_api_get_top_headlines(self): + news_api = NewsAPI() + articles = news_api.get_top_headlines(query="crypto", total=2) + assert isinstance(articles, list) + assert len(articles) == 2 + for article in articles: + assert hasattr(article, 'source') + assert hasattr(article, 'time') + assert hasattr(article, 'title') + assert hasattr(article, 'description') + diff --git a/tests/conftest.py b/tests/conftest.py index 2b6c16f..f2601b1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,6 +20,7 @@ def pytest_configure(config:pytest.Config): ("gemini", "marks tests that use Gemini model"), ("ollama_gpt", "marks tests that use Ollama GPT model"), ("ollama_qwen", "marks tests that use Ollama Qwen model"), + ("news", "marks tests that use news"), ] for marker in markers: line = f"{marker[0]}: {marker[1]}" @@ -37,6 +38,7 @@ def pytest_collection_modifyitems(config, items): "gemini": pytest.mark.gemini, "ollama_gpt": pytest.mark.ollama_gpt, "ollama_qwen": pytest.mark.ollama_qwen, + "news": pytest.mark.news, } for item in items: diff --git a/uv.lock b/uv.lock index 646299f..26356c0 100644 --- a/uv.lock +++ b/uv.lock @@ -686,6 +686,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/fd/69/b547032297c7e63ba2af494edba695d781af8a0c6e89e4d06cf848b21d80/multidict-6.6.4-py3-none-any.whl", hash = "sha256:27d8f8e125c07cb954e54d75d04905a9bba8a439c1d84aca94949d4d03d8601c", size = 12313, upload-time = "2025-08-11T12:08:46.891Z" }, ] +[[package]] +name = "newsapi-python" +version = "0.2.7" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "requests" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/f8/4b/12fb9495211fc5a6d3a96968759c1a48444124a1654aaf65d0de80b46794/newsapi-python-0.2.7.tar.gz", hash = "sha256:a4b66d5dd9892198cdaa476f7542f2625cdd218e5e3121c8f880b2ace717a3c2", size = 7485, upload-time = "2023-03-02T13:15:35.89Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/74/47/e3b099102f0c826d37841d2266e19f1568dcf58ba86e4c6948e2a124f91d/newsapi_python-0.2.7-py2.py3-none-any.whl", hash = "sha256:11d34013a24d92ca7b7cbdac84ed2d504862b1e22467bc2a9a6913a70962318e", size = 7942, upload-time = "2023-03-02T13:15:34.475Z" }, +] + [[package]] name = "numpy" version = "2.3.3" @@ -1298,6 +1310,7 @@ dependencies = [ { name = "dotenv" }, { name = "google-genai" }, { name = "gradio" }, + { name = "newsapi-python" }, { name = "ollama" }, { name = "pytest" }, { name = "python-binance" }, @@ -1310,6 +1323,7 @@ requires-dist = [ { name = "dotenv" }, { name = "google-genai" }, { name = "gradio" }, + { name = "newsapi-python" }, { name = "ollama" }, { name = "pytest" }, { name = "python-binance" }, From fc4753a24562c4765bde052835cd57606ce9aefd Mon Sep 17 00:00:00 2001 From: Berack96 Date: Mon, 29 Sep 2025 19:21:08 +0200 Subject: [PATCH 04/22] Add news API functionality and update tests for article retrieval --- demos/news_api.py | 16 ++++++++++++++++ src/app/news/news_api.py | 11 ++++++----- tests/api/test_news_api.py | 2 +- 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 demos/news_api.py diff --git a/demos/news_api.py b/demos/news_api.py new file mode 100644 index 0000000..0fc4c37 --- /dev/null +++ b/demos/news_api.py @@ -0,0 +1,16 @@ +#### FOR ALL FILES OUTSIDE src/ FOLDER #### +import sys +import os +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../src'))) +########################################### + +from dotenv import load_dotenv +from app.news import NewsAPI + +def main(): + api = NewsAPI() + print("ok") + +if __name__ == "__main__": + load_dotenv() + main() \ No newline at end of file diff --git a/src/app/news/news_api.py b/src/app/news/news_api.py index 54b210a..ce213cf 100644 --- a/src/app/news/news_api.py +++ b/src/app/news/news_api.py @@ -17,16 +17,17 @@ class NewsAPI: assert api_key is not None, "NEWS_API_KEY environment variable not set" self.client = newsapi.NewsApiClient(api_key=api_key) - self.category = "business" - self.language = "en" - self.page_size = 100 + self.category = "business" # Cryptocurrency is under business + self.language = "en" # TODO Only English articles for now? + self.max_page_size = 100 def get_top_headlines(self, query:str, total:int=100) -> list[Article]: - page_size = min(self.page_size, total) + page_size = min(self.max_page_size, total) pages = (total // page_size) + (1 if total % page_size > 0 else 0) + articles = [] for page in range(1, pages + 1): - headlines = self.client.get_top_headlines(category=self.category, language=self.language, page_size=page_size, page=page) + headlines = self.client.get_top_headlines(q=query, category=self.category, language=self.language, page_size=page_size, page=page) results = [result_to_article(article) for article in headlines.get("articles", [])] articles.extend(results) return articles diff --git a/tests/api/test_news_api.py b/tests/api/test_news_api.py index 99a3179..9558882 100644 --- a/tests/api/test_news_api.py +++ b/tests/api/test_news_api.py @@ -10,7 +10,7 @@ class TestNewsAPI: news_api = NewsAPI() articles = news_api.get_top_headlines(query="crypto", total=2) assert isinstance(articles, list) - assert len(articles) == 2 + assert len(articles) > 0 # Ensure we got some articles (apparently it doesn't always return the requested number) for article in articles: assert hasattr(article, 'source') assert hasattr(article, 'time') From dfca44c9d5d8c3d114a59c6765e7c7e7dcb26e9a Mon Sep 17 00:00:00 2001 From: Simone Garau <20005068@studenti.uniupo.it> Date: Mon, 29 Sep 2025 21:28:41 +0200 Subject: [PATCH 05/22] ToDo: 1. Aggiungere un aggregator per i dati recuperati dai provider. 2. Lavorare effettivamente all'issue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Done: 1. creati test per i provider 2. creato market_providers_api_demo.py per mostrare i dati recuperati dalle api dei providers 3. aggiornato i provider 4. creato il provider binance sia pubblico che con chiave 5. creato error_handler.py per gestire decoratori e utilità: retry automatico, gestione timeout... --- .env.example | 19 +- demos/cdp_market_demo.py | 116 ----- demos/market_agent_demo.py | 100 ----- demos/market_providers_api_demo.py | 374 ++++++++++++++++ src/app/markets/__init__.py | 52 ++- src/app/markets/base.py | 59 ++- src/app/markets/binance.py | 185 +++++++- src/app/markets/binance_public.py | 227 ++++++++++ src/app/markets/coinbase.py | 54 ++- src/app/markets/cryptocompare.py | 43 +- src/app/markets/error_handler.py | 236 ++++++++++ tests/agents/test_market.py | 688 ++++++++++++++++++++++++----- 12 files changed, 1753 insertions(+), 400 deletions(-) delete mode 100644 demos/cdp_market_demo.py delete mode 100644 demos/market_agent_demo.py create mode 100644 demos/market_providers_api_demo.py create mode 100644 src/app/markets/binance_public.py create mode 100644 src/app/markets/error_handler.py diff --git a/.env.example b/.env.example index 0bef205..1b969ef 100644 --- a/.env.example +++ b/.env.example @@ -6,19 +6,30 @@ # Vedi https://docs.agno.com/examples/models per vedere tutti i modelli supportati GOOGLE_API_KEY= +# Inserire il percorso di installazione di ollama (es. /usr/share/ollama/.ollama) +# attenzione che fra Linux nativo e WSL il percorso è diverso +OLLAMA_MODELS_PATH= ############################################################################### # Configurazioni per gli agenti di mercato ############################################################################### # Coinbase CDP API per Market Agent # Ottenibili da: https://portal.cdp.coinbase.com/access/api -CDP_API_KEY_NAME= -CDP_API_PRIVATE_KEY= +# IMPORTANTE: Usare le credenziali CDP (NON Exchange legacy) +# - COINBASE_API_KEY: organizations/{org_id}/apiKeys/{key_id} +# - COINBASE_API_SECRET: La private key completa (inclusi BEGIN/END) +# - NON serve COINBASE_PASSPHRASE (solo per Exchange legacy) +COINBASE_API_KEY= +COINBASE_API_SECRET= -# CryptoCompare API per Market Agent (alternativa) +# CryptoCompare API per Market Agent # Ottenibile da: https://www.cryptocompare.com/cryptopian/api-keys +# NOTA: API legacy, potrebbe essere deprecata in futuro +# Funzionalità limitata: get_all_products() non supportato CRYPTOCOMPARE_API_KEY= -# Binance API per Market Agent (alternativa) +# Binance API per Market Agent +# Ottenibili da: https://www.binance.com/en/my/settings/api-management +# Supporta sia API autenticate che pubbliche (PublicBinance) BINANCE_API_KEY= BINANCE_API_SECRET= diff --git a/demos/cdp_market_demo.py b/demos/cdp_market_demo.py deleted file mode 100644 index 307d02f..0000000 --- a/demos/cdp_market_demo.py +++ /dev/null @@ -1,116 +0,0 @@ -#!/usr/bin/env python3 -""" -Demo script per testare il MarketAgent aggiornato con Coinbase CDP -""" - -import sys -import os -sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) - -from src.app.agents.market_agent import MarketAgent -import logging - -# Setup logging -logging.basicConfig( - level=logging.INFO, - format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' -) - -def main(): - print("🚀 Test MarketAgent con Coinbase CDP") - print("=" * 50) - - # Inizializza l'agent - agent = MarketAgent() - - # Verifica provider disponibili - providers = agent.get_available_providers() - print(f"📡 Provider disponibili: {providers}") - - if not providers: - print("⚠️ Nessun provider configurato. Verifica il file .env") - print("\nPer Coinbase CDP, serve:") - print("CDP_API_KEY_NAME=your_key_name") - print("CDP_API_PRIVATE_KEY=your_private_key") - print("\nPer CryptoCompare, serve:") - print("CRYPTOCOMPARE_API_KEY=your_api_key") - return - - # Mostra capabilities di ogni provider - for provider in providers: - capabilities = agent.get_provider_capabilities(provider) - print(f"🔧 {provider.upper()}: {capabilities}") - - print("\n" + "=" * 50) - - # Test ottenimento prezzo singolo - test_symbols = ["BTC", "ETH", "ADA"] - - for symbol in test_symbols: - print(f"\n💰 Prezzo {symbol}:") - - # Prova ogni provider - for provider in providers: - try: - price = agent.get_asset_price(symbol, provider) - if price: - print(f" {provider}: ${price:,.2f}") - else: - print(f" {provider}: N/A") - except Exception as e: - print(f" {provider}: Errore - {e}") - - print("\n" + "=" * 50) - - # Test market overview - print("\n📊 Market Overview:") - try: - overview = agent.get_market_overview(["BTC", "ETH", "ADA", "DOT"]) - - if overview["data"]: - print(f"📡 Fonte: {overview['source']}") - - for crypto, prices in overview["data"].items(): - if isinstance(prices, dict): - usd_price = prices.get("USD", "N/A") - eur_price = prices.get("EUR", "N/A") - - if eur_price != "N/A": - print(f" {crypto}: ${usd_price} (€{eur_price})") - else: - print(f" {crypto}: ${usd_price}") - else: - print("⚠️ Nessun dato disponibile") - - except Exception as e: - print(f"❌ Errore nel market overview: {e}") - - print("\n" + "=" * 50) - - # Test funzione analyze - print("\n🔍 Analisi mercato:") - try: - analysis = agent.analyze("Market overview") - print(analysis) - except Exception as e: - print(f"❌ Errore nell'analisi: {e}") - - # Test specifico Coinbase CDP se disponibile - if 'coinbase' in providers: - print("\n" + "=" * 50) - print("\n🏦 Test specifico Coinbase CDP:") - - try: - # Test asset singolo - btc_info = agent.get_coinbase_asset_info("BTC") - print(f"📈 BTC Info: {btc_info}") - - # Test asset multipli - multi_assets = agent.get_coinbase_multiple_assets(["BTC", "ETH"]) - print(f"📊 Multi Assets: {multi_assets}") - - except Exception as e: - print(f"❌ Errore nel test Coinbase CDP: {e}") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/demos/market_agent_demo.py b/demos/market_agent_demo.py deleted file mode 100644 index 1ef8f21..0000000 --- a/demos/market_agent_demo.py +++ /dev/null @@ -1,100 +0,0 @@ -#!/usr/bin/env python3 -""" -Esempio di utilizzo del MarketAgent unificato. -Questo script mostra come utilizzare il nuovo MarketAgent che supporta -multiple fonti di dati (Coinbase e CryptoCompare). -""" - -import sys -from pathlib import Path - -# Aggiungi il path src al PYTHONPATH -src_path = Path(__file__).parent / "src" -sys.path.insert(0, str(src_path)) - -from dotenv import load_dotenv -from app.agents.market_agent import MarketAgent - -# Carica variabili d'ambiente -load_dotenv() - -def main(): - print("🚀 Market Agent Demo\n") - - try: - # Inizializza il market agent (auto-configura i provider disponibili) - agent = MarketAgent() - - # Mostra provider disponibili - providers = agent.get_available_providers() - print(f"📡 Available providers: {providers}") - - if not providers: - print("❌ No providers configured. Please check your .env file.") - print("Required variables:") - print(" For Coinbase: COINBASE_API_KEY, COINBASE_SECRET, COINBASE_PASSPHRASE") - print(" For CryptoCompare: CRYPTOCOMPARE_API_KEY") - return - - # Mostra le capacità di ogni provider - print("\n🔧 Provider capabilities:") - for provider in providers: - capabilities = agent.get_provider_capabilities(provider) - print(f" {provider}: {capabilities}") - - # Ottieni panoramica del mercato - print("\n📊 Market Overview:") - overview = agent.get_market_overview(["BTC", "ETH", "ADA"]) - print(f"Data source: {overview.get('source', 'Unknown')}") - - for crypto, prices in overview.get('data', {}).items(): - if isinstance(prices, dict): - usd = prices.get('USD', 'N/A') - eur = prices.get('EUR', 'N/A') - if eur != 'N/A': - print(f" {crypto}: ${usd} (€{eur})") - else: - print(f" {crypto}: ${usd}") - - # Analisi completa del mercato - print("\n📈 Market Analysis:") - analysis = agent.analyze("comprehensive market analysis") - print(analysis) - - # Test specifici per provider (se disponibili) - if 'cryptocompare' in providers: - print("\n🔸 CryptoCompare specific test:") - try: - btc_price = agent.get_single_crypto_price("BTC", "USD") - print(f" BTC price: ${btc_price}") - - top_coins = agent.get_top_cryptocurrencies(5) - if top_coins.get('Data'): - print(" Top 5 cryptocurrencies by market cap:") - for coin in top_coins['Data'][:3]: # Show top 3 - coin_info = coin.get('CoinInfo', {}) - display = coin.get('DISPLAY', {}).get('USD', {}) - name = coin_info.get('FullName', 'Unknown') - price = display.get('PRICE', 'N/A') - print(f" {name}: {price}") - except Exception as e: - print(f" CryptoCompare test failed: {e}") - - if 'coinbase' in providers: - print("\n🔸 Coinbase specific test:") - try: - ticker = agent.get_coinbase_ticker("BTC-USD") - price = ticker.get('price', 'N/A') - volume = ticker.get('volume_24h', 'N/A') - print(f" BTC-USD: ${price} (24h volume: {volume})") - except Exception as e: - print(f" Coinbase test failed: {e}") - - print("\n✅ Demo completed successfully!") - - except Exception as e: - print(f"❌ Demo failed: {e}") - print("Make sure you have configured at least one provider in your .env file.") - -if __name__ == "__main__": - main() \ No newline at end of file diff --git a/demos/market_providers_api_demo.py b/demos/market_providers_api_demo.py new file mode 100644 index 0000000..39055bb --- /dev/null +++ b/demos/market_providers_api_demo.py @@ -0,0 +1,374 @@ +#!/usr/bin/env python3 +""" +Demo Completo per Market Data Providers +======================================== + +Questo script dimostra l'utilizzo di tutti i wrapper che implementano BaseWrapper: +- CoinBaseWrapper (richiede credenziali) +- CryptoCompareWrapper (richiede API key) +- BinanceWrapper (richiede credenziali) +- PublicBinanceAgent (accesso pubblico) + +Lo script effettua chiamate GET a diversi provider e visualizza i dati +in modo strutturato con informazioni dettagliate su timestamp, stato +delle richieste e formattazione tabellare. +""" + +import sys +import os +from pathlib import Path +from datetime import datetime +from typing import Dict, List, Optional, Any +import traceback + +# Aggiungi il path src al PYTHONPATH +project_root = Path(__file__).parent.parent +sys.path.insert(0, str(project_root / "src")) + +from dotenv import load_dotenv +from app.markets import ( + CoinBaseWrapper, + CryptoCompareWrapper, + BinanceWrapper, + PublicBinanceAgent, + BaseWrapper +) + +# Carica variabili d'ambiente +load_dotenv() + +class DemoFormatter: + """Classe per formattare l'output del demo in modo strutturato.""" + + @staticmethod + def print_header(title: str, char: str = "=", width: int = 80): + """Stampa un'intestazione formattata.""" + print(f"\n{char * width}") + print(f"{title:^{width}}") + print(f"{char * width}") + + @staticmethod + def print_subheader(title: str, char: str = "-", width: int = 60): + """Stampa una sotto-intestazione formattata.""" + print(f"\n{char * width}") + print(f" {title}") + print(f"{char * width}") + + @staticmethod + def print_request_info(provider_name: str, method: str, timestamp: datetime, + status: str, error: Optional[str] = None): + """Stampa informazioni sulla richiesta.""" + print(f"🕒 Timestamp: {timestamp.strftime('%Y-%m-%d %H:%M:%S')}") + print(f"🏷️ Provider: {provider_name}") + print(f"🔧 Method: {method}") + print(f"📊 Status: {status}") + if error: + print(f"❌ Error: {error}") + print() + + @staticmethod + def print_product_table(products: List[Any], title: str = "Products"): + """Stampa una tabella di prodotti.""" + if not products: + print(f"📋 {title}: Nessun prodotto trovato") + return + + print(f"📋 {title} ({len(products)} items):") + print(f"{'Symbol':<15} {'ID':<20} {'Price':<12} {'Quote':<10} {'Status':<10}") + print("-" * 67) + + for product in products[:10]: # Mostra solo i primi 10 + symbol = getattr(product, 'symbol', 'N/A') + product_id = getattr(product, 'id', 'N/A') + price = getattr(product, 'price', 0.0) + quote = getattr(product, 'quote_currency', 'N/A') + status = getattr(product, 'status', 'N/A') + + # Tronca l'ID se troppo lungo + if len(product_id) > 18: + product_id = product_id[:15] + "..." + + price_str = f"${price:.2f}" if price > 0 else "N/A" + + print(f"{symbol:<15} {product_id:<20} {price_str:<12} {quote:<10} {status:<10}") + + if len(products) > 10: + print(f"... e altri {len(products) - 10} prodotti") + print() + + @staticmethod + def print_prices_table(prices: List[Any], title: str = "Historical Prices"): + """Stampa una tabella di prezzi storici.""" + if not prices: + print(f"💰 {title}: Nessun prezzo trovato") + return + + print(f"💰 {title} ({len(prices)} entries):") + print(f"{'Time':<12} {'Open':<12} {'High':<12} {'Low':<12} {'Close':<12} {'Volume':<15}") + print("-" * 75) + + for price in prices[:5]: # Mostra solo i primi 5 + time_str = getattr(price, 'time', 'N/A') + # Il time è già una stringa, non serve strftime + if len(time_str) > 10: + time_str = time_str[:10] # Tronca se troppo lungo + + open_price = f"${getattr(price, 'open', 0):.2f}" + high_price = f"${getattr(price, 'high', 0):.2f}" + low_price = f"${getattr(price, 'low', 0):.2f}" + close_price = f"${getattr(price, 'close', 0):.2f}" + volume = f"{getattr(price, 'volume', 0):,.0f}" + + print(f"{time_str:<12} {open_price:<12} {high_price:<12} {low_price:<12} {close_price:<12} {volume:<15}") + + if len(prices) > 5: + print(f"... e altri {len(prices) - 5} prezzi") + print() + +class ProviderTester: + """Classe per testare i provider di market data.""" + + def __init__(self): + self.formatter = DemoFormatter() + self.test_symbols = ["BTC", "ETH", "ADA"] + + def test_provider(self, wrapper: BaseWrapper, provider_name: str) -> Dict[str, Any]: + """Testa un provider specifico con tutti i metodi disponibili.""" + results = { + "provider_name": provider_name, + "tests": {}, + "overall_status": "SUCCESS" + } + + self.formatter.print_subheader(f"🔍 Testing {provider_name}") + + # Test get_product + for symbol in self.test_symbols: + timestamp = datetime.now() + try: + product = wrapper.get_product(symbol) + self.formatter.print_request_info( + provider_name, f"get_product({symbol})", timestamp, "✅ SUCCESS" + ) + if product: + print(f"📦 Product: {product.symbol} (ID: {product.id})") + print(f" Price: ${product.price:.2f}, Quote: {product.quote_currency}") + print(f" Status: {product.status}, Volume 24h: {product.volume_24h:,.2f}") + else: + print(f"📦 Product: Nessun prodotto trovato per {symbol}") + + results["tests"][f"get_product_{symbol}"] = "SUCCESS" + + except Exception as e: + error_msg = str(e) + self.formatter.print_request_info( + provider_name, f"get_product({symbol})", timestamp, "❌ ERROR", error_msg + ) + results["tests"][f"get_product_{symbol}"] = f"ERROR: {error_msg}" + results["overall_status"] = "PARTIAL" + + # Test get_products + timestamp = datetime.now() + try: + products = wrapper.get_products(self.test_symbols) + self.formatter.print_request_info( + provider_name, f"get_products({self.test_symbols})", timestamp, "✅ SUCCESS" + ) + self.formatter.print_product_table(products, f"{provider_name} Products") + results["tests"]["get_products"] = "SUCCESS" + + except Exception as e: + error_msg = str(e) + self.formatter.print_request_info( + provider_name, f"get_products({self.test_symbols})", timestamp, "❌ ERROR", error_msg + ) + results["tests"]["get_products"] = f"ERROR: {error_msg}" + results["overall_status"] = "PARTIAL" + + # Test get_all_products + timestamp = datetime.now() + try: + all_products = wrapper.get_all_products() + self.formatter.print_request_info( + provider_name, "get_all_products()", timestamp, "✅ SUCCESS" + ) + self.formatter.print_product_table(all_products, f"{provider_name} All Products") + results["tests"]["get_all_products"] = "SUCCESS" + + except Exception as e: + error_msg = str(e) + self.formatter.print_request_info( + provider_name, "get_all_products()", timestamp, "❌ ERROR", error_msg + ) + results["tests"]["get_all_products"] = f"ERROR: {error_msg}" + results["overall_status"] = "PARTIAL" + + # Test get_historical_prices + timestamp = datetime.now() + try: + prices = wrapper.get_historical_prices("BTC") + self.formatter.print_request_info( + provider_name, "get_historical_prices(BTC)", timestamp, "✅ SUCCESS" + ) + self.formatter.print_prices_table(prices, f"{provider_name} BTC Historical Prices") + results["tests"]["get_historical_prices"] = "SUCCESS" + + except Exception as e: + error_msg = str(e) + self.formatter.print_request_info( + provider_name, "get_historical_prices(BTC)", timestamp, "❌ ERROR", error_msg + ) + results["tests"]["get_historical_prices"] = f"ERROR: {error_msg}" + results["overall_status"] = "PARTIAL" + + return results + +def check_environment_variables() -> Dict[str, bool]: + """Verifica la presenza delle variabili d'ambiente necessarie.""" + env_vars = { + "COINBASE_API_KEY": bool(os.getenv("COINBASE_API_KEY")), + "COINBASE_API_SECRET": bool(os.getenv("COINBASE_API_SECRET")), + "CRYPTOCOMPARE_API_KEY": bool(os.getenv("CRYPTOCOMPARE_API_KEY")), + "BINANCE_API_KEY": bool(os.getenv("BINANCE_API_KEY")), + "BINANCE_API_SECRET": bool(os.getenv("BINANCE_API_SECRET")), + } + return env_vars + +def initialize_providers() -> Dict[str, BaseWrapper]: + """Inizializza tutti i provider disponibili.""" + providers = {} + env_vars = check_environment_variables() + + # PublicBinanceAgent (sempre disponibile) + try: + providers["PublicBinance"] = PublicBinanceAgent() + print("✅ PublicBinanceAgent inizializzato con successo") + except Exception as e: + print(f"❌ Errore nell'inizializzazione di PublicBinanceAgent: {e}") + + # CryptoCompareWrapper + if env_vars["CRYPTOCOMPARE_API_KEY"]: + try: + providers["CryptoCompare"] = CryptoCompareWrapper() + print("✅ CryptoCompareWrapper inizializzato con successo") + except Exception as e: + print(f"❌ Errore nell'inizializzazione di CryptoCompareWrapper: {e}") + else: + print("⚠️ CryptoCompareWrapper saltato: CRYPTOCOMPARE_API_KEY non trovata") + + # CoinBaseWrapper + if env_vars["COINBASE_API_KEY"] and env_vars["COINBASE_API_SECRET"]: + try: + providers["CoinBase"] = CoinBaseWrapper() + print("✅ CoinBaseWrapper inizializzato con successo") + except Exception as e: + print(f"❌ Errore nell'inizializzazione di CoinBaseWrapper: {e}") + else: + print("⚠️ CoinBaseWrapper saltato: credenziali Coinbase non complete") + + # BinanceWrapper + if env_vars["BINANCE_API_KEY"] and env_vars["BINANCE_API_SECRET"]: + try: + providers["Binance"] = BinanceWrapper() + print("✅ BinanceWrapper inizializzato con successo") + except Exception as e: + print(f"❌ Errore nell'inizializzazione di BinanceWrapper: {e}") + else: + print("⚠️ BinanceWrapper saltato: credenziali Binance non complete") + + return providers + +def print_summary(results: List[Dict[str, Any]]): + """Stampa un riassunto finale dei risultati.""" + formatter = DemoFormatter() + formatter.print_header("📊 RIASSUNTO FINALE", "=", 80) + + total_providers = len(results) + successful_providers = sum(1 for r in results if r["overall_status"] == "SUCCESS") + partial_providers = sum(1 for r in results if r["overall_status"] == "PARTIAL") + + print(f"🔢 Provider testati: {total_providers}") + print(f"✅ Provider completamente funzionanti: {successful_providers}") + print(f"⚠️ Provider parzialmente funzionanti: {partial_providers}") + print(f"❌ Provider non funzionanti: {total_providers - successful_providers - partial_providers}") + + print("\n📋 Dettaglio per provider:") + for result in results: + provider_name = result["provider_name"] + status = result["overall_status"] + status_icon = "✅" if status == "SUCCESS" else "⚠️" if status == "PARTIAL" else "❌" + + print(f"\n{status_icon} {provider_name}:") + for test_name, test_result in result["tests"].items(): + test_icon = "✅" if test_result == "SUCCESS" else "❌" + print(f" {test_icon} {test_name}: {test_result}") + +def main(): + """Funzione principale del demo.""" + formatter = DemoFormatter() + + # Intestazione principale + formatter.print_header("🚀 DEMO COMPLETO MARKET DATA PROVIDERS", "=", 80) + + print(f"🕒 Avvio demo: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") + print("📝 Questo demo testa tutti i wrapper BaseWrapper disponibili") + print("🔍 Ogni test include timestamp, stato della richiesta e dati formattati") + + # Verifica variabili d'ambiente + formatter.print_subheader("🔐 Verifica Configurazione") + env_vars = check_environment_variables() + + print("Variabili d'ambiente:") + for var_name, is_present in env_vars.items(): + status = "✅ Presente" if is_present else "❌ Mancante" + print(f" {var_name}: {status}") + + # Inizializza provider + formatter.print_subheader("🏗️ Inizializzazione Provider") + providers = initialize_providers() + + if not providers: + print("❌ Nessun provider disponibile. Verifica la configurazione.") + return + + print(f"\n🎯 Provider disponibili per il test: {list(providers.keys())}") + + # Testa ogni provider + formatter.print_header("🧪 ESECUZIONE TEST PROVIDER", "=", 80) + + tester = ProviderTester() + all_results = [] + + for provider_name, wrapper in providers.items(): + try: + result = tester.test_provider(wrapper, provider_name) + all_results.append(result) + except Exception as e: + print(f"❌ Errore critico nel test di {provider_name}: {e}") + traceback.print_exc() + all_results.append({ + "provider_name": provider_name, + "tests": {}, + "overall_status": "CRITICAL_ERROR", + "error": str(e) + }) + + # Stampa riassunto finale + print_summary(all_results) + + # Informazioni aggiuntive + formatter.print_header("ℹ️ INFORMAZIONI AGGIUNTIVE", "=", 80) + print("📚 Documentazione:") + print(" - BaseWrapper: src/app/markets/base.py") + print(" - Test completi: tests/agents/test_market.py") + print(" - Configurazione: .env") + + print("\n🔧 Per abilitare tutti i provider:") + print(" 1. Configura le credenziali nel file .env") + print(" 2. Segui la documentazione di ogni provider") + print(" 3. Riavvia il demo") + + print(f"\n🏁 Demo completato: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/app/markets/__init__.py b/src/app/markets/__init__.py index 4bb3e9e..7880fd4 100644 --- a/src/app/markets/__init__.py +++ b/src/app/markets/__init__.py @@ -1,8 +1,14 @@ -from app.markets.base import BaseWrapper -from app.markets.coinbase import CoinBaseWrapper -from app.markets.cryptocompare import CryptoCompareWrapper +from .base import BaseWrapper +from .coinbase import CoinBaseWrapper +from .cryptocompare import CryptoCompareWrapper +from .binance import BinanceWrapper +from .binance_public import PublicBinanceAgent +from .error_handler import ProviderFallback, MarketAPIError, safe_execute from agno.utils.log import log_warning +import logging + +logger = logging.getLogger(__name__) class MarketAPIs(BaseWrapper): """ @@ -43,15 +49,37 @@ class MarketAPIs(BaseWrapper): """ self.currency = currency self.wrappers = MarketAPIs.get_list_available_market_apis(currency=currency) + self.fallback_manager = ProviderFallback(self.wrappers) - # Metodi che semplicemente chiamano il metodo corrispondente del primo wrapper disponibile - # TODO magari fare in modo che se il primo fallisce, prova con il secondo, ecc. - # oppure fare un round-robin tra i vari wrapper oppure usarli tutti e fare una media dei risultati - def get_product(self, asset_id): - return self.wrappers[0].get_product(asset_id) + # Metodi con fallback robusto tra provider multipli + def get_product(self, asset_id: str): + """Ottiene informazioni su un prodotto con fallback automatico tra provider.""" + try: + return self.fallback_manager.execute_with_fallback("get_product", asset_id) + except MarketAPIError as e: + logger.error(f"Failed to get product {asset_id}: {str(e)}") + raise + def get_products(self, asset_ids: list): - return self.wrappers[0].get_products(asset_ids) + """Ottiene informazioni su più prodotti con fallback automatico tra provider.""" + try: + return self.fallback_manager.execute_with_fallback("get_products", asset_ids) + except MarketAPIError as e: + logger.error(f"Failed to get products {asset_ids}: {str(e)}") + raise + def get_all_products(self): - return self.wrappers[0].get_all_products() - def get_historical_prices(self, asset_id = "BTC"): - return self.wrappers[0].get_historical_prices(asset_id) + """Ottiene tutti i prodotti con fallback automatico tra provider.""" + try: + return self.fallback_manager.execute_with_fallback("get_all_products") + except MarketAPIError as e: + logger.error(f"Failed to get all products: {str(e)}") + raise + + def get_historical_prices(self, asset_id: str = "BTC"): + """Ottiene prezzi storici con fallback automatico tra provider.""" + try: + return self.fallback_manager.execute_with_fallback("get_historical_prices", asset_id) + except MarketAPIError as e: + logger.error(f"Failed to get historical prices for {asset_id}: {str(e)}") + raise diff --git a/src/app/markets/base.py b/src/app/markets/base.py index 032f8aa..74bc7e2 100644 --- a/src/app/markets/base.py +++ b/src/app/markets/base.py @@ -1,4 +1,4 @@ -from coinbase.rest.types.product_types import Candle, GetProductResponse +from coinbase.rest.types.product_types import Candle, GetProductResponse, Product from pydantic import BaseModel class BaseWrapper: @@ -27,16 +27,28 @@ class ProductInfo(BaseModel): status: str = "" quote_currency: str = "" + @staticmethod def from_coinbase(product_data: GetProductResponse) -> 'ProductInfo': product = ProductInfo() - product.id = product_data.product_id - product.symbol = product_data.base_currency_id - product.price = float(product_data.price) - product.volume_24h = float(product_data.volume_24h) if product_data.volume_24h else 0 + product.id = product_data.product_id or "" + product.symbol = product_data.base_currency_id or "" + product.price = float(product_data.price) if product_data.price else 0.0 + product.volume_24h = float(product_data.volume_24h) if product_data.volume_24h else 0.0 # TODO Check what status means in Coinbase - product.status = product_data.status + product.status = product_data.status or "" return product + @staticmethod + def from_coinbase_product(product_data: Product) -> 'ProductInfo': + product = ProductInfo() + product.id = product_data.product_id or "" + product.symbol = product_data.base_currency_id or "" + product.price = float(product_data.price) if product_data.price else 0.0 + product.volume_24h = float(product_data.volume_24h) if product_data.volume_24h else 0.0 + product.status = product_data.status or "" + return product + + @staticmethod def from_cryptocompare(asset_data: dict) -> 'ProductInfo': product = ProductInfo() product.id = asset_data['FROMSYMBOL'] + '-' + asset_data['TOSYMBOL'] @@ -46,6 +58,27 @@ class ProductInfo(BaseModel): product.status = "" # Cryptocompare does not provide status return product + @staticmethod + def from_binance(ticker_data: dict, ticker_24h_data: dict) -> 'ProductInfo': + """ + Crea un oggetto ProductInfo da dati Binance. + + Args: + ticker_data: Dati del ticker di prezzo + ticker_24h_data: Dati del ticker 24h + + Returns: + Oggetto ProductInfo + """ + product = ProductInfo() + product.id = ticker_data['symbol'] + product.symbol = ticker_data['symbol'].replace('USDT', '').replace('BUSD', '') + product.price = float(ticker_data['price']) + product.volume_24h = float(ticker_24h_data['volume']) + product.status = "TRADING" # Binance non fornisce status esplicito + product.quote_currency = "USDT" # Assumiamo USDT come default + return product + class Price(BaseModel): """ Rappresenta i dati di prezzo per un asset, come ottenuti dalle API di mercato. @@ -58,16 +91,18 @@ class Price(BaseModel): volume: float = 0.0 time: str = "" + @staticmethod def from_coinbase(candle_data: Candle) -> 'Price': price = Price() - price.high = float(candle_data.high) - price.low = float(candle_data.low) - price.open = float(candle_data.open) - price.close = float(candle_data.close) - price.volume = float(candle_data.volume) - price.time = str(candle_data.start) + price.high = float(candle_data.high) if candle_data.high else 0.0 + price.low = float(candle_data.low) if candle_data.low else 0.0 + price.open = float(candle_data.open) if candle_data.open else 0.0 + price.close = float(candle_data.close) if candle_data.close else 0.0 + price.volume = float(candle_data.volume) if candle_data.volume else 0.0 + price.time = str(candle_data.start) if candle_data.start else "" return price + @staticmethod def from_cryptocompare(price_data: dict) -> 'Price': price = Price() price.high = float(price_data['high']) diff --git a/src/app/markets/binance.py b/src/app/markets/binance.py index 80f64c2..8bc1101 100644 --- a/src/app/markets/binance.py +++ b/src/app/markets/binance.py @@ -1,30 +1,169 @@ -# Versione pubblica senza autenticazione +import os +from typing import Optional +from datetime import datetime, timedelta from binance.client import Client +from .base import ProductInfo, BaseWrapper, Price +from .error_handler import retry_on_failure, handle_api_errors, MarketAPIError -# TODO fare l'aggancio con API in modo da poterlo usare come wrapper di mercato -# TODO implementare i metodi di BaseWrapper -class PublicBinanceAgent: - def __init__(self): - # Client pubblico (senza credenziali) - self.client = Client() +class BinanceWrapper(BaseWrapper): + """ + Wrapper per le API autenticate di Binance. + + Implementa l'interfaccia BaseWrapper per fornire accesso unificato + ai dati di mercato di Binance tramite le API REST con autenticazione. + + La documentazione delle API è disponibile qui: + https://binance-docs.github.io/apidocs/spot/en/ + """ + + def __init__(self, api_key: Optional[str] = None, api_secret: Optional[str] = None, currency: str = "USDT"): + """ + Inizializza il wrapper con le credenziali API. + + Args: + api_key: Chiave API di Binance (se None, usa variabile d'ambiente) + api_secret: Secret API di Binance (se None, usa variabile d'ambiente) + currency: Valuta di quotazione di default (default: USDT) + """ + if api_key is None: + api_key = os.getenv("BINANCE_API_KEY") + assert api_key is not None, "API key is required" - def get_public_prices(self): - """Ottiene prezzi pubblici""" + if api_secret is None: + api_secret = os.getenv("BINANCE_API_SECRET") + assert api_secret is not None, "API secret is required" + + self.currency = currency + self.client = Client(api_key=api_key, api_secret=api_secret) + + def __format_symbol(self, asset_id: str) -> str: + """ + Formatta l'asset_id nel formato richiesto da Binance. + + Args: + asset_id: ID dell'asset (es. "BTC" o "BTC-USDT") + + Returns: + Simbolo formattato per Binance (es. "BTCUSDT") + """ + if '-' in asset_id: + # Se già nel formato "BTC-USDT", converte in "BTCUSDT" + return asset_id.replace('-', '') + else: + # Se solo "BTC", aggiunge la valuta di default + return f"{asset_id}{self.currency}" + + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors + def get_product(self, asset_id: str) -> ProductInfo: + """ + Ottiene informazioni su un singolo prodotto. + + Args: + asset_id: ID dell'asset da recuperare + + Returns: + Oggetto ProductInfo con le informazioni del prodotto + """ + symbol = self.__format_symbol(asset_id) + ticker = self.client.get_symbol_ticker(symbol=symbol) + ticker_24h = self.client.get_ticker(symbol=symbol) + + return ProductInfo.from_binance(ticker, ticker_24h) + + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors + def get_products(self, asset_ids: list[str]) -> list[ProductInfo]: + """ + Ottiene informazioni su più prodotti. + + Args: + asset_ids: Lista di ID degli asset da recuperare + + Returns: + Lista di oggetti ProductInfo + """ + products = [] + for asset_id in asset_ids: + try: + product = self.get_product(asset_id) + products.append(product) + except Exception as e: + print(f"Errore nel recupero di {asset_id}: {e}") + continue + return products + + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors + def get_all_products(self) -> list[ProductInfo]: + """ + Ottiene informazioni su tutti i prodotti disponibili. + + Returns: + Lista di oggetti ProductInfo per tutti i prodotti + """ try: - btc_price = self.client.get_symbol_ticker(symbol="BTCUSDT") - eth_price = self.client.get_symbol_ticker(symbol="ETHUSDT") - - return { - 'BTC_USD': float(btc_price['price']), - 'ETH_USD': float(eth_price['price']), - 'source': 'binance_public' - } + # Ottiene tutti i ticker 24h che contengono le informazioni necessarie + all_tickers = self.client.get_ticker() + products = [] + + for ticker in all_tickers: + # Filtra solo i simboli che terminano con la valuta di default + if ticker['symbol'].endswith(self.currency): + try: + # Crea ProductInfo direttamente dal ticker 24h + product = ProductInfo() + product.id = ticker['symbol'] + product.symbol = ticker['symbol'].replace(self.currency, '') + product.price = float(ticker['lastPrice']) + product.volume_24h = float(ticker['volume']) + product.status = "TRADING" # Binance non fornisce status esplicito + product.quote_currency = self.currency + products.append(product) + except (ValueError, KeyError) as e: + print(f"Errore nel parsing di {ticker['symbol']}: {e}") + continue + + return products except Exception as e: - print(f"Errore: {e}") - return None + print(f"Errore nel recupero di tutti i prodotti: {e}") + return [] -# Uso senza credenziali -public_agent = PublicBinanceAgent() -public_prices = public_agent.get_public_prices() -print(public_prices) + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors + def get_historical_prices(self, asset_id: str = "BTC") -> list[Price]: + """ + Ottiene i prezzi storici per un asset. + + Args: + asset_id: ID dell'asset (default: "BTC") + + Returns: + Lista di oggetti Price con i dati storici + """ + symbol = self.__format_symbol(asset_id) + + try: + # Ottiene candele orarie degli ultimi 30 giorni + klines = self.client.get_historical_klines( + symbol=symbol, + interval=Client.KLINE_INTERVAL_1HOUR, + start_str="30 days ago UTC" + ) + + prices = [] + for kline in klines: + price = Price() + price.open = float(kline[1]) + price.high = float(kline[2]) + price.low = float(kline[3]) + price.close = float(kline[4]) + price.volume = float(kline[5]) + price.time = str(datetime.fromtimestamp(kline[0] / 1000)) + prices.append(price) + + return prices + except Exception as e: + print(f"Errore nel recupero dei prezzi storici per {symbol}: {e}") + return [] diff --git a/src/app/markets/binance_public.py b/src/app/markets/binance_public.py new file mode 100644 index 0000000..598840b --- /dev/null +++ b/src/app/markets/binance_public.py @@ -0,0 +1,227 @@ +""" +Versione pubblica di Binance per accesso ai dati pubblici senza autenticazione. + +Questa implementazione estende BaseWrapper per mantenere coerenza +con l'architettura del modulo markets. +""" + +from typing import Optional, Dict, Any +from datetime import datetime, timedelta +from binance.client import Client +from .base import BaseWrapper, ProductInfo, Price +from .error_handler import retry_on_failure, handle_api_errors, MarketAPIError + + +class PublicBinanceAgent(BaseWrapper): + """ + Agent per l'accesso ai dati pubblici di Binance. + + Utilizza l'API pubblica di Binance per ottenere informazioni + sui prezzi e sui mercati senza richiedere autenticazione. + """ + + def __init__(self): + """Inizializza il client pubblico senza credenziali.""" + self.client = Client() + + def __format_symbol(self, asset_id: str) -> str: + """ + Formatta l'asset_id per Binance (es. BTC -> BTCUSDT). + + Args: + asset_id: ID dell'asset (es. "BTC", "ETH") + + Returns: + Simbolo formattato per Binance + """ + if asset_id.endswith("USDT") or asset_id.endswith("BUSD"): + return asset_id + return f"{asset_id}USDT" + + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors + def get_product(self, asset_id: str) -> ProductInfo: + """ + Ottiene informazioni su un singolo prodotto. + + Args: + asset_id: ID dell'asset (es. "BTC") + + Returns: + Oggetto ProductInfo con le informazioni del prodotto + """ + symbol = self.__format_symbol(asset_id) + try: + ticker = self.client.get_symbol_ticker(symbol=symbol) + ticker_24h = self.client.get_ticker(symbol=symbol) + return ProductInfo.from_binance(ticker, ticker_24h) + except Exception as e: + print(f"Errore nel recupero del prodotto {asset_id}: {e}") + return ProductInfo(id=asset_id, symbol=asset_id) + + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors + def get_products(self, asset_ids: list[str]) -> list[ProductInfo]: + """ + Ottiene informazioni su più prodotti. + + Args: + asset_ids: Lista di ID degli asset + + Returns: + Lista di oggetti ProductInfo + """ + products = [] + for asset_id in asset_ids: + product = self.get_product(asset_id) + products.append(product) + return products + + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors + def get_all_products(self) -> list[ProductInfo]: + """ + Ottiene informazioni su tutti i prodotti disponibili. + + Returns: + Lista di oggetti ProductInfo per i principali asset + """ + # Per la versione pubblica, restituiamo solo i principali asset + major_assets = ["BTC", "ETH", "BNB", "ADA", "DOT", "LINK", "LTC", "XRP"] + return self.get_products(major_assets) + + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors + def get_historical_prices(self, asset_id: str = "BTC") -> list[Price]: + """ + Ottiene i prezzi storici per un asset. + + Args: + asset_id: ID dell'asset (default: "BTC") + + Returns: + Lista di oggetti Price con i dati storici + """ + symbol = self.__format_symbol(asset_id) + try: + # Ottieni candele degli ultimi 30 giorni + end_time = datetime.now() + start_time = end_time - timedelta(days=30) + + klines = self.client.get_historical_klines( + symbol, + Client.KLINE_INTERVAL_1DAY, + start_time.strftime("%d %b %Y %H:%M:%S"), + end_time.strftime("%d %b %Y %H:%M:%S") + ) + + prices = [] + for kline in klines: + price = Price( + open=float(kline[1]), + high=float(kline[2]), + low=float(kline[3]), + close=float(kline[4]), + volume=float(kline[5]), + time=str(datetime.fromtimestamp(kline[0] / 1000)) + ) + prices.append(price) + + return prices + except Exception as e: + print(f"Errore nel recupero dei prezzi storici per {asset_id}: {e}") + return [] + + def get_public_prices(self, symbols: Optional[list[str]] = None) -> Optional[Dict[str, Any]]: + """ + Ottiene i prezzi pubblici per i simboli specificati. + + Args: + symbols: Lista di simboli da recuperare (es. ["BTCUSDT", "ETHUSDT"]). + Se None, recupera BTC e ETH di default. + + Returns: + Dizionario con i prezzi e informazioni sulla fonte, o None in caso di errore. + """ + if symbols is None: + symbols = ["BTCUSDT", "ETHUSDT"] + + try: + prices = {} + for symbol in symbols: + ticker = self.client.get_symbol_ticker(symbol=symbol) + # Converte BTCUSDT -> BTC_USD per consistenza + clean_symbol = symbol.replace("USDT", "_USD").replace("BUSD", "_USD") + prices[clean_symbol] = float(ticker['price']) + + return { + **prices, + 'source': 'binance_public', + 'timestamp': self.client.get_server_time()['serverTime'] + } + except Exception as e: + print(f"Errore nel recupero dei prezzi pubblici: {e}") + return None + + def get_24hr_ticker(self, symbol: str) -> Optional[Dict[str, Any]]: + """ + Ottiene le statistiche 24h per un simbolo specifico. + + Args: + symbol: Simbolo del trading pair (es. "BTCUSDT") + + Returns: + Dizionario con le statistiche 24h o None in caso di errore. + """ + try: + ticker = self.client.get_ticker(symbol=symbol) + return { + 'symbol': ticker['symbol'], + 'price': float(ticker['lastPrice']), + 'price_change': float(ticker['priceChange']), + 'price_change_percent': float(ticker['priceChangePercent']), + 'high_24h': float(ticker['highPrice']), + 'low_24h': float(ticker['lowPrice']), + 'volume_24h': float(ticker['volume']), + 'source': 'binance_public' + } + except Exception as e: + print(f"Errore nel recupero del ticker 24h per {symbol}: {e}") + return None + + def get_exchange_info(self) -> Optional[Dict[str, Any]]: + """ + Ottiene informazioni generali sull'exchange. + + Returns: + Dizionario con informazioni sull'exchange o None in caso di errore. + """ + try: + info = self.client.get_exchange_info() + return { + 'timezone': info['timezone'], + 'server_time': info['serverTime'], + 'symbols_count': len(info['symbols']), + 'source': 'binance_public' + } + except Exception as e: + print(f"Errore nel recupero delle informazioni exchange: {e}") + return None + + +# Esempio di utilizzo +if __name__ == "__main__": + # Uso senza credenziali + public_agent = PublicBinanceAgent() + + # Ottieni prezzi di default (BTC e ETH) + public_prices = public_agent.get_public_prices() + print("Prezzi pubblici:", public_prices) + + # Ottieni statistiche 24h per BTC + btc_stats = public_agent.get_24hr_ticker("BTCUSDT") + print("Statistiche BTC 24h:", btc_stats) + + # Ottieni informazioni exchange + exchange_info = public_agent.get_exchange_info() + print("Info exchange:", exchange_info) \ No newline at end of file diff --git a/src/app/markets/coinbase.py b/src/app/markets/coinbase.py index aac556d..7d2b2d2 100644 --- a/src/app/markets/coinbase.py +++ b/src/app/markets/coinbase.py @@ -1,13 +1,21 @@ import os +from typing import Optional +from datetime import datetime, timedelta from coinbase.rest import RESTClient -from app.markets.base import ProductInfo, BaseWrapper, Price +from .base import ProductInfo, BaseWrapper, Price +from .error_handler import retry_on_failure, handle_api_errors, MarketAPIError, RateLimitError class CoinBaseWrapper(BaseWrapper): """ - Wrapper per le API di Coinbase. - La documentazione delle API è disponibile qui: https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/introduction + Wrapper per le API di Coinbase Advanced Trade. + + Implementa l'interfaccia BaseWrapper per fornire accesso unificato + ai dati di mercato di Coinbase tramite le API REST. + + La documentazione delle API è disponibile qui: + https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/introduction """ - def __init__(self, api_key:str = None, api_private_key:str = None, currency: str = "USD"): + def __init__(self, api_key: Optional[str] = None, api_private_key: Optional[str] = None, currency: str = "USD"): if api_key is None: api_key = os.getenv("COINBASE_API_KEY") assert api_key is not None, "API key is required" @@ -25,21 +33,49 @@ class CoinBaseWrapper(BaseWrapper): def __format(self, asset_id: str) -> str: return asset_id if '-' in asset_id else f"{asset_id}-{self.currency}" + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors def get_product(self, asset_id: str) -> ProductInfo: asset_id = self.__format(asset_id) asset = self.client.get_product(asset_id) return ProductInfo.from_coinbase(asset) + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors def get_products(self, asset_ids: list[str]) -> list[ProductInfo]: all_asset_ids = [self.__format(asset_id) for asset_id in asset_ids] - assets = self.client.get_products(all_asset_ids) - return [ProductInfo.from_coinbase(asset) for asset in assets.products] + assets = self.client.get_products(product_ids=all_asset_ids) + if assets.products: + return [ProductInfo.from_coinbase_product(asset) for asset in assets.products] + return [] + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors def get_all_products(self) -> list[ProductInfo]: assets = self.client.get_products() - return [ProductInfo.from_coinbase(asset) for asset in assets.products] + if assets.products: + return [ProductInfo.from_coinbase_product(asset) for asset in assets.products] + return [] + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors def get_historical_prices(self, asset_id: str = "BTC") -> list[Price]: asset_id = self.__format(asset_id) - data = self.client.get_candles(product_id=asset_id) - return [Price.from_coinbase(candle) for candle in data.candles] + # Get last 14 days of hourly data (14*24 = 336 candles, within 350 limit) + end_time = datetime.now() + start_time = end_time - timedelta(days=14) + + # Convert to UNIX timestamps as strings (required by Coinbase API) + start_timestamp = str(int(start_time.timestamp())) + end_timestamp = str(int(end_time.timestamp())) + + data = self.client.get_candles( + product_id=asset_id, + start=start_timestamp, + end=end_timestamp, + granularity="ONE_HOUR", + limit=350 # Explicitly set the limit + ) + if data.candles: + return [Price.from_coinbase(candle) for candle in data.candles] + return [] diff --git a/src/app/markets/cryptocompare.py b/src/app/markets/cryptocompare.py index 188a2c2..5b84843 100644 --- a/src/app/markets/cryptocompare.py +++ b/src/app/markets/cryptocompare.py @@ -1,6 +1,8 @@ import os import requests -from app.markets.base import ProductInfo, BaseWrapper, Price +from typing import Optional, Dict, Any +from .base import ProductInfo, BaseWrapper, Price +from .error_handler import retry_on_failure, handle_api_errors, MarketAPIError BASE_URL = "https://min-api.cryptocompare.com" @@ -10,7 +12,7 @@ class CryptoCompareWrapper(BaseWrapper): La documentazione delle API è disponibile qui: https://developers.coindesk.com/documentation/legacy/Price/SingleSymbolPriceEndpoint !!ATTENZIONE!! sembra essere una API legacy e potrebbe essere deprecata in futuro. """ - def __init__(self, api_key:str = None, currency:str='USD'): + def __init__(self, api_key: Optional[str] = None, currency: str = 'USD'): if api_key is None: api_key = os.getenv("CRYPTOCOMPARE_API_KEY") assert api_key is not None, "API key is required" @@ -18,7 +20,7 @@ class CryptoCompareWrapper(BaseWrapper): self.api_key = api_key self.currency = currency - def __request(self, endpoint: str, params: dict = None) -> dict: + def __request(self, endpoint: str, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: if params is None: params = {} params['api_key'] = self.api_key @@ -26,6 +28,8 @@ class CryptoCompareWrapper(BaseWrapper): response = requests.get(f"{BASE_URL}{endpoint}", params=params) return response.json() + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors def get_product(self, asset_id: str) -> ProductInfo: response = self.__request("/data/pricemultifull", params = { "fsyms": asset_id, @@ -34,6 +38,8 @@ class CryptoCompareWrapper(BaseWrapper): data = response.get('RAW', {}).get(asset_id, {}).get(self.currency, {}) return ProductInfo.from_cryptocompare(data) + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors def get_products(self, asset_ids: list[str]) -> list[ProductInfo]: response = self.__request("/data/pricemultifull", params = { "fsyms": ",".join(asset_ids), @@ -46,10 +52,37 @@ class CryptoCompareWrapper(BaseWrapper): assets.append(ProductInfo.from_cryptocompare(asset_data)) return assets + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors def get_all_products(self) -> list[ProductInfo]: - raise NotImplementedError("CryptoCompare does not support fetching all assets") + """ + Workaround per CryptoCompare: utilizza una lista predefinita di asset popolari + poiché l'API non fornisce un endpoint per recuperare tutti i prodotti. + """ + # Lista di asset popolari supportati da CryptoCompare + popular_assets = [ + "BTC", "ETH", "ADA", "DOT", "LINK", "LTC", "XRP", "BCH", "BNB", "SOL", + "MATIC", "AVAX", "ATOM", "UNI", "DOGE", "SHIB", "TRX", "ETC", "FIL", "XLM" + ] + + try: + # Utilizza get_products per recuperare i dati di tutti gli asset popolari + return self.get_products(popular_assets) + except Exception as e: + # Fallback: prova con un set ridotto di asset principali + main_assets = ["BTC", "ETH", "ADA", "DOT", "LINK"] + try: + return self.get_products(main_assets) + except Exception as fallback_error: + # Se anche il fallback fallisce, solleva l'errore originale con informazioni aggiuntive + raise NotImplementedError( + f"CryptoCompare get_all_products() workaround failed. " + f"Original error: {str(e)}, Fallback error: {str(fallback_error)}" + ) - def get_historical_prices(self, asset_id: str, day_back: int = 10) -> list[dict]: + @retry_on_failure(max_retries=3, delay=1.0) + @handle_api_errors + def get_historical_prices(self, asset_id: str = "BTC", day_back: int = 10) -> list[Price]: assert day_back <= 30, "day_back should be less than or equal to 30" response = self.__request("/data/v2/histohour", params = { "fsym": asset_id, diff --git a/src/app/markets/error_handler.py b/src/app/markets/error_handler.py new file mode 100644 index 0000000..38aa47f --- /dev/null +++ b/src/app/markets/error_handler.py @@ -0,0 +1,236 @@ +""" +Modulo per la gestione robusta degli errori nei market providers. + +Fornisce decoratori e utilità per: +- Retry automatico con backoff esponenziale +- Logging standardizzato degli errori +- Gestione di timeout e rate limiting +- Fallback tra provider multipli +""" + +import time +import logging +from functools import wraps +from typing import Any, Callable, Optional, Type, Union, List +from requests.exceptions import RequestException, Timeout, ConnectionError +from binance.exceptions import BinanceAPIException, BinanceRequestException + +# Configurazione logging +logger = logging.getLogger(__name__) + +class MarketAPIError(Exception): + """Eccezione base per errori delle API di mercato.""" + pass + +class RateLimitError(MarketAPIError): + """Eccezione per errori di rate limiting.""" + pass + +class AuthenticationError(MarketAPIError): + """Eccezione per errori di autenticazione.""" + pass + +class DataNotFoundError(MarketAPIError): + """Eccezione quando i dati richiesti non sono disponibili.""" + pass + +def retry_on_failure( + max_retries: int = 3, + delay: float = 1.0, + backoff_factor: float = 2.0, + exceptions: tuple = (RequestException, BinanceAPIException, BinanceRequestException) +) -> Callable: + """ + Decoratore per retry automatico con backoff esponenziale. + + Args: + max_retries: Numero massimo di tentativi + delay: Delay iniziale in secondi + backoff_factor: Fattore di moltiplicazione per il delay + exceptions: Tuple di eccezioni da catturare per il retry + + Returns: + Decoratore per la funzione + """ + def decorator(func: Callable) -> Callable: + @wraps(func) + def wrapper(*args, **kwargs) -> Any: + last_exception = None + current_delay = delay + + for attempt in range(max_retries + 1): + try: + return func(*args, **kwargs) + except exceptions as e: + last_exception = e + + if attempt == max_retries: + logger.error( + f"Function {func.__name__} failed after {max_retries + 1} attempts. " + f"Last error: {str(e)}" + ) + raise MarketAPIError(f"Max retries exceeded: {str(e)}") from e + + logger.warning( + f"Attempt {attempt + 1}/{max_retries + 1} failed for {func.__name__}: {str(e)}. " + f"Retrying in {current_delay:.1f}s..." + ) + + time.sleep(current_delay) + current_delay *= backoff_factor + except Exception as e: + # Per eccezioni non previste, non fare retry + logger.error(f"Unexpected error in {func.__name__}: {str(e)}") + raise + + # Questo non dovrebbe mai essere raggiunto + if last_exception: + raise last_exception + else: + raise MarketAPIError("Unknown error occurred") + + return wrapper + return decorator + +def handle_api_errors(func: Callable) -> Callable: + """ + Decoratore per gestione standardizzata degli errori API. + + Converte errori specifici dei provider in eccezioni standardizzate. + """ + @wraps(func) + def wrapper(*args, **kwargs) -> Any: + try: + return func(*args, **kwargs) + except BinanceAPIException as e: + if e.code == -1021: # Timestamp error + raise MarketAPIError(f"Binance timestamp error: {e.message}") + elif e.code == -1003: # Rate limit + raise RateLimitError(f"Binance rate limit exceeded: {e.message}") + elif e.code in [-2014, -2015]: # API key errors + raise AuthenticationError(f"Binance authentication error: {e.message}") + else: + raise MarketAPIError(f"Binance API error [{e.code}]: {e.message}") + except ConnectionError as e: + raise MarketAPIError(f"Connection error: {str(e)}") + except Timeout as e: + raise MarketAPIError(f"Request timeout: {str(e)}") + except RequestException as e: + raise MarketAPIError(f"Request error: {str(e)}") + except Exception as e: + logger.error(f"Unexpected error in {func.__name__}: {str(e)}") + raise MarketAPIError(f"Unexpected error: {str(e)}") from e + + return wrapper + +def safe_execute( + func: Callable, + default_value: Any = None, + log_errors: bool = True, + error_message: Optional[str] = None +) -> Any: + """ + Esegue una funzione in modo sicuro, restituendo un valore di default in caso di errore. + + Args: + func: Funzione da eseguire + default_value: Valore da restituire in caso di errore + log_errors: Se loggare gli errori + error_message: Messaggio di errore personalizzato + + Returns: + Risultato della funzione o valore di default + """ + try: + return func() + except Exception as e: + if log_errors: + message = error_message or f"Error executing {func.__name__}" + logger.warning(f"{message}: {str(e)}") + return default_value + +class ProviderFallback: + """ + Classe per gestire il fallback tra provider multipli. + """ + + def __init__(self, providers: List[Any]): + """ + Inizializza con una lista di provider ordinati per priorità. + + Args: + providers: Lista di provider ordinati per priorità + """ + self.providers = providers + + def execute_with_fallback( + self, + method_name: str, + *args, + **kwargs + ) -> Any: + """ + Esegue un metodo su tutti i provider fino a trovarne uno che funziona. + + Args: + method_name: Nome del metodo da chiamare + *args: Argomenti posizionali + **kwargs: Argomenti nominali + + Returns: + Risultato del primo provider che funziona + + Raises: + MarketAPIError: Se tutti i provider falliscono + """ + last_error = None + + for i, provider in enumerate(self.providers): + try: + if hasattr(provider, method_name): + method = getattr(provider, method_name) + result = method(*args, **kwargs) + + if i > 0: # Se non è il primo provider + logger.info(f"Fallback successful: used provider {type(provider).__name__}") + + return result + else: + logger.warning(f"Provider {type(provider).__name__} doesn't have method {method_name}") + continue + + except Exception as e: + last_error = e + logger.warning( + f"Provider {type(provider).__name__} failed for {method_name}: {str(e)}" + ) + continue + + # Se arriviamo qui, tutti i provider hanno fallito + raise MarketAPIError( + f"All providers failed for method {method_name}. Last error: {str(last_error)}" + ) + +def validate_response_data(data: Any, required_fields: Optional[List[str]] = None) -> bool: + """ + Valida che i dati di risposta contengano i campi richiesti. + + Args: + data: Dati da validare + required_fields: Lista di campi richiesti + + Returns: + True se i dati sono validi, False altrimenti + """ + if data is None: + return False + + if required_fields is None: + return True + + if isinstance(data, dict): + return all(field in data for field in required_fields) + elif hasattr(data, '__dict__'): + return all(hasattr(data, field) for field in required_fields) + + return False \ No newline at end of file diff --git a/tests/agents/test_market.py b/tests/agents/test_market.py index 56931b3..9fd5732 100644 --- a/tests/agents/test_market.py +++ b/tests/agents/test_market.py @@ -1,146 +1,596 @@ +""" +Test suite completo per il sistema di mercato. + +Questo modulo testa approfonditamente tutte le implementazioni di BaseWrapper +e verifica la conformità all'interfaccia definita in base.py. +""" + import os import pytest -from app.agents.market import MarketToolkit -from app.markets.base import BaseWrapper +from unittest.mock import Mock, patch, MagicMock +from typing import Type, List + +# Import delle classi da testare +from app.markets.base import BaseWrapper, ProductInfo, Price from app.markets.coinbase import CoinBaseWrapper from app.markets.cryptocompare import CryptoCompareWrapper +from app.markets.binance import BinanceWrapper +from app.markets.binance_public import PublicBinanceAgent from app.markets import MarketAPIs -class TestMarketSystem: - """Test suite per il sistema di mercato (wrappers + toolkit)""" - @pytest.fixture(scope="class") - def market_wrapper(self) -> BaseWrapper: - return MarketAPIs("USD") +class TestBaseWrapperInterface: + """Test per verificare che tutte le implementazioni rispettino l'interfaccia BaseWrapper.""" + + def test_all_wrappers_extend_basewrapper(self): + """Verifica che tutte le classi wrapper estendano BaseWrapper.""" + wrapper_classes = [ + CoinBaseWrapper, + CryptoCompareWrapper, + BinanceWrapper, + PublicBinanceAgent, + MarketAPIs + ] + + for wrapper_class in wrapper_classes: + assert issubclass(wrapper_class, BaseWrapper), f"{wrapper_class.__name__} deve estendere BaseWrapper" + + def test_all_wrappers_implement_required_methods(self): + """Verifica che tutte le classi implementino i metodi richiesti dall'interfaccia.""" + wrapper_classes = [ + CoinBaseWrapper, + CryptoCompareWrapper, + BinanceWrapper, + PublicBinanceAgent, + MarketAPIs + ] + + required_methods = ['get_product', 'get_products', 'get_all_products', 'get_historical_prices'] + + for wrapper_class in wrapper_classes: + for method in required_methods: + assert hasattr(wrapper_class, method), f"{wrapper_class.__name__} deve implementare {method}" + assert callable(getattr(wrapper_class, method)), f"{method} deve essere callable in {wrapper_class.__name__}" - def test_wrapper_initialization(self, market_wrapper): - assert market_wrapper is not None - assert hasattr(market_wrapper, 'get_product') - assert hasattr(market_wrapper, 'get_products') - assert hasattr(market_wrapper, 'get_all_products') - assert hasattr(market_wrapper, 'get_historical_prices') - def test_providers_configuration(self): - available_providers = [] - if os.getenv('CDP_API_KEY_NAME') and os.getenv('CDP_API_PRIVATE_KEY'): - available_providers.append('coinbase') - if os.getenv('CRYPTOCOMPARE_API_KEY'): - available_providers.append('cryptocompare') - assert len(available_providers) > 0 +class TestProductInfoModel: + """Test per la classe ProductInfo e i suoi metodi di conversione.""" + + def test_productinfo_initialization(self): + """Test inizializzazione di ProductInfo.""" + product = ProductInfo() + assert product.id == "" + assert product.symbol == "" + assert product.price == 0.0 + assert product.volume_24h == 0.0 + assert product.status == "" + assert product.quote_currency == "" + + def test_productinfo_with_data(self): + """Test ProductInfo con dati specifici.""" + product = ProductInfo( + id="BTC-USD", + symbol="BTC", + price=50000.0, + volume_24h=1000000.0, + status="TRADING", + quote_currency="USD" + ) + assert product.id == "BTC-USD" + assert product.symbol == "BTC" + assert product.price == 50000.0 + assert product.volume_24h == 1000000.0 + assert product.status == "TRADING" + assert product.quote_currency == "USD" + + def test_productinfo_from_cryptocompare(self): + """Test conversione da dati CryptoCompare.""" + mock_data = { + 'FROMSYMBOL': 'BTC', + 'TOSYMBOL': 'USD', + 'PRICE': 50000.0, + 'VOLUME24HOUR': 1000000.0 + } + + product = ProductInfo.from_cryptocompare(mock_data) + assert product.id == "BTC-USD" + assert product.symbol == "BTC" + assert product.price == 50000.0 + assert product.volume_24h == 1000000.0 + assert product.status == "" + + def test_productinfo_from_binance(self): + """Test conversione da dati Binance.""" + ticker_data = {'symbol': 'BTCUSDT', 'price': '50000.0'} + ticker_24h_data = {'volume': '1000000.0'} + + product = ProductInfo.from_binance(ticker_data, ticker_24h_data) + assert product.id == "BTCUSDT" + assert product.symbol == "BTC" + assert product.price == 50000.0 + assert product.volume_24h == 1000000.0 + assert product.status == "TRADING" + assert product.quote_currency == "USDT" - def test_wrapper_capabilities(self, market_wrapper): - capabilities = [] - if hasattr(market_wrapper, 'get_product'): - capabilities.append('single_product') - if hasattr(market_wrapper, 'get_products'): - capabilities.append('multiple_products') - if hasattr(market_wrapper, 'get_historical_prices'): - capabilities.append('historical_data') - assert len(capabilities) > 0 - def test_market_data_retrieval(self, market_wrapper): - btc_product = market_wrapper.get_product("BTC") - assert btc_product is not None - assert hasattr(btc_product, 'symbol') - assert hasattr(btc_product, 'price') - assert btc_product.price > 0 +class TestPriceModel: + """Test per la classe Price e i suoi metodi di conversione.""" + + def test_price_initialization(self): + """Test inizializzazione di Price.""" + price = Price() + assert price.high == 0.0 + assert price.low == 0.0 + assert price.open == 0.0 + assert price.close == 0.0 + assert price.volume == 0.0 + assert price.time == "" + + def test_price_with_data(self): + """Test Price con dati specifici.""" + price = Price( + high=51000.0, + low=49000.0, + open=50000.0, + close=50500.0, + volume=1000.0, + time="2024-01-01T00:00:00Z" + ) + assert price.high == 51000.0 + assert price.low == 49000.0 + assert price.open == 50000.0 + assert price.close == 50500.0 + assert price.volume == 1000.0 + assert price.time == "2024-01-01T00:00:00Z" + + def test_price_from_cryptocompare(self): + """Test conversione da dati CryptoCompare.""" + mock_data = { + 'high': 51000.0, + 'low': 49000.0, + 'open': 50000.0, + 'close': 50500.0, + 'volumeto': 1000.0, + 'time': 1704067200 + } + + price = Price.from_cryptocompare(mock_data) + assert price.high == 51000.0 + assert price.low == 49000.0 + assert price.open == 50000.0 + assert price.close == 50500.0 + assert price.volume == 1000.0 + assert price.time == "1704067200" - def test_market_toolkit_integration(self, market_wrapper): - try: - toolkit = MarketToolkit() - assert toolkit is not None - assert hasattr(toolkit, 'market_agent') - assert toolkit.market_api is not None - - tools = toolkit.tools - assert len(tools) > 0 - - except Exception as e: - print(f"MarketToolkit test failed: {e}") - # Non fail completamente - il toolkit potrebbe avere dipendenze specifiche +class TestCoinBaseWrapper: + """Test specifici per CoinBaseWrapper.""" + @pytest.mark.skipif( - not os.getenv('CRYPTOCOMPARE_API_KEY'), - reason="CRYPTOCOMPARE_API_KEY not configured" + not (os.getenv('COINBASE_API_KEY') and os.getenv('COINBASE_API_SECRET')), + reason="Credenziali Coinbase non configurate" ) - def test_cryptocompare_wrapper(self): - try: - api_key = os.getenv('CRYPTOCOMPARE_API_KEY') - wrapper = CryptoCompareWrapper(api_key=api_key, currency="USD") + def test_coinbase_initialization_with_env_vars(self): + """Test inizializzazione con variabili d'ambiente.""" + wrapper = CoinBaseWrapper(currency="USD") + assert wrapper.currency == "USD" + assert wrapper.client is not None + + @patch.dict(os.environ, {}, clear=True) + def test_coinbase_initialization_with_params(self): + """Test inizializzazione con parametri espliciti quando non ci sono variabili d'ambiente.""" + with pytest.raises(AssertionError, match="API key is required"): + CoinBaseWrapper(api_key=None, api_private_key=None) + + @patch('app.markets.coinbase.RESTClient') + def test_coinbase_asset_formatting_behavior(self, mock_client): + """Test comportamento di formattazione asset ID attraverso get_product.""" + mock_response = Mock() + mock_response.product_id = "BTC-USD" + mock_response.base_currency_id = "BTC" + mock_response.price = "50000.0" + mock_response.volume_24h = "1000000.0" + mock_response.status = "TRADING" + + mock_client_instance = Mock() + mock_client_instance.get_product.return_value = mock_response + mock_client.return_value = mock_client_instance + + wrapper = CoinBaseWrapper(api_key="test", api_private_key="test") + + # Test che entrambi i formati funzionino + wrapper.get_product("BTC") + wrapper.get_product("BTC-USD") + + # Verifica che get_product sia stato chiamato con il formato corretto + assert mock_client_instance.get_product.call_count == 2 + + @patch('app.markets.coinbase.RESTClient') + def test_coinbase_get_product(self, mock_client): + """Test get_product con mock.""" + mock_response = Mock() + mock_response.product_id = "BTC-USD" + mock_response.base_currency_id = "BTC" + mock_response.price = "50000.0" + mock_response.volume_24h = "1000000.0" + mock_response.status = "TRADING" + + mock_client_instance = Mock() + mock_client_instance.get_product.return_value = mock_response + mock_client.return_value = mock_client_instance + + wrapper = CoinBaseWrapper(api_key="test", api_private_key="test") + product = wrapper.get_product("BTC") + + assert isinstance(product, ProductInfo) + assert product.symbol == "BTC" + mock_client_instance.get_product.assert_called_once_with("BTC-USD") - btc_product = wrapper.get_product("BTC") - assert btc_product is not None - assert btc_product.symbol == "BTC" - assert btc_product.price > 0 - - products = wrapper.get_products(["BTC", "ETH"]) - assert isinstance(products, list) - assert len(products) > 0 - - for product in products: - if product.symbol in ["BTC", "ETH"]: - assert product.price > 0 - - except Exception as e: - print(f"CryptoCompare test failed: {e}") - # Non fail il test se c'è un errore di rete o API +class TestCryptoCompareWrapper: + """Test specifici per CryptoCompareWrapper.""" + @pytest.mark.skipif( - not (os.getenv('CDP_API_KEY_NAME') and os.getenv('CDP_API_PRIVATE_KEY')), - reason="Coinbase credentials not configured" + not os.getenv('CRYPTOCOMPARE_API_KEY'), + reason="CRYPTOCOMPARE_API_KEY non configurata" ) - def test_coinbase_wrapper(self): - try: - api_key = os.getenv('CDP_API_KEY_NAME') - api_secret = os.getenv('CDP_API_PRIVATE_KEY') - wrapper = CoinBaseWrapper( - api_key=api_key, - api_private_key=api_secret, - currency="USD" - ) + def test_cryptocompare_initialization_with_env_var(self): + """Test inizializzazione con variabile d'ambiente.""" + wrapper = CryptoCompareWrapper(currency="USD") + assert wrapper.currency == "USD" + assert wrapper.api_key is not None + + def test_cryptocompare_initialization_with_param(self): + """Test inizializzazione con parametro esplicito.""" + wrapper = CryptoCompareWrapper(api_key="test_key", currency="EUR") + assert wrapper.api_key == "test_key" + assert wrapper.currency == "EUR" + + @patch('app.markets.cryptocompare.requests.get') + def test_cryptocompare_get_product(self, mock_get): + """Test get_product con mock.""" + mock_response = Mock() + mock_response.json.return_value = { + 'RAW': { + 'BTC': { + 'USD': { + 'FROMSYMBOL': 'BTC', + 'TOSYMBOL': 'USD', + 'PRICE': 50000.0, + 'VOLUME24HOUR': 1000000.0 + } + } + } + } + mock_response.raise_for_status.return_value = None + mock_get.return_value = mock_response + + wrapper = CryptoCompareWrapper(api_key="test_key") + product = wrapper.get_product("BTC") + + assert isinstance(product, ProductInfo) + assert product.symbol == "BTC" + assert product.price == 50000.0 + + def test_cryptocompare_get_all_products_workaround(self): + """Test che get_all_products funzioni con il workaround implementato.""" + wrapper = CryptoCompareWrapper(api_key="test_key") + # Il metodo ora dovrebbe restituire una lista di ProductInfo invece di sollevare NotImplementedError + products = wrapper.get_all_products() + assert isinstance(products, list) + # Verifica che la lista non sia vuota (dovrebbe contenere almeno alcuni asset popolari) + assert len(products) > 0 + # Verifica che ogni elemento sia un ProductInfo + for product in products: + assert isinstance(product, ProductInfo) - btc_product = wrapper.get_product("BTC") - assert btc_product is not None - assert btc_product.symbol == "BTC" - assert btc_product.price > 0 - products = wrapper.get_products(["BTC", "ETH"]) - assert isinstance(products, list) - assert len(products) > 0 +class TestBinanceWrapper: + """Test specifici per BinanceWrapper.""" + + def test_binance_initialization_without_credentials(self): + """Test che l'inizializzazione fallisca senza credenziali.""" + # Assicuriamoci che le variabili d'ambiente siano vuote per questo test + with patch.dict(os.environ, {}, clear=True): + with pytest.raises(AssertionError, match="API key is required"): + BinanceWrapper(api_key=None, api_secret="test") + + with pytest.raises(AssertionError, match="API secret is required"): + BinanceWrapper(api_key="test", api_secret=None) + + @patch('app.markets.binance.Client') + def test_binance_symbol_formatting_behavior(self, mock_client): + """Test comportamento di formattazione simbolo attraverso get_product.""" + mock_client_instance = Mock() + mock_client_instance.get_symbol_ticker.return_value = { + 'symbol': 'BTCUSDT', + 'price': '50000.0' + } + mock_client_instance.get_ticker.return_value = { + 'volume': '1000000.0' + } + mock_client.return_value = mock_client_instance + + wrapper = BinanceWrapper(api_key="test", api_secret="test") + + # Test che entrambi i formati funzionino + wrapper.get_product("BTC") + wrapper.get_product("BTCUSDT") + + # Verifica che i metodi siano stati chiamati + assert mock_client_instance.get_symbol_ticker.call_count == 2 + + @patch('app.markets.binance.Client') + def test_binance_get_product(self, mock_client): + """Test get_product con mock.""" + mock_client_instance = Mock() + mock_client_instance.get_symbol_ticker.return_value = { + 'symbol': 'BTCUSDT', + 'price': '50000.0' + } + mock_client_instance.get_ticker.return_value = { + 'volume': '1000000.0' + } + mock_client.return_value = mock_client_instance + + wrapper = BinanceWrapper(api_key="test", api_secret="test") + product = wrapper.get_product("BTC") + + assert isinstance(product, ProductInfo) + assert product.symbol == "BTC" + assert product.price == 50000.0 - except Exception as e: - print(f"Coinbase test failed: {e}") - # Non fail il test se c'è un errore di credenziali o rete - def test_provider_selection_mechanism(self): - potential_providers = 0 - if os.getenv('CDP_API_KEY_NAME') and os.getenv('CDP_API_PRIVATE_KEY'): - potential_providers += 1 - if os.getenv('CRYPTOCOMPARE_API_KEY'): - potential_providers += 1 +class TestPublicBinanceAgent: + """Test specifici per PublicBinanceAgent.""" + + @patch('app.markets.binance_public.Client') + def test_public_binance_initialization(self, mock_client): + """Test inizializzazione senza credenziali.""" + agent = PublicBinanceAgent() + assert agent.client is not None + mock_client.assert_called_once_with() + + @patch('app.markets.binance_public.Client') + def test_public_binance_symbol_formatting_behavior(self, mock_client): + """Test comportamento di formattazione simbolo attraverso get_product.""" + mock_client_instance = Mock() + mock_client_instance.get_symbol_ticker.return_value = { + 'symbol': 'BTCUSDT', + 'price': '50000.0' + } + mock_client_instance.get_ticker.return_value = { + 'volume': '1000000.0' + } + mock_client.return_value = mock_client_instance + + agent = PublicBinanceAgent() + + # Test che entrambi i formati funzionino + agent.get_product("BTC") + agent.get_product("BTCUSDT") + + # Verifica che i metodi siano stati chiamati + assert mock_client_instance.get_symbol_ticker.call_count == 2 + + @patch('app.markets.binance_public.Client') + def test_public_binance_get_product(self, mock_client): + """Test get_product con mock.""" + mock_client_instance = Mock() + mock_client_instance.get_symbol_ticker.return_value = { + 'symbol': 'BTCUSDT', + 'price': '50000.0' + } + mock_client_instance.get_ticker.return_value = { + 'volume': '1000000.0' + } + mock_client.return_value = mock_client_instance + + agent = PublicBinanceAgent() + product = agent.get_product("BTC") + + assert isinstance(product, ProductInfo) + assert product.symbol == "BTC" + assert product.price == 50000.0 + + @patch('app.markets.binance_public.Client') + def test_public_binance_get_all_products(self, mock_client): + """Test get_all_products restituisce asset principali.""" + mock_client_instance = Mock() + mock_client_instance.get_symbol_ticker.return_value = { + 'symbol': 'BTCUSDT', + 'price': '50000.0' + } + mock_client_instance.get_ticker.return_value = { + 'volume': '1000000.0' + } + mock_client.return_value = mock_client_instance + + agent = PublicBinanceAgent() + products = agent.get_all_products() + + assert isinstance(products, list) + assert len(products) == 8 # Numero di asset principali definiti + for product in products: + assert isinstance(product, ProductInfo) + + @patch('app.markets.binance_public.Client') + def test_public_binance_get_public_prices(self, mock_client): + """Test metodo specifico get_public_prices.""" + mock_client_instance = Mock() + mock_client_instance.get_symbol_ticker.return_value = {'price': '50000.0'} + mock_client_instance.get_server_time.return_value = {'serverTime': 1704067200000} + mock_client.return_value = mock_client_instance + + agent = PublicBinanceAgent() + prices = agent.get_public_prices(["BTCUSDT"]) + + assert isinstance(prices, dict) + assert 'BTC_USD' in prices + assert prices['BTC_USD'] == 50000.0 + assert 'source' in prices + assert prices['source'] == 'binance_public' - if potential_providers == 0: - with pytest.raises(AssertionError, match="No valid API keys"): - MarketAPIs.get_list_available_market_apis() - else: - wrapper = MarketAPIs("USD") - assert wrapper is not None - assert hasattr(wrapper, 'get_product') - def test_error_handling(self, market_wrapper): - try: - fake_product = market_wrapper.get_product("NONEXISTENT_CRYPTO_SYMBOL_12345") - assert fake_product is None or fake_product.price == 0 - except Exception as e: - pass +class TestMarketAPIs: + """Test per la classe MarketAPIs che aggrega i wrapper.""" + + def test_market_apis_initialization_no_providers(self): + """Test che l'inizializzazione fallisca senza provider disponibili.""" + with patch.dict(os.environ, {}, clear=True): + with pytest.raises(AssertionError, match="No market API keys"): + MarketAPIs("USD") + + @patch('app.markets.CoinBaseWrapper') + def test_market_apis_with_coinbase_only(self, mock_coinbase): + """Test con solo Coinbase disponibile.""" + mock_instance = Mock() + mock_coinbase.return_value = mock_instance + + with patch('app.markets.CryptoCompareWrapper', side_effect=Exception("No API key")): + apis = MarketAPIs("USD") + assert len(apis.wrappers) == 1 + assert apis.wrappers[0] == mock_instance + + @patch('app.markets.CoinBaseWrapper') + @patch('app.markets.CryptoCompareWrapper') + def test_market_apis_delegation(self, mock_crypto, mock_coinbase): + """Test che i metodi vengano delegati al primo wrapper disponibile.""" + mock_coinbase_instance = Mock() + mock_crypto_instance = Mock() + mock_coinbase.return_value = mock_coinbase_instance + mock_crypto.return_value = mock_crypto_instance + + apis = MarketAPIs("USD") + + # Test delegazione get_product + apis.get_product("BTC") + mock_coinbase_instance.get_product.assert_called_once_with("BTC") + + # Test delegazione get_products + apis.get_products(["BTC", "ETH"]) + mock_coinbase_instance.get_products.assert_called_once_with(["BTC", "ETH"]) + + # Test delegazione get_all_products + apis.get_all_products() + mock_coinbase_instance.get_all_products.assert_called_once() + + # Test delegazione get_historical_prices + apis.get_historical_prices("BTC") + mock_coinbase_instance.get_historical_prices.assert_called_once_with("BTC") - try: - empty_products = market_wrapper.get_products([]) - assert isinstance(empty_products, list) - except Exception as e: - pass - def test_wrapper_currency_support(self, market_wrapper): - assert hasattr(market_wrapper, 'currency') - assert isinstance(market_wrapper.currency, str) - assert len(market_wrapper.currency) >= 3 # USD, EUR, etc. +class TestErrorHandling: + """Test per la gestione degli errori in tutti i wrapper.""" + + @patch('app.markets.binance_public.Client') + def test_public_binance_error_handling(self, mock_client): + """Test gestione errori in PublicBinanceAgent.""" + mock_client_instance = Mock() + mock_client_instance.get_symbol_ticker.side_effect = Exception("API Error") + mock_client.return_value = mock_client_instance + + agent = PublicBinanceAgent() + product = agent.get_product("INVALID") + + # Dovrebbe restituire un ProductInfo vuoto invece di sollevare eccezione + assert isinstance(product, ProductInfo) + assert product.id == "INVALID" + assert product.symbol == "INVALID" + + @patch('app.markets.cryptocompare.requests.get') + def test_cryptocompare_network_error(self, mock_get): + """Test gestione errori di rete in CryptoCompareWrapper.""" + mock_get.side_effect = Exception("Network Error") + + wrapper = CryptoCompareWrapper(api_key="test") + + with pytest.raises(Exception): + wrapper.get_product("BTC") + + @patch('app.markets.binance.Client') + def test_binance_api_error_in_get_products(self, mock_client): + """Test gestione errori in BinanceWrapper.get_products.""" + mock_client_instance = Mock() + mock_client_instance.get_symbol_ticker.side_effect = Exception("API Error") + mock_client.return_value = mock_client_instance + + wrapper = BinanceWrapper(api_key="test", api_secret="test") + products = wrapper.get_products(["BTC", "ETH"]) + + # Dovrebbe restituire lista vuota invece di sollevare eccezione + assert isinstance(products, list) + assert len(products) == 0 + + +class TestIntegrationScenarios: + """Test di integrazione per scenari reali.""" + + def test_wrapper_method_signatures(self): + """Verifica che tutti i wrapper abbiano le stesse signature dei metodi.""" + wrapper_classes = [CoinBaseWrapper, CryptoCompareWrapper, BinanceWrapper, PublicBinanceAgent] + + for wrapper_class in wrapper_classes: + # Verifica get_product + assert hasattr(wrapper_class, 'get_product') + + # Verifica get_products + assert hasattr(wrapper_class, 'get_products') + + # Verifica get_all_products + assert hasattr(wrapper_class, 'get_all_products') + + # Verifica get_historical_prices + assert hasattr(wrapper_class, 'get_historical_prices') + + def test_productinfo_consistency(self): + """Test che tutti i metodi from_* di ProductInfo restituiscano oggetti consistenti.""" + # Test from_cryptocompare + crypto_data = { + 'FROMSYMBOL': 'BTC', + 'TOSYMBOL': 'USD', + 'PRICE': 50000.0, + 'VOLUME24HOUR': 1000000.0 + } + crypto_product = ProductInfo.from_cryptocompare(crypto_data) + + # Test from_binance + binance_ticker = {'symbol': 'BTCUSDT', 'price': '50000.0'} + binance_24h = {'volume': '1000000.0'} + binance_product = ProductInfo.from_binance(binance_ticker, binance_24h) + + # Verifica che entrambi abbiano gli stessi campi + assert hasattr(crypto_product, 'id') + assert hasattr(crypto_product, 'symbol') + assert hasattr(crypto_product, 'price') + assert hasattr(crypto_product, 'volume_24h') + + assert hasattr(binance_product, 'id') + assert hasattr(binance_product, 'symbol') + assert hasattr(binance_product, 'price') + assert hasattr(binance_product, 'volume_24h') + + def test_price_consistency(self): + """Test che tutti i metodi from_* di Price restituiscano oggetti consistenti.""" + # Test from_cryptocompare + crypto_data = { + 'high': 51000.0, + 'low': 49000.0, + 'open': 50000.0, + 'close': 50500.0, + 'volumeto': 1000.0, + 'time': 1704067200 + } + crypto_price = Price.from_cryptocompare(crypto_data) + + # Verifica che abbia tutti i campi richiesti + assert hasattr(crypto_price, 'high') + assert hasattr(crypto_price, 'low') + assert hasattr(crypto_price, 'open') + assert hasattr(crypto_price, 'close') + assert hasattr(crypto_price, 'volume') + assert hasattr(crypto_price, 'time') + + +if __name__ == "__main__": + pytest.main([__file__, "-v"]) From c17a948ae091fb41a8bd8c8aacdd8123e4fdbb9a Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Sep 2025 00:34:07 +0200 Subject: [PATCH 06/22] Refactor news API integration to use NewsApiWrapper and GnewsWrapper; add tests for Gnews API functionality --- demos/news_api.py | 4 +-- pyproject.toml | 3 +- src/app/news/__init__.py | 5 +-- src/app/news/base.py | 6 ++++ src/app/news/gnews_api.py | 31 +++++++++++++++++ src/app/news/news_api.py | 17 +++++++--- tests/api/test_gnews_api.py | 34 +++++++++++++++++++ tests/api/test_news_api.py | 22 +++++++++++-- tests/conftest.py | 2 -- uv.lock | 66 +++++++++++++++++++++++++++++++++++++ 10 files changed, 175 insertions(+), 15 deletions(-) create mode 100644 src/app/news/gnews_api.py create mode 100644 tests/api/test_gnews_api.py diff --git a/demos/news_api.py b/demos/news_api.py index 0fc4c37..26dab24 100644 --- a/demos/news_api.py +++ b/demos/news_api.py @@ -5,10 +5,10 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../src' ########################################### from dotenv import load_dotenv -from app.news import NewsAPI +from app.news import NewsApiWrapper def main(): - api = NewsAPI() + api = NewsApiWrapper() print("ok") if __name__ == "__main__": diff --git a/pyproject.toml b/pyproject.toml index 3ef7154..b83de19 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,8 +14,6 @@ dependencies = [ "pytest", # ✅ per gestire variabili d'ambiente (generalmente API keys od opzioni) "dotenv", - # 🟡 per fare scraping di pagine web - #"bs4", # ✅ per fare una UI web semplice con input e output "gradio", @@ -34,6 +32,7 @@ dependencies = [ # ✅ per interagire con API di notizie "newsapi-python", + "gnews", ] [tool.pytest.ini_options] diff --git a/src/app/news/__init__.py b/src/app/news/__init__.py index 9b88ff6..957453d 100644 --- a/src/app/news/__init__.py +++ b/src/app/news/__init__.py @@ -1,3 +1,4 @@ -from .news_api import NewsAPI +from .news_api import NewsApiWrapper +from .gnews_api import GnewsWrapper -__all__ = ["NewsAPI"] \ No newline at end of file +__all__ = ["NewsApiWrapper", "GnewsWrapper"] \ No newline at end of file diff --git a/src/app/news/base.py b/src/app/news/base.py index 0391424..8b3f55d 100644 --- a/src/app/news/base.py +++ b/src/app/news/base.py @@ -6,3 +6,9 @@ class Article(BaseModel): title: str = "" description: str = "" +class NewsWrapper: + def get_top_headlines(self, query: str, total: int = 100) -> list[Article]: + raise NotImplementedError("This method should be overridden by subclasses") + def get_latest_news(self, query: str, total: int = 100) -> list[Article]: + raise NotImplementedError("This method should be overridden by subclasses") + diff --git a/src/app/news/gnews_api.py b/src/app/news/gnews_api.py new file mode 100644 index 0000000..53451c9 --- /dev/null +++ b/src/app/news/gnews_api.py @@ -0,0 +1,31 @@ +from gnews import GNews +from .base import Article, NewsWrapper + +def result_to_article(result: dict) -> Article: + article = Article() + article.source = result.get("source", "") + article.time = result.get("publishedAt", "") + article.title = result.get("title", "") + article.description = result.get("description", "") + return article + +class GnewsWrapper(NewsWrapper): + def get_top_headlines(self, query: str, total: int = 100) -> list[Article]: + gnews = GNews(language='en', max_results=total, period='7d') + results = gnews.get_top_news() + + articles = [] + for result in results: + article = result_to_article(result) + articles.append(article) + return articles + + def get_latest_news(self, query: str, total: int = 100) -> list[Article]: + gnews = GNews(language='en', max_results=total, period='7d') + results = gnews.get_news(query) + + articles = [] + for result in results: + article = result_to_article(result) + articles.append(article) + return articles diff --git a/src/app/news/news_api.py b/src/app/news/news_api.py index ce213cf..9629ecd 100644 --- a/src/app/news/news_api.py +++ b/src/app/news/news_api.py @@ -1,7 +1,6 @@ import os import newsapi -from .base import Article - +from .base import Article, NewsWrapper def result_to_article(result: dict) -> Article: article = Article() @@ -11,7 +10,7 @@ def result_to_article(result: dict) -> Article: article.description = result.get("description", "") return article -class NewsAPI: +class NewsApiWrapper(NewsWrapper): def __init__(self): api_key = os.getenv("NEWS_API_KEY") assert api_key is not None, "NEWS_API_KEY environment variable not set" @@ -21,7 +20,7 @@ class NewsAPI: self.language = "en" # TODO Only English articles for now? self.max_page_size = 100 - def get_top_headlines(self, query:str, total:int=100) -> list[Article]: + def get_top_headlines(self, query: str, total: int = 100) -> list[Article]: page_size = min(self.max_page_size, total) pages = (total // page_size) + (1 if total % page_size > 0 else 0) @@ -32,4 +31,14 @@ class NewsAPI: articles.extend(results) return articles + def get_latest_news(self, query: str, total: int = 100) -> list[Article]: + page_size = min(self.max_page_size, total) + pages = (total // page_size) + (1 if total % page_size > 0 else 0) + + articles = [] + for page in range(1, pages + 1): + everything = self.client.get_everything(q=query, language=self.language, sort_by="publishedAt", page_size=page_size, page=page) + results = [result_to_article(article) for article in everything.get("articles", [])] + articles.extend(results) + return articles diff --git a/tests/api/test_gnews_api.py b/tests/api/test_gnews_api.py new file mode 100644 index 0000000..49c8418 --- /dev/null +++ b/tests/api/test_gnews_api.py @@ -0,0 +1,34 @@ +import pytest +from app.news import GnewsWrapper + + +@pytest.mark.news +@pytest.mark.api +class TestGnewsAPI: + + def test_gnews_api_initialization(self): + gnews_api = GnewsWrapper() + assert gnews_api is not None + + def test_gnews_api_get_latest_news(self): + gnews_api = GnewsWrapper() + articles = gnews_api.get_latest_news(query="crypto", total=2) + assert isinstance(articles, list) + assert len(articles) == 2 + for article in articles: + assert hasattr(article, 'source') + assert hasattr(article, 'time') + assert hasattr(article, 'title') + assert hasattr(article, 'description') + + def test_gnews_api_get_top_headlines(self): + news_api = GnewsWrapper() + articles = news_api.get_top_headlines(query="crypto", total=2) + assert isinstance(articles, list) + assert len(articles) == 2 + for article in articles: + assert hasattr(article, 'source') + assert hasattr(article, 'time') + assert hasattr(article, 'title') + assert hasattr(article, 'description') + diff --git a/tests/api/test_news_api.py b/tests/api/test_news_api.py index 9558882..4778d5b 100644 --- a/tests/api/test_news_api.py +++ b/tests/api/test_news_api.py @@ -1,13 +1,29 @@ -from app.news import NewsAPI +import pytest +from app.news import NewsApiWrapper + +@pytest.mark.news +@pytest.mark.api class TestNewsAPI: def test_news_api_initialization(self): - news_api = NewsAPI() + news_api = NewsApiWrapper() assert news_api.client is not None + def test_news_api_get_latest_news(self): + news_api = NewsApiWrapper() + articles = news_api.get_latest_news(query="crypto", total=2) + assert isinstance(articles, list) + assert len(articles) > 0 # Ensure we got some articles (apparently it doesn't always return the requested number) + for article in articles: + assert hasattr(article, 'source') + assert hasattr(article, 'time') + assert hasattr(article, 'title') + assert hasattr(article, 'description') + + def test_news_api_get_top_headlines(self): - news_api = NewsAPI() + news_api = NewsApiWrapper() articles = news_api.get_top_headlines(query="crypto", total=2) assert isinstance(articles, list) assert len(articles) > 0 # Ensure we got some articles (apparently it doesn't always return the requested number) diff --git a/tests/conftest.py b/tests/conftest.py index f2601b1..21502d1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -30,7 +30,6 @@ def pytest_collection_modifyitems(config, items): """Modifica automaticamente gli item di test aggiungendogli marker basati sul nome""" markers_to_add = { - "api": pytest.mark.api, "coinbase": pytest.mark.api, "cryptocompare": pytest.mark.api, "overview": pytest.mark.slow, @@ -38,7 +37,6 @@ def pytest_collection_modifyitems(config, items): "gemini": pytest.mark.gemini, "ollama_gpt": pytest.mark.ollama_gpt, "ollama_qwen": pytest.mark.ollama_qwen, - "news": pytest.mark.news, } for item in items: diff --git a/uv.lock b/uv.lock index 26356c0..7eb69ba 100644 --- a/uv.lock +++ b/uv.lock @@ -130,6 +130,19 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/df/73/b6e24bd22e6720ca8ee9a85a0c4a2971af8497d8f3193fa05390cbd46e09/backoff-2.2.1-py3-none-any.whl", hash = "sha256:63579f9a0628e06278f7e47b7d7d5b6ce20dc65c5e96a6f3ca99a6adca0396e8", size = 15148, upload-time = "2022-10-05T19:19:30.546Z" }, ] +[[package]] +name = "beautifulsoup4" +version = "4.14.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "soupsieve" }, + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/77/e9/df2358efd7659577435e2177bfa69cba6c33216681af51a707193dec162a/beautifulsoup4-4.14.2.tar.gz", hash = "sha256:2a98ab9f944a11acee9cc848508ec28d9228abfd522ef0fad6a02a72e0ded69e", size = 625822, upload-time = "2025-09-29T10:05:42.613Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/94/fe/3aed5d0be4d404d12d36ab97e2f1791424d9ca39c2f754a6285d59a3b01d/beautifulsoup4-4.14.2-py3-none-any.whl", hash = "sha256:5ef6fa3a8cbece8488d66985560f97ed091e22bbc4e9c2338508a9d5de6d4515", size = 106392, upload-time = "2025-09-29T10:05:43.771Z" }, +] + [[package]] name = "brotli" version = "1.1.0" @@ -310,6 +323,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/87/22/f020c047ae1346613db9322638186468238bcfa8849b4668a22b97faad65/dateparser-1.2.2-py3-none-any.whl", hash = "sha256:5a5d7211a09013499867547023a2a0c91d5a27d15dd4dbcea676ea9fe66f2482", size = 315453, upload-time = "2025-06-26T09:29:21.412Z" }, ] +[[package]] +name = "dnspython" +version = "2.8.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/8c/8b/57666417c0f90f08bcafa776861060426765fdb422eb10212086fb811d26/dnspython-2.8.0.tar.gz", hash = "sha256:181d3c6996452cb1189c4046c61599b84a5a86e099562ffde77d26984ff26d0f", size = 368251, upload-time = "2025-09-07T18:58:00.022Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/ba/5a/18ad964b0086c6e62e2e7500f7edc89e3faa45033c71c1893d34eed2b2de/dnspython-2.8.0-py3-none-any.whl", hash = "sha256:01d9bbc4a2d76bf0db7c1f729812ded6d912bd318d3b1cf81d30c0f845dbf3af", size = 331094, upload-time = "2025-09-07T18:57:58.071Z" }, +] + [[package]] name = "docstring-parser" version = "0.17.0" @@ -344,6 +366,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/32/e4/c543271a8018874b7f682bf6156863c416e1334b8ed3e51a69495c5d4360/fastapi-0.116.2-py3-none-any.whl", hash = "sha256:c3a7a8fb830b05f7e087d920e0d786ca1fc9892eb4e9a84b227be4c1bc7569db", size = 95670, upload-time = "2025-09-16T18:29:21.329Z" }, ] +[[package]] +name = "feedparser" +version = "6.0.12" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "sgmllib3k" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/dc/79/db7edb5e77d6dfbc54d7d9df72828be4318275b2e580549ff45a962f6461/feedparser-6.0.12.tar.gz", hash = "sha256:64f76ce90ae3e8ef5d1ede0f8d3b50ce26bcce71dd8ae5e82b1cd2d4a5f94228", size = 286579, upload-time = "2025-09-10T13:33:59.486Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/4e/eb/c96d64137e29ae17d83ad2552470bafe3a7a915e85434d9942077d7fd011/feedparser-6.0.12-py3-none-any.whl", hash = "sha256:6bbff10f5a52662c00a2e3f86a38928c37c48f77b3c511aedcd51de933549324", size = 81480, upload-time = "2025-09-10T13:33:58.022Z" }, +] + [[package]] name = "ffmpy" version = "0.6.1" @@ -421,6 +455,21 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/01/61/d4b89fec821f72385526e1b9d9a3a0385dda4a72b206d28049e2c7cd39b8/gitpython-3.1.45-py3-none-any.whl", hash = "sha256:8908cb2e02fb3b93b7eb0f2827125cb699869470432cc885f019b8fd0fccff77", size = 208168, upload-time = "2025-07-24T03:45:52.517Z" }, ] +[[package]] +name = "gnews" +version = "0.4.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "beautifulsoup4" }, + { name = "dnspython" }, + { name = "feedparser" }, + { name = "requests" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/6c/65/d4b19ebde3edd4d0cb63660fe61e9777de1dd35ea819cb72a5b53002bb97/gnews-0.4.2.tar.gz", hash = "sha256:5016cf5299f42ea072adb295abe5e9f093c5c422da2c12e6661d1dcdbc56d011", size = 24847, upload-time = "2025-07-27T13:46:54.717Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/6f/77/00b21cce68b6041e78edf23efbc95eea6a4555cd474594b7360d1b9e4444/gnews-0.4.2-py3-none-any.whl", hash = "sha256:ed1fa603a7edeb3886925e756b114afb1e0c5b7b9f56fe5ebeedeeb730d2a9c4", size = 18142, upload-time = "2025-07-27T13:46:53.848Z" }, +] + [[package]] name = "google-auth" version = "2.40.3" @@ -1164,6 +1213,12 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/6a/23/8146aad7d88f4fcb3a6218f41a60f6c2d4e3a72de72da1825dc7c8f7877c/semantic_version-2.10.0-py2.py3-none-any.whl", hash = "sha256:de78a3b8e0feda74cabc54aab2da702113e33ac9d9eb9d2389bcf1f58b7d9177", size = 15552, upload-time = "2022-05-26T13:35:21.206Z" }, ] +[[package]] +name = "sgmllib3k" +version = "1.0.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/9e/bd/3704a8c3e0942d711c1299ebf7b9091930adae6675d7c8f476a7ce48653c/sgmllib3k-1.0.0.tar.gz", hash = "sha256:7868fb1c8bfa764c1ac563d3cf369c381d1325d36124933a726f29fcdaa812e9", size = 5750, upload-time = "2010-08-24T14:33:52.445Z" } + [[package]] name = "shellingham" version = "1.5.4" @@ -1200,6 +1255,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235, upload-time = "2024-02-25T23:20:01.196Z" }, ] +[[package]] +name = "soupsieve" +version = "2.8" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/6d/e6/21ccce3262dd4889aa3332e5a119a3491a95e8f60939870a3a035aabac0d/soupsieve-2.8.tar.gz", hash = "sha256:e2dd4a40a628cb5f28f6d4b0db8800b8f581b65bb380b97de22ba5ca8d72572f", size = 103472, upload-time = "2025-08-27T15:39:51.78Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/14/a0/bb38d3b76b8cae341dad93a2dd83ab7462e6dbcdd84d43f54ee60a8dc167/soupsieve-2.8-py3-none-any.whl", hash = "sha256:0cc76456a30e20f5d7f2e14a98a4ae2ee4e5abdc7c5ea0aafe795f344bc7984c", size = 36679, upload-time = "2025-08-27T15:39:50.179Z" }, +] + [[package]] name = "starlette" version = "0.48.0" @@ -1308,6 +1372,7 @@ dependencies = [ { name = "agno" }, { name = "coinbase-advanced-py" }, { name = "dotenv" }, + { name = "gnews" }, { name = "google-genai" }, { name = "gradio" }, { name = "newsapi-python" }, @@ -1321,6 +1386,7 @@ requires-dist = [ { name = "agno" }, { name = "coinbase-advanced-py" }, { name = "dotenv" }, + { name = "gnews" }, { name = "google-genai" }, { name = "gradio" }, { name = "newsapi-python" }, From 6aa9d4969f54764038a3cb439870bff3f1b85146 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Sep 2025 01:54:26 +0200 Subject: [PATCH 07/22] Add CryptoPanic API integration and related tests; update .env.example and test configurations --- .env.example | 5 ++- src/app/news/__init__.py | 3 +- src/app/news/cryptopanic_api.py | 70 +++++++++++++++++++++++++++++++ tests/api/test_cryptopanic_api.py | 38 +++++++++++++++++ tests/api/test_gnews_api.py | 16 +++---- tests/api/test_news_api.py | 20 +++++---- tests/conftest.py | 11 +++++ 7 files changed, 143 insertions(+), 20 deletions(-) create mode 100644 src/app/news/cryptopanic_api.py create mode 100644 tests/api/test_cryptopanic_api.py diff --git a/.env.example b/.env.example index d5d5a38..f59ad82 100644 --- a/.env.example +++ b/.env.example @@ -10,12 +10,10 @@ GOOGLE_API_KEY= # Configurazioni per gli agenti di mercato ############################################################################### -# Coinbase CDP API per Market Agent # Ottenibili da: https://portal.cdp.coinbase.com/access/api CDP_API_KEY_NAME= CDP_API_PRIVATE_KEY= -# CryptoCompare API per Market Agent (alternativa) # Ottenibile da: https://www.cryptocompare.com/cryptopian/api-keys CRYPTOCOMPARE_API_KEY= @@ -26,6 +24,9 @@ BINANCE_API_SECRET= ############################################################################### # Configurazioni per gli agenti di notizie ############################################################################### + # Ottenibile da: https://newsapi.org/docs NEWS_API_KEY= +# Ottenibile da: https://cryptopanic.com/developers/api/ +CRYPTOPANIC_API_KEY= diff --git a/src/app/news/__init__.py b/src/app/news/__init__.py index 957453d..f130e6b 100644 --- a/src/app/news/__init__.py +++ b/src/app/news/__init__.py @@ -1,4 +1,5 @@ from .news_api import NewsApiWrapper from .gnews_api import GnewsWrapper +from .cryptopanic_api import CryptoPanicWrapper -__all__ = ["NewsApiWrapper", "GnewsWrapper"] \ No newline at end of file +__all__ = ["NewsApiWrapper", "GnewsWrapper", "CryptoPanicWrapper"] \ No newline at end of file diff --git a/src/app/news/cryptopanic_api.py b/src/app/news/cryptopanic_api.py new file mode 100644 index 0000000..2e9270c --- /dev/null +++ b/src/app/news/cryptopanic_api.py @@ -0,0 +1,70 @@ +import os +import requests +from enum import Enum +from .base import NewsWrapper, Article + +class CryptoPanicFilter(Enum): + RISING = "rising" + HOT = "hot" + BULLISH = "bullish" + BEARISH = "bearish" + IMPORTANT = "important" + SAVED = "saved" + LOL = "lol" + ANY = "" + +class CryptoPanicKind(Enum): + NEWS = "news" + MEDIA = "media" + ALL = "all" + +def get_articles(response: dict) -> list[Article]: + articles = [] + if 'results' in response: + for item in response['results']: + article = Article() + article.source = item.get('source', {}).get('title', '') + article.time = item.get('published_at', '') + article.title = item.get('title', '') + article.description = item.get('description', '') + articles.append(article) + return articles + +class CryptoPanicWrapper(NewsWrapper): + def __init__(self): + self.api_key = os.getenv("CRYPTOPANIC_API_KEY", "") + assert self.api_key, "CRYPTOPANIC_API_KEY environment variable not set" + + # Set here for the future, but currently not needed + plan_type = os.getenv("CRYPTOPANIC_API_PLAN", "developer").lower() + assert plan_type in ["developer", "growth", "enterprise"], "Invalid CRYPTOPANIC_API_PLAN value" + + self.base_url = f"https://cryptopanic.com/api/{plan_type}/v2" + self.filter = CryptoPanicFilter.ANY + self.kind = CryptoPanicKind.NEWS + + def get_base_params(self) -> dict[str, str]: + params = {} + params['public'] = 'true' # recommended for app and bots + params['auth_token'] = self.api_key + params['kind'] = self.kind.value + if self.filter != CryptoPanicFilter.ANY: + params['filter'] = self.filter.value + return params + + def set_filter(self, filter: CryptoPanicFilter): + self.filter = filter + + def get_top_headlines(self, query: str, total: int = 100) -> list[Article]: + params = self.get_base_params() + params['currencies'] = query + + response = requests.get(f"{self.base_url}/posts/", params=params) + assert response.status_code == 200, f"Error fetching data: {response}" + + json_response = response.json() + articles = get_articles(json_response) + return articles[:total] + + def get_latest_news(self, query: str, total: int = 100) -> list[Article]: + return self.get_top_headlines(query, total) # same endpoint for both, so just call it diff --git a/tests/api/test_cryptopanic_api.py b/tests/api/test_cryptopanic_api.py new file mode 100644 index 0000000..dda2f77 --- /dev/null +++ b/tests/api/test_cryptopanic_api.py @@ -0,0 +1,38 @@ +import os +import pytest +from app.news import CryptoPanicWrapper + + +@pytest.mark.limited +@pytest.mark.news +@pytest.mark.api +@pytest.mark.skipif(not os.getenv("CRYPTOPANIC_API_KEY"), reason="CRYPTOPANIC_API_KEY not set") +class TestCryptoPanicAPI: + + def test_crypto_panic_api_initialization(self): + crypto = CryptoPanicWrapper() + assert crypto is not None + + def test_crypto_panic_api_get_latest_news(self): + crypto = CryptoPanicWrapper() + articles = crypto.get_latest_news(query="", total=2) + assert isinstance(articles, list) + assert len(articles) == 2 + for article in articles: + assert article.source is not None or article.source != "" + assert article.time is not None or article.time != "" + assert article.title is not None or article.title != "" + assert article.description is not None or article.description != "" + + # Useless since both methods use the same endpoint + # def test_crypto_panic_api_get_top_headlines(self): + # crypto = CryptoPanicWrapper() + # articles = crypto.get_top_headlines(query="crypto", total=2) + # assert isinstance(articles, list) + # assert len(articles) == 2 + # for article in articles: + # assert article.source is not None or article.source != "" + # assert article.time is not None or article.time != "" + # assert article.title is not None or article.title != "" + # assert article.description is not None or article.description != "" + diff --git a/tests/api/test_gnews_api.py b/tests/api/test_gnews_api.py index 49c8418..1013fa7 100644 --- a/tests/api/test_gnews_api.py +++ b/tests/api/test_gnews_api.py @@ -16,10 +16,10 @@ class TestGnewsAPI: assert isinstance(articles, list) assert len(articles) == 2 for article in articles: - assert hasattr(article, 'source') - assert hasattr(article, 'time') - assert hasattr(article, 'title') - assert hasattr(article, 'description') + assert article.source is not None or article.source != "" + assert article.time is not None or article.time != "" + assert article.title is not None or article.title != "" + assert article.description is not None or article.description != "" def test_gnews_api_get_top_headlines(self): news_api = GnewsWrapper() @@ -27,8 +27,8 @@ class TestGnewsAPI: assert isinstance(articles, list) assert len(articles) == 2 for article in articles: - assert hasattr(article, 'source') - assert hasattr(article, 'time') - assert hasattr(article, 'title') - assert hasattr(article, 'description') + assert article.source is not None or article.source != "" + assert article.time is not None or article.time != "" + assert article.title is not None or article.title != "" + assert article.description is not None or article.description != "" diff --git a/tests/api/test_news_api.py b/tests/api/test_news_api.py index 4778d5b..da8f607 100644 --- a/tests/api/test_news_api.py +++ b/tests/api/test_news_api.py @@ -1,9 +1,11 @@ +import os import pytest from app.news import NewsApiWrapper @pytest.mark.news @pytest.mark.api +@pytest.mark.skipif(not os.getenv("NEWS_API_KEY"), reason="NEWS_API_KEY not set") class TestNewsAPI: def test_news_api_initialization(self): @@ -16,20 +18,20 @@ class TestNewsAPI: assert isinstance(articles, list) assert len(articles) > 0 # Ensure we got some articles (apparently it doesn't always return the requested number) for article in articles: - assert hasattr(article, 'source') - assert hasattr(article, 'time') - assert hasattr(article, 'title') - assert hasattr(article, 'description') + assert article.source is not None or article.source != "" + assert article.time is not None or article.time != "" + assert article.title is not None or article.title != "" + assert article.description is not None or article.description != "" def test_news_api_get_top_headlines(self): news_api = NewsApiWrapper() articles = news_api.get_top_headlines(query="crypto", total=2) assert isinstance(articles, list) - assert len(articles) > 0 # Ensure we got some articles (apparently it doesn't always return the requested number) + # assert len(articles) > 0 # apparently it doesn't always return SOME articles for article in articles: - assert hasattr(article, 'source') - assert hasattr(article, 'time') - assert hasattr(article, 'title') - assert hasattr(article, 'description') + assert article.source is not None or article.source != "" + assert article.time is not None or article.time != "" + assert article.title is not None or article.title != "" + assert article.description is not None or article.description != "" diff --git a/tests/conftest.py b/tests/conftest.py index 21502d1..5584992 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -21,6 +21,7 @@ def pytest_configure(config:pytest.Config): ("ollama_gpt", "marks tests that use Ollama GPT model"), ("ollama_qwen", "marks tests that use Ollama Qwen model"), ("news", "marks tests that use news"), + ("limited", "marks tests that have limited execution due to API constraints"), ] for marker in markers: line = f"{marker[0]}: {marker[1]}" @@ -44,3 +45,13 @@ def pytest_collection_modifyitems(config, items): for key, marker in markers_to_add.items(): if key in name: item.add_marker(marker) + + # Rimuovo i test "limited" e "slow" se non richiesti esplicitamente + mark_to_remove = ['limited', 'slow'] + for mark in mark_to_remove: + markexpr = getattr(config.option, "markexpr", None) + if markexpr and mark in markexpr.lower(): + continue + + new_mark = (f"({markexpr}) and " if markexpr else "") + f"not {mark}" + setattr(config.option, "markexpr", new_mark) From 912a9b9c8d6ea3d73045a36fcca0f91ff6268400 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Sep 2025 02:55:09 +0200 Subject: [PATCH 08/22] Implement WrapperHandler for managing multiple news API wrappers; add tests for wrapper functionality --- src/app/news/__init__.py | 15 +++++++- src/app/utils/wrapper_handler.py | 48 +++++++++++++++++++++++ tests/agents/test_predictor.py | 5 ++- tests/conftest.py | 1 + tests/utils/test_wrapper_handler.py | 60 +++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 src/app/utils/wrapper_handler.py create mode 100644 tests/utils/test_wrapper_handler.py diff --git a/src/app/news/__init__.py b/src/app/news/__init__.py index f130e6b..8b0fd04 100644 --- a/src/app/news/__init__.py +++ b/src/app/news/__init__.py @@ -1,5 +1,18 @@ +from app.utils.wrapper_handler import WrapperHandler +from .base import NewsWrapper, Article from .news_api import NewsApiWrapper from .gnews_api import GnewsWrapper from .cryptopanic_api import CryptoPanicWrapper -__all__ = ["NewsApiWrapper", "GnewsWrapper", "CryptoPanicWrapper"] \ No newline at end of file +__all__ = ["NewsApiWrapper", "GnewsWrapper", "CryptoPanicWrapper"] + + +class NewsAPIs(NewsWrapper): + def __init__(self): + wrappers = [GnewsWrapper, NewsApiWrapper, CryptoPanicWrapper] + self.wrapper_handler: WrapperHandler[NewsWrapper] = WrapperHandler.build_wrappers(wrappers) + + def get_top_headlines(self, query: str, total: int = 100) -> list[Article]: + return self.wrapper_handler.try_call(lambda w: w.get_top_headlines(query, total)) + def get_latest_news(self, query: str, total: int = 100) -> list[Article]: + return self.wrapper_handler.try_call(lambda w: w.get_latest_news(query, total)) diff --git a/src/app/utils/wrapper_handler.py b/src/app/utils/wrapper_handler.py new file mode 100644 index 0000000..05cd8f4 --- /dev/null +++ b/src/app/utils/wrapper_handler.py @@ -0,0 +1,48 @@ +import time +from typing import TypeVar, Callable, Generic, Iterable, Type +from agno.utils.log import log_warning + +W = TypeVar("W") +T = TypeVar("T") + +class WrapperHandler(Generic[W]): + def __init__(self, wrappers: list[W], try_per_wrapper: int = 3, retry_delay: int = 2): + self.wrappers = wrappers + self.retry_per_wrapper = try_per_wrapper + self.retry_delay = retry_delay + self.index = 0 + self.retry_count = 0 + + def try_call(self, func: Callable[[W], T]) -> T: + iterations = 0 + while iterations < len(self.wrappers): + print(f"Trying wrapper {self.index}") + try: + wrapper = self.wrappers[self.index] + result = func(wrapper) + self.retry_count = 0 + return result + except Exception as e: + self.retry_count += 1 + print(f"Error occurred {self.retry_count}/{self.retry_per_wrapper}: {e}") + if self.retry_count >= self.retry_per_wrapper: + self.index = (self.index + 1) % len(self.wrappers) + self.retry_count = 0 + iterations += 1 + else: + log_warning(f"{wrapper} failed {self.retry_count}/{self.retry_per_wrapper}: {e}") + time.sleep(self.retry_delay) + + raise Exception(f"All wrappers failed after retries") + + @staticmethod + def build_wrappers(constructors: Iterable[Type[W]], try_per_wrapper: int = 3, retry_delay: int = 2) -> 'WrapperHandler[W]': + result = [] + for wrapper_class in constructors: + try: + wrapper = wrapper_class() + result.append(wrapper) + except Exception as e: + log_warning(f"{wrapper_class} cannot be initialized: {e}") + + return WrapperHandler(result, try_per_wrapper, retry_delay) \ No newline at end of file diff --git a/tests/agents/test_predictor.py b/tests/agents/test_predictor.py index c99104b..1255a48 100644 --- a/tests/agents/test_predictor.py +++ b/tests/agents/test_predictor.py @@ -16,8 +16,8 @@ def unified_checks(model: AppModels, input): for item in content.portfolio: assert item.asset not in (None, "", "null") assert isinstance(item.asset, str) - assert item.percentage > 0 - assert item.percentage <= 100 + assert item.percentage >= 0.0 + assert item.percentage <= 100.0 assert isinstance(item.percentage, (int, float)) assert item.motivation not in (None, "", "null") assert isinstance(item.motivation, str) @@ -41,6 +41,7 @@ class TestPredictor: def test_gemini_model_output(self, inputs): unified_checks(AppModels.GEMINI, inputs) + @pytest.mark.slow def test_ollama_qwen_model_output(self, inputs): unified_checks(AppModels.OLLAMA_QWEN, inputs) diff --git a/tests/conftest.py b/tests/conftest.py index 5584992..9bd9589 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -22,6 +22,7 @@ def pytest_configure(config:pytest.Config): ("ollama_qwen", "marks tests that use Ollama Qwen model"), ("news", "marks tests that use news"), ("limited", "marks tests that have limited execution due to API constraints"), + ("wrapper", "marks tests for wrapper handler"), ] for marker in markers: line = f"{marker[0]}: {marker[1]}" diff --git a/tests/utils/test_wrapper_handler.py b/tests/utils/test_wrapper_handler.py new file mode 100644 index 0000000..2b9583a --- /dev/null +++ b/tests/utils/test_wrapper_handler.py @@ -0,0 +1,60 @@ +import pytest +from app.utils.wrapper_handler import WrapperHandler + +class MockWrapper: + def do_something(self) -> str: + return "Success" + +class FailingWrapper(MockWrapper): + def do_something(self): + raise Exception("Intentional Failure") + + +@pytest.mark.wrapper +class TestWrapperHandler: + def test_all_wrappers_fail(self): + wrappers = [FailingWrapper, FailingWrapper] + handler: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers(wrappers, try_per_wrapper=2, retry_delay=0) + + with pytest.raises(Exception) as exc_info: + handler.try_call(lambda w: w.do_something()) + assert "All wrappers failed after retries" in str(exc_info.value) + + def test_success_on_first_try(self): + wrappers = [MockWrapper, FailingWrapper] + handler: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers(wrappers, try_per_wrapper=2, retry_delay=0) + + result = handler.try_call(lambda w: w.do_something()) + assert result == "Success" + assert handler.index == 0 # Should still be on the first wrapper + assert handler.retry_count == 0 + + def test_eventual_success(self): + wrappers = [FailingWrapper, MockWrapper] + handler: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers(wrappers, try_per_wrapper=2, retry_delay=0) + + result = handler.try_call(lambda w: w.do_something()) + assert result == "Success" + assert handler.index == 1 # Should have switched to the second wrapper + assert handler.retry_count == 0 + + def test_partial_failures(self): + wrappers = [FailingWrapper, MockWrapper, FailingWrapper] + handler: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers(wrappers, try_per_wrapper=1, retry_delay=0) + + result = handler.try_call(lambda w: w.do_something()) + assert result == "Success" + assert handler.index == 1 # Should have switched to the second wrapper + assert handler.retry_count == 0 + + # Next call should still succeed on the second wrapper + result = handler.try_call(lambda w: w.do_something()) + assert result == "Success" + assert handler.index == 1 # Should still be on the second wrapper + assert handler.retry_count == 0 + + handler.index = 2 # Manually switch to the third wrapper + result = handler.try_call(lambda w: w.do_something()) + assert result == "Success" + assert handler.index == 1 # Should return to the second wrapper after failure + assert handler.retry_count == 0 From 40fb400a9ca100c8a2ce3111b39cc05543e271e8 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Sep 2025 11:46:52 +0200 Subject: [PATCH 09/22] Enhance WrapperHandler - docstrings - add try_call_all method - update tests --- src/app/utils/wrapper_handler.py | 66 ++++++++++++++++++++++++++++- tests/utils/test_wrapper_handler.py | 19 +++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/app/utils/wrapper_handler.py b/src/app/utils/wrapper_handler.py index 05cd8f4..0ea72d6 100644 --- a/src/app/utils/wrapper_handler.py +++ b/src/app/utils/wrapper_handler.py @@ -6,7 +6,24 @@ W = TypeVar("W") T = TypeVar("T") class WrapperHandler(Generic[W]): + """ + A handler for managing multiple wrappers with retry logic. + It attempts to call a function on the current wrapper, and if it fails, + it retries a specified number of times before switching to the next wrapper. + If all wrappers fail, it raises an exception. + + Note: use `build_wrappers` to create an instance of this class for better error handling. + """ + def __init__(self, wrappers: list[W], try_per_wrapper: int = 3, retry_delay: int = 2): + """ + Initializes the WrapperHandler with a list of wrappers and retry settings.\n + Use `build_wrappers` to create an instance of this class for better error handling. + Args: + wrappers (list[W]): A list of wrapper instances to manage. + try_per_wrapper (int): Number of retries per wrapper before switching to the next. + retry_delay (int): Delay in seconds between retries. + """ self.wrappers = wrappers self.retry_per_wrapper = try_per_wrapper self.retry_delay = retry_delay @@ -14,9 +31,19 @@ class WrapperHandler(Generic[W]): self.retry_count = 0 def try_call(self, func: Callable[[W], T]) -> T: + """ + Attempts to call the provided function on the current wrapper. + If it fails, it retries a specified number of times before switching to the next wrapper. + If all wrappers fail, it raises an exception. + Args: + func (Callable[[W], T]): A function that takes a wrapper and returns a result. + Returns: + T: The result of the function call. + Raises: + Exception: If all wrappers fail after retries. + """ iterations = 0 while iterations < len(self.wrappers): - print(f"Trying wrapper {self.index}") try: wrapper = self.wrappers[self.index] result = func(wrapper) @@ -24,7 +51,6 @@ class WrapperHandler(Generic[W]): return result except Exception as e: self.retry_count += 1 - print(f"Error occurred {self.retry_count}/{self.retry_per_wrapper}: {e}") if self.retry_count >= self.retry_per_wrapper: self.index = (self.index + 1) % len(self.wrappers) self.retry_count = 0 @@ -35,8 +61,44 @@ class WrapperHandler(Generic[W]): raise Exception(f"All wrappers failed after retries") + def try_call_all(self, func: Callable[[W], T]) -> list[T]: + """ + Calls the provided function on all wrappers, collecting results. + If a wrapper fails, it logs a warning and continues with the next. + If all wrappers fail, it raises an exception. + Args: + func (Callable[[W], T]): A function that takes a wrapper and returns a result. + Returns: + list[T]: A list of results from the function calls. + Raises: + Exception: If all wrappers fail. + """ + results = [] + for wrapper in self.wrappers: + try: + result = func(wrapper) + results.append(result) + except Exception as e: + log_warning(f"{wrapper} failed: {e}") + if not results: + raise Exception("All wrappers failed") + return results + @staticmethod def build_wrappers(constructors: Iterable[Type[W]], try_per_wrapper: int = 3, retry_delay: int = 2) -> 'WrapperHandler[W]': + """ + Builds a WrapperHandler instance with the given wrapper constructors. + It attempts to initialize each wrapper and logs a warning if any cannot be initialized. + Only successfully initialized wrappers are included in the handler. + Args: + constructors (Iterable[Type[W]]): An iterable of wrapper classes to instantiate. e.g. [WrapperA, WrapperB] + try_per_wrapper (int): Number of retries per wrapper before switching to the next. + retry_delay (int): Delay in seconds between retries. + Returns: + WrapperHandler[W]: An instance of WrapperHandler with the initialized wrappers. + Raises: + Exception: If no wrappers could be initialized. + """ result = [] for wrapper_class in constructors: try: diff --git a/tests/utils/test_wrapper_handler.py b/tests/utils/test_wrapper_handler.py index 2b9583a..fd5ffff 100644 --- a/tests/utils/test_wrapper_handler.py +++ b/tests/utils/test_wrapper_handler.py @@ -58,3 +58,22 @@ class TestWrapperHandler: assert result == "Success" assert handler.index == 1 # Should return to the second wrapper after failure assert handler.retry_count == 0 + + def test_try_call_all(self): + wrappers = [FailingWrapper, MockWrapper, FailingWrapper] + handler: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers(wrappers, try_per_wrapper=1, retry_delay=0) + + results = handler.try_call_all(lambda w: w.do_something()) + assert results == ["Success"] # Only the second wrapper should succeed + + wrappers = [FailingWrapper, MockWrapper, FailingWrapper, MockWrapper] + handler: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers(wrappers, try_per_wrapper=1, retry_delay=0) + + results = handler.try_call_all(lambda w: w.do_something()) + assert results == ["Success", "Success"] # Only the second and fourth wrappers should succeed + + # Test when all wrappers fail + handler_all_fail: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers([FailingWrapper, FailingWrapper], try_per_wrapper=1, retry_delay=0) + with pytest.raises(Exception) as exc_info: + handler_all_fail.try_call_all(lambda w: w.do_something()) + assert "All wrappers failed" in str(exc_info.value) From fb38aef79039e753c3ca9c70908786fc8c78b323 Mon Sep 17 00:00:00 2001 From: Simone Garau <20005068@studenti.uniupo.it> Date: Tue, 30 Sep 2025 12:11:10 +0200 Subject: [PATCH 10/22] pre merge con phil --- src/app.py | 14 ++++---- src/app/agents/market_agent.py | 35 +++++++++++++++++++ src/app/{tool.py => pipeline.py} | 4 +-- src/app/{agents => }/predictor.py | 0 src/app/toolkits/__init__.py | 0 .../market.py => toolkits/market_toolkit.py} | 0 tests/agents/test_predictor.py | 2 +- 7 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 src/app/agents/market_agent.py rename src/app/{tool.py => pipeline.py} (96%) rename src/app/{agents => }/predictor.py (100%) create mode 100644 src/app/toolkits/__init__.py rename src/app/{agents/market.py => toolkits/market_toolkit.py} (100%) diff --git a/src/app.py b/src/app.py index 983779e..8477149 100644 --- a/src/app.py +++ b/src/app.py @@ -1,7 +1,7 @@ import gradio as gr from dotenv import load_dotenv -from app.tool import ToolAgent +from app.pipeline import Pipeline from agno.utils.log import log_info ######################################## @@ -16,31 +16,31 @@ if __name__ == "__main__": load_dotenv() ###################################### - tool_agent = ToolAgent() + pipeline = Pipeline() with gr.Blocks() as demo: gr.Markdown("# 🤖 Agente di Analisi e Consulenza Crypto") with gr.Row(): provider = gr.Dropdown( - choices=tool_agent.list_providers(), + choices=pipeline.list_providers(), type="index", label="Modello da usare" ) - provider.change(fn=tool_agent.choose_provider, inputs=provider, outputs=None) + provider.change(fn=pipeline.choose_provider, inputs=provider, outputs=None) style = gr.Dropdown( - choices=tool_agent.list_styles(), + choices=pipeline.list_styles(), type="index", label="Stile di investimento" ) - style.change(fn=tool_agent.choose_style, inputs=style, outputs=None) + style.change(fn=pipeline.choose_style, inputs=style, outputs=None) user_input = gr.Textbox(label="Richiesta utente") output = gr.Textbox(label="Risultato analisi", lines=12) analyze_btn = gr.Button("🔎 Analizza") - analyze_btn.click(fn=tool_agent.interact, inputs=[user_input], outputs=output) + analyze_btn.click(fn=pipeline.interact, inputs=[user_input], outputs=output) server, port = ("0.0.0.0", 8000) log_info(f"Starting UPO AppAI on http://{server}:{port}") diff --git a/src/app/agents/market_agent.py b/src/app/agents/market_agent.py new file mode 100644 index 0000000..3325f97 --- /dev/null +++ b/src/app/agents/market_agent.py @@ -0,0 +1,35 @@ + +from agno.agent import Agent +from src.app.toolkits.market_toolkit import MarketToolkit + + +class MarketAgent(Agent): + """ + Wrapper che trasforma MarketToolkit in un Agent compatibile con Team. + Espone un metodo run(query) che restituisce dati di mercato. + """ + + def __init__(self, currency: str = "USD"): + self.toolkit = MarketToolkit() + self.currency = currency + self.name = "MarketAgent" + + def run(self, query: str) -> str: + # Heuristica semplice: se la query cita simboli specifici, recupera quelli + symbols = [] + for token in query.upper().split(): + if token in ("BTC", "ETH", "XRP", "LTC", "BCH"): # TODO: estendere dinamicamente + symbols.append(token) + + if not symbols: + symbols = ["BTC", "ETH"] # default + + results = [] + for sym in symbols: + try: + price = self.toolkit.get_current_price(sym) + results.append(f"{sym}: {price}") + except Exception as e: + results.append(f"{sym}: errore ({e})") + + return "📊 Dati di mercato:\n" + "\n".join(results) \ No newline at end of file diff --git a/src/app/tool.py b/src/app/pipeline.py similarity index 96% rename from src/app/tool.py rename to src/app/pipeline.py index d0b3ca0..d5508c2 100644 --- a/src/app/tool.py +++ b/src/app/pipeline.py @@ -1,11 +1,11 @@ from app.agents.news_agent import NewsAgent from app.agents.social_agent import SocialAgent -from app.agents.predictor import PredictorStyle, PredictorInput, PredictorOutput, PREDICTOR_INSTRUCTIONS +from app.predictor import PredictorStyle, PredictorInput, PredictorOutput, PREDICTOR_INSTRUCTIONS from app.markets import MarketAPIs from app.models import AppModels from agno.utils.log import log_info -class ToolAgent: +class Pipeline: """ Classe principale che coordina gli agenti per rispondere alle richieste dell'utente. """ diff --git a/src/app/agents/predictor.py b/src/app/predictor.py similarity index 100% rename from src/app/agents/predictor.py rename to src/app/predictor.py diff --git a/src/app/toolkits/__init__.py b/src/app/toolkits/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/app/agents/market.py b/src/app/toolkits/market_toolkit.py similarity index 100% rename from src/app/agents/market.py rename to src/app/toolkits/market_toolkit.py diff --git a/tests/agents/test_predictor.py b/tests/agents/test_predictor.py index c99104b..baf488d 100644 --- a/tests/agents/test_predictor.py +++ b/tests/agents/test_predictor.py @@ -1,5 +1,5 @@ import pytest -from app.agents.predictor import PREDICTOR_INSTRUCTIONS, PredictorInput, PredictorOutput, PredictorStyle +from app.predictor import PREDICTOR_INSTRUCTIONS, PredictorInput, PredictorOutput, PredictorStyle from app.markets.base import ProductInfo from app.models import AppModels From dfe3b4ad902f345ceafe57b1f61b080581315895 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Sep 2025 12:24:43 +0200 Subject: [PATCH 11/22] Add DuckDuckGo and Google News wrappers; refactor CryptoPanic and NewsAPI - Implemented DuckDuckGoWrapper for news retrieval using DuckDuckGo tools. - Added GoogleNewsWrapper for accessing Google News RSS feed. - Refactored CryptoPanicWrapper to unify get_top_headlines and get_latest_news methods. - Updated NewsApiWrapper to simplify top headlines retrieval. - Added tests for DuckDuckGo and Google News wrappers. - Enhanced documentation for CryptoPanicWrapper and NewsApiWrapper. - Created base module for social media integrations. --- pyproject.toml | 1 + src/app/news/__init__.py | 24 +++- src/app/news/base.py | 27 +++- src/app/news/cryptopanic_api.py | 15 +- src/app/news/duckduckgo.py | 32 +++++ src/app/news/gnews_api.py | 9 +- src/app/news/news_api.py | 10 +- src/app/social/__init.py | 1 + src/app/social/base.py | 0 tests/api/test_cryptopanic_api.py | 2 +- tests/api/test_duckduckgo_news.py | 35 +++++ ...{test_gnews_api.py => test_google_news.py} | 12 +- tests/api/test_news_api.py | 2 +- uv.lock | 128 ++++++++++++++++++ 14 files changed, 274 insertions(+), 24 deletions(-) create mode 100644 src/app/news/duckduckgo.py create mode 100644 src/app/social/__init.py create mode 100644 src/app/social/base.py create mode 100644 tests/api/test_duckduckgo_news.py rename tests/api/{test_gnews_api.py => test_google_news.py} (82%) diff --git a/pyproject.toml b/pyproject.toml index b83de19..8f4cc87 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,7 @@ dependencies = [ # ✅ per interagire con API di notizie "newsapi-python", "gnews", + "ddgs", ] [tool.pytest.ini_options] diff --git a/src/app/news/__init__.py b/src/app/news/__init__.py index 8b0fd04..3218296 100644 --- a/src/app/news/__init__.py +++ b/src/app/news/__init__.py @@ -1,18 +1,32 @@ from app.utils.wrapper_handler import WrapperHandler from .base import NewsWrapper, Article from .news_api import NewsApiWrapper -from .gnews_api import GnewsWrapper +from .gnews_api import GoogleNewsWrapper from .cryptopanic_api import CryptoPanicWrapper +from .duckduckgo import DuckDuckGoWrapper -__all__ = ["NewsApiWrapper", "GnewsWrapper", "CryptoPanicWrapper"] +__all__ = ["NewsApiWrapper", "GoogleNewsWrapper", "CryptoPanicWrapper", "DuckDuckGoWrapper"] class NewsAPIs(NewsWrapper): + """ + A wrapper class that aggregates multiple news API wrappers and tries them in order until one succeeds. + This class uses the WrapperHandler to manage multiple NewsWrapper instances. + It includes, and tries, the following news API wrappers in this order: + - GnewsWrapper + - DuckDuckGoWrapper + - NewsApiWrapper + - CryptoPanicWrapper + + It provides methods to get top headlines and latest news by delegating the calls to the first successful wrapper. + If all wrappers fail, it raises an exception. + """ + def __init__(self): - wrappers = [GnewsWrapper, NewsApiWrapper, CryptoPanicWrapper] + wrappers = [GoogleNewsWrapper, DuckDuckGoWrapper, NewsApiWrapper, CryptoPanicWrapper] self.wrapper_handler: WrapperHandler[NewsWrapper] = WrapperHandler.build_wrappers(wrappers) - def get_top_headlines(self, query: str, total: int = 100) -> list[Article]: - return self.wrapper_handler.try_call(lambda w: w.get_top_headlines(query, total)) + def get_top_headlines(self, total: int = 100) -> list[Article]: + return self.wrapper_handler.try_call(lambda w: w.get_top_headlines(total)) def get_latest_news(self, query: str, total: int = 100) -> list[Article]: return self.wrapper_handler.try_call(lambda w: w.get_latest_news(query, total)) diff --git a/src/app/news/base.py b/src/app/news/base.py index 8b3f55d..0a8f6be 100644 --- a/src/app/news/base.py +++ b/src/app/news/base.py @@ -7,8 +7,29 @@ class Article(BaseModel): description: str = "" class NewsWrapper: - def get_top_headlines(self, query: str, total: int = 100) -> list[Article]: - raise NotImplementedError("This method should be overridden by subclasses") - def get_latest_news(self, query: str, total: int = 100) -> list[Article]: + """ + Base class for news API wrappers. + All news API wrappers should inherit from this class and implement the methods. + """ + + def get_top_headlines(self, total: int = 100) -> list[Article]: + """ + Get top headlines, optionally limited by total. + Args: + total (int): The maximum number of articles to return. + Returns: + list[Article]: A list of Article objects. + """ + raise NotImplementedError("This method should be overridden by subclasses") + + def get_latest_news(self, query: str, total: int = 100) -> list[Article]: + """ + Get latest news based on a query. + Args: + query (str): The search query. + total (int): The maximum number of articles to return. + Returns: + list[Article]: A list of Article objects. + """ raise NotImplementedError("This method should be overridden by subclasses") diff --git a/src/app/news/cryptopanic_api.py b/src/app/news/cryptopanic_api.py index 2e9270c..a949c69 100644 --- a/src/app/news/cryptopanic_api.py +++ b/src/app/news/cryptopanic_api.py @@ -31,6 +31,13 @@ def get_articles(response: dict) -> list[Article]: return articles class CryptoPanicWrapper(NewsWrapper): + """ + A wrapper for the CryptoPanic API (Documentation: https://cryptopanic.com/developers/api/) + Requires an API key set in the environment variable CRYPTOPANIC_API_KEY. + It is free to use, but has rate limits and restrictions based on the plan type (the free plan is 'developer' with 100 req/month). + Supports different plan types via the CRYPTOPANIC_API_PLAN environment variable (developer, growth, enterprise). + """ + def __init__(self): self.api_key = os.getenv("CRYPTOPANIC_API_KEY", "") assert self.api_key, "CRYPTOPANIC_API_KEY environment variable not set" @@ -55,7 +62,10 @@ class CryptoPanicWrapper(NewsWrapper): def set_filter(self, filter: CryptoPanicFilter): self.filter = filter - def get_top_headlines(self, query: str, total: int = 100) -> list[Article]: + def get_top_headlines(self, total: int = 100) -> list[Article]: + return self.get_latest_news("", total) # same endpoint so just call the other method + + def get_latest_news(self, query: str, total: int = 100) -> list[Article]: params = self.get_base_params() params['currencies'] = query @@ -65,6 +75,3 @@ class CryptoPanicWrapper(NewsWrapper): json_response = response.json() articles = get_articles(json_response) return articles[:total] - - def get_latest_news(self, query: str, total: int = 100) -> list[Article]: - return self.get_top_headlines(query, total) # same endpoint for both, so just call it diff --git a/src/app/news/duckduckgo.py b/src/app/news/duckduckgo.py new file mode 100644 index 0000000..3a7c0bf --- /dev/null +++ b/src/app/news/duckduckgo.py @@ -0,0 +1,32 @@ +import json +from .base import Article, NewsWrapper +from agno.tools.duckduckgo import DuckDuckGoTools + +def create_article(result: dict) -> Article: + article = Article() + article.source = result.get("source", "") + article.time = result.get("date", "") + article.title = result.get("title", "") + article.description = result.get("body", "") + return article + +class DuckDuckGoWrapper(NewsWrapper): + """ + A wrapper for DuckDuckGo News search using the Tool from agno.tools.duckduckgo. + It can be rewritten to use direct API calls if needed in the future, but currently is easy to write and use. + """ + + def __init__(self): + self.tool = DuckDuckGoTools() + self.query = "crypto" + + def get_top_headlines(self, total: int = 100) -> list[Article]: + results = self.tool.duckduckgo_news(self.query, max_results=total) + json_results = json.loads(results) + return [create_article(result) for result in json_results] + + def get_latest_news(self, query: str, total: int = 100) -> list[Article]: + results = self.tool.duckduckgo_news(query or self.query, max_results=total) + json_results = json.loads(results) + return [create_article(result) for result in json_results] + diff --git a/src/app/news/gnews_api.py b/src/app/news/gnews_api.py index 53451c9..2e35f46 100644 --- a/src/app/news/gnews_api.py +++ b/src/app/news/gnews_api.py @@ -9,8 +9,13 @@ def result_to_article(result: dict) -> Article: article.description = result.get("description", "") return article -class GnewsWrapper(NewsWrapper): - def get_top_headlines(self, query: str, total: int = 100) -> list[Article]: +class GoogleNewsWrapper(NewsWrapper): + """ + A wrapper for the Google News RSS Feed (Documentation: https://github.com/ranahaani/GNews/?tab=readme-ov-file#about-gnews) + It does not require an API key and is free to use. + """ + + def get_top_headlines(self, total: int = 100) -> list[Article]: gnews = GNews(language='en', max_results=total, period='7d') results = gnews.get_top_news() diff --git a/src/app/news/news_api.py b/src/app/news/news_api.py index 9629ecd..0e6d684 100644 --- a/src/app/news/news_api.py +++ b/src/app/news/news_api.py @@ -11,6 +11,12 @@ def result_to_article(result: dict) -> Article: return article class NewsApiWrapper(NewsWrapper): + """ + A wrapper for the NewsAPI (Documentation: https://newsapi.org/docs/get-started) + Requires an API key set in the environment variable NEWS_API_KEY. + It is free to use, but has rate limits and restrictions based on the plan type (the free plan is 'developer' with 100 req/day). + """ + def __init__(self): api_key = os.getenv("NEWS_API_KEY") assert api_key is not None, "NEWS_API_KEY environment variable not set" @@ -20,13 +26,13 @@ class NewsApiWrapper(NewsWrapper): self.language = "en" # TODO Only English articles for now? self.max_page_size = 100 - def get_top_headlines(self, query: str, total: int = 100) -> list[Article]: + def get_top_headlines(self, total: int = 100) -> list[Article]: page_size = min(self.max_page_size, total) pages = (total // page_size) + (1 if total % page_size > 0 else 0) articles = [] for page in range(1, pages + 1): - headlines = self.client.get_top_headlines(q=query, category=self.category, language=self.language, page_size=page_size, page=page) + headlines = self.client.get_top_headlines(q="", category=self.category, language=self.language, page_size=page_size, page=page) results = [result_to_article(article) for article in headlines.get("articles", [])] articles.extend(results) return articles diff --git a/src/app/social/__init.py b/src/app/social/__init.py new file mode 100644 index 0000000..0d46bc8 --- /dev/null +++ b/src/app/social/__init.py @@ -0,0 +1 @@ +from .base import SocialWrapper \ No newline at end of file diff --git a/src/app/social/base.py b/src/app/social/base.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/api/test_cryptopanic_api.py b/tests/api/test_cryptopanic_api.py index dda2f77..c8020d3 100644 --- a/tests/api/test_cryptopanic_api.py +++ b/tests/api/test_cryptopanic_api.py @@ -27,7 +27,7 @@ class TestCryptoPanicAPI: # Useless since both methods use the same endpoint # def test_crypto_panic_api_get_top_headlines(self): # crypto = CryptoPanicWrapper() - # articles = crypto.get_top_headlines(query="crypto", total=2) + # articles = crypto.get_top_headlines(total=2) # assert isinstance(articles, list) # assert len(articles) == 2 # for article in articles: diff --git a/tests/api/test_duckduckgo_news.py b/tests/api/test_duckduckgo_news.py new file mode 100644 index 0000000..ea38272 --- /dev/null +++ b/tests/api/test_duckduckgo_news.py @@ -0,0 +1,35 @@ +import pytest +from app.news import DuckDuckGoWrapper + + +@pytest.mark.news +@pytest.mark.api +class TestDuckDuckGoNews: + + def test_news_api_initialization(self): + news = DuckDuckGoWrapper() + assert news.tool is not None + + def test_news_api_get_latest_news(self): + news = DuckDuckGoWrapper() + articles = news.get_latest_news(query="crypto", total=2) + assert isinstance(articles, list) + assert len(articles) == 2 + for article in articles: + assert article.source is not None or article.source != "" + assert article.time is not None or article.time != "" + assert article.title is not None or article.title != "" + assert article.description is not None or article.description != "" + + + def test_news_api_get_top_headlines(self): + news = DuckDuckGoWrapper() + articles = news.get_top_headlines(total=2) + assert isinstance(articles, list) + assert len(articles) == 2 + for article in articles: + assert article.source is not None or article.source != "" + assert article.time is not None or article.time != "" + assert article.title is not None or article.title != "" + assert article.description is not None or article.description != "" + diff --git a/tests/api/test_gnews_api.py b/tests/api/test_google_news.py similarity index 82% rename from tests/api/test_gnews_api.py rename to tests/api/test_google_news.py index 1013fa7..c7750f3 100644 --- a/tests/api/test_gnews_api.py +++ b/tests/api/test_google_news.py @@ -1,17 +1,17 @@ import pytest -from app.news import GnewsWrapper +from app.news import GoogleNewsWrapper @pytest.mark.news @pytest.mark.api -class TestGnewsAPI: +class TestGoogleNews: def test_gnews_api_initialization(self): - gnews_api = GnewsWrapper() + gnews_api = GoogleNewsWrapper() assert gnews_api is not None def test_gnews_api_get_latest_news(self): - gnews_api = GnewsWrapper() + gnews_api = GoogleNewsWrapper() articles = gnews_api.get_latest_news(query="crypto", total=2) assert isinstance(articles, list) assert len(articles) == 2 @@ -22,8 +22,8 @@ class TestGnewsAPI: assert article.description is not None or article.description != "" def test_gnews_api_get_top_headlines(self): - news_api = GnewsWrapper() - articles = news_api.get_top_headlines(query="crypto", total=2) + news_api = GoogleNewsWrapper() + articles = news_api.get_top_headlines(total=2) assert isinstance(articles, list) assert len(articles) == 2 for article in articles: diff --git a/tests/api/test_news_api.py b/tests/api/test_news_api.py index da8f607..927419b 100644 --- a/tests/api/test_news_api.py +++ b/tests/api/test_news_api.py @@ -26,7 +26,7 @@ class TestNewsAPI: def test_news_api_get_top_headlines(self): news_api = NewsApiWrapper() - articles = news_api.get_top_headlines(query="crypto", total=2) + articles = news_api.get_top_headlines(total=2) assert isinstance(articles, list) # assert len(articles) > 0 # apparently it doesn't always return SOME articles for article in articles: diff --git a/uv.lock b/uv.lock index 7eb69ba..ce4499b 100644 --- a/uv.lock +++ b/uv.lock @@ -169,6 +169,23 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/3d/d5/942051b45a9e883b5b6e98c041698b1eb2012d25e5948c58d6bf85b1bb43/Brotli-1.1.0-cp312-cp312-win_amd64.whl", hash = "sha256:906bc3a79de8c4ae5b86d3d75a8b77e44404b0f4261714306e3ad248d8ab0951", size = 357255, upload-time = "2023-09-07T14:04:17.83Z" }, ] +[[package]] +name = "brotlicffi" +version = "1.1.0.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "cffi" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/95/9d/70caa61192f570fcf0352766331b735afa931b4c6bc9a348a0925cc13288/brotlicffi-1.1.0.0.tar.gz", hash = "sha256:b77827a689905143f87915310b93b273ab17888fd43ef350d4832c4a71083c13", size = 465192, upload-time = "2023-09-14T14:22:40.707Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/a2/11/7b96009d3dcc2c931e828ce1e157f03824a69fb728d06bfd7b2fc6f93718/brotlicffi-1.1.0.0-cp37-abi3-macosx_10_9_x86_64.whl", hash = "sha256:9b7ae6bd1a3f0df532b6d67ff674099a96d22bc0948955cb338488c31bfb8851", size = 453786, upload-time = "2023-09-14T14:21:57.72Z" }, + { url = "https://files.pythonhosted.org/packages/d6/e6/a8f46f4a4ee7856fbd6ac0c6fb0dc65ed181ba46cd77875b8d9bbe494d9e/brotlicffi-1.1.0.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:19ffc919fa4fc6ace69286e0a23b3789b4219058313cf9b45625016bf7ff996b", size = 2911165, upload-time = "2023-09-14T14:21:59.613Z" }, + { url = "https://files.pythonhosted.org/packages/be/20/201559dff14e83ba345a5ec03335607e47467b6633c210607e693aefac40/brotlicffi-1.1.0.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9feb210d932ffe7798ee62e6145d3a757eb6233aa9a4e7db78dd3690d7755814", size = 2927895, upload-time = "2023-09-14T14:22:01.22Z" }, + { url = "https://files.pythonhosted.org/packages/cd/15/695b1409264143be3c933f708a3f81d53c4a1e1ebbc06f46331decbf6563/brotlicffi-1.1.0.0-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:84763dbdef5dd5c24b75597a77e1b30c66604725707565188ba54bab4f114820", size = 2851834, upload-time = "2023-09-14T14:22:03.571Z" }, + { url = "https://files.pythonhosted.org/packages/b4/40/b961a702463b6005baf952794c2e9e0099bde657d0d7e007f923883b907f/brotlicffi-1.1.0.0-cp37-abi3-win32.whl", hash = "sha256:1b12b50e07c3911e1efa3a8971543e7648100713d4e0971b13631cce22c587eb", size = 341731, upload-time = "2023-09-14T14:22:05.74Z" }, + { url = "https://files.pythonhosted.org/packages/1c/fa/5408a03c041114ceab628ce21766a4ea882aa6f6f0a800e04ee3a30ec6b9/brotlicffi-1.1.0.0-cp37-abi3-win_amd64.whl", hash = "sha256:994a4f0681bb6c6c3b0925530a1926b7a189d878e6e5e38fae8efa47c5d9c613", size = 366783, upload-time = "2023-09-14T14:22:07.096Z" }, +] + [[package]] name = "cachetools" version = "5.5.2" @@ -323,6 +340,21 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/87/22/f020c047ae1346613db9322638186468238bcfa8849b4668a22b97faad65/dateparser-1.2.2-py3-none-any.whl", hash = "sha256:5a5d7211a09013499867547023a2a0c91d5a27d15dd4dbcea676ea9fe66f2482", size = 315453, upload-time = "2025-06-26T09:29:21.412Z" }, ] +[[package]] +name = "ddgs" +version = "9.6.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "click" }, + { name = "httpx", extra = ["brotli", "http2", "socks"] }, + { name = "lxml" }, + { name = "primp" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/db/45/7a408de2cd89855403ea18ed776f12c291eabe7dd54bc5b00f7cdb43f8ba/ddgs-9.6.0.tar.gz", hash = "sha256:8caf555d4282c1cf5c15969994ad55f4239bd15e97cf004a5da8f1cad37529bf", size = 35865, upload-time = "2025-09-17T13:27:10.533Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/40/cd/ef820662e0d87f46b829bba7e2324c7978e0153692bbd2f08f7746049708/ddgs-9.6.0-py3-none-any.whl", hash = "sha256:24120f1b672fd3a28309db029e7038eb3054381730aea7a08d51bb909dd55520", size = 41558, upload-time = "2025-09-17T13:27:08.99Z" }, +] + [[package]] name = "dnspython" version = "2.8.0" @@ -577,6 +609,19 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/04/4b/29cac41a4d98d144bf5f6d33995617b185d14b22401f75ca86f384e87ff1/h11-0.16.0-py3-none-any.whl", hash = "sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86", size = 37515, upload-time = "2025-04-24T03:35:24.344Z" }, ] +[[package]] +name = "h2" +version = "4.3.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "hpack" }, + { name = "hyperframe" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/1d/17/afa56379f94ad0fe8defd37d6eb3f89a25404ffc71d4d848893d270325fc/h2-4.3.0.tar.gz", hash = "sha256:6c59efe4323fa18b47a632221a1888bd7fde6249819beda254aeca909f221bf1", size = 2152026, upload-time = "2025-08-23T18:12:19.778Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/69/b2/119f6e6dcbd96f9069ce9a2665e0146588dc9f88f29549711853645e736a/h2-4.3.0-py3-none-any.whl", hash = "sha256:c438f029a25f7945c69e0ccf0fb951dc3f73a5f6412981daee861431b70e2bdd", size = 61779, upload-time = "2025-08-23T18:12:17.779Z" }, +] + [[package]] name = "hf-xet" version = "1.1.10" @@ -592,6 +637,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/ee/0e/471f0a21db36e71a2f1752767ad77e92d8cde24e974e03d662931b1305ec/hf_xet-1.1.10-cp37-abi3-win_amd64.whl", hash = "sha256:5f54b19cc347c13235ae7ee98b330c26dd65ef1df47e5316ffb1e87713ca7045", size = 2804691, upload-time = "2025-09-12T20:10:28.433Z" }, ] +[[package]] +name = "hpack" +version = "4.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/2c/48/71de9ed269fdae9c8057e5a4c0aa7402e8bb16f2c6e90b3aa53327b113f8/hpack-4.1.0.tar.gz", hash = "sha256:ec5eca154f7056aa06f196a557655c5b009b382873ac8d1e66e79e87535f1dca", size = 51276, upload-time = "2025-01-22T21:44:58.347Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/07/c6/80c95b1b2b94682a72cbdbfb85b81ae2daffa4291fbfa1b1464502ede10d/hpack-4.1.0-py3-none-any.whl", hash = "sha256:157ac792668d995c657d93111f46b4535ed114f0c9c8d672271bbec7eae1b496", size = 34357, upload-time = "2025-01-22T21:44:56.92Z" }, +] + [[package]] name = "httpcore" version = "1.0.9" @@ -620,6 +674,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/2a/39/e50c7c3a983047577ee07d2a9e53faf5a69493943ec3f6a384bdc792deb2/httpx-0.28.1-py3-none-any.whl", hash = "sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad", size = 73517, upload-time = "2024-12-06T15:37:21.509Z" }, ] +[package.optional-dependencies] +brotli = [ + { name = "brotli", marker = "platform_python_implementation == 'CPython'" }, + { name = "brotlicffi", marker = "platform_python_implementation != 'CPython'" }, +] +http2 = [ + { name = "h2" }, +] +socks = [ + { name = "socksio" }, +] + [[package]] name = "huggingface-hub" version = "0.35.0" @@ -639,6 +705,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/fe/85/a18508becfa01f1e4351b5e18651b06d210dbd96debccd48a452acccb901/huggingface_hub-0.35.0-py3-none-any.whl", hash = "sha256:f2e2f693bca9a26530b1c0b9bcd4c1495644dad698e6a0060f90e22e772c31e9", size = 563436, upload-time = "2025-09-16T13:49:30.627Z" }, ] +[[package]] +name = "hyperframe" +version = "6.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/02/e7/94f8232d4a74cc99514c13a9f995811485a6903d48e5d952771ef6322e30/hyperframe-6.1.0.tar.gz", hash = "sha256:f630908a00854a7adeabd6382b43923a4c4cd4b821fcb527e6ab9e15382a3b08", size = 26566, upload-time = "2025-01-22T21:41:49.302Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/48/30/47d0bf6072f7252e6521f3447ccfa40b421b6824517f82854703d0f5a98b/hyperframe-6.1.0-py3-none-any.whl", hash = "sha256:b03380493a519fce58ea5af42e4a42317bf9bd425596f7a0835ffce80f1a42e5", size = 13007, upload-time = "2025-01-22T21:41:47.295Z" }, +] + [[package]] name = "idna" version = "3.10" @@ -669,6 +744,32 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/62/a1/3d680cbfd5f4b8f15abc1d571870c5fc3e594bb582bc3b64ea099db13e56/jinja2-3.1.6-py3-none-any.whl", hash = "sha256:85ece4451f492d0c13c5dd7c13a64681a86afae63a5f347908daf103ce6d2f67", size = 134899, upload-time = "2025-03-05T20:05:00.369Z" }, ] +[[package]] +name = "lxml" +version = "6.0.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/aa/88/262177de60548e5a2bfc46ad28232c9e9cbde697bd94132aeb80364675cb/lxml-6.0.2.tar.gz", hash = "sha256:cd79f3367bd74b317dda655dc8fcfa304d9eb6e4fb06b7168c5cf27f96e0cd62", size = 4073426, upload-time = "2025-09-22T04:04:59.287Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/f3/c8/8ff2bc6b920c84355146cd1ab7d181bc543b89241cfb1ebee824a7c81457/lxml-6.0.2-cp312-cp312-macosx_10_13_universal2.whl", hash = "sha256:a59f5448ba2ceccd06995c95ea59a7674a10de0810f2ce90c9006f3cbc044456", size = 8661887, upload-time = "2025-09-22T04:01:17.265Z" }, + { url = "https://files.pythonhosted.org/packages/37/6f/9aae1008083bb501ef63284220ce81638332f9ccbfa53765b2b7502203cf/lxml-6.0.2-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:e8113639f3296706fbac34a30813929e29247718e88173ad849f57ca59754924", size = 4667818, upload-time = "2025-09-22T04:01:19.688Z" }, + { url = "https://files.pythonhosted.org/packages/f1/ca/31fb37f99f37f1536c133476674c10b577e409c0a624384147653e38baf2/lxml-6.0.2-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:a8bef9b9825fa8bc816a6e641bb67219489229ebc648be422af695f6e7a4fa7f", size = 4950807, upload-time = "2025-09-22T04:01:21.487Z" }, + { url = "https://files.pythonhosted.org/packages/da/87/f6cb9442e4bada8aab5ae7e1046264f62fdbeaa6e3f6211b93f4c0dd97f1/lxml-6.0.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:65ea18d710fd14e0186c2f973dc60bb52039a275f82d3c44a0e42b43440ea534", size = 5109179, upload-time = "2025-09-22T04:01:23.32Z" }, + { url = "https://files.pythonhosted.org/packages/c8/20/a7760713e65888db79bbae4f6146a6ae5c04e4a204a3c48896c408cd6ed2/lxml-6.0.2-cp312-cp312-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c371aa98126a0d4c739ca93ceffa0fd7a5d732e3ac66a46e74339acd4d334564", size = 5023044, upload-time = "2025-09-22T04:01:25.118Z" }, + { url = "https://files.pythonhosted.org/packages/a2/b0/7e64e0460fcb36471899f75831509098f3fd7cd02a3833ac517433cb4f8f/lxml-6.0.2-cp312-cp312-manylinux_2_26_i686.manylinux_2_28_i686.whl", hash = "sha256:700efd30c0fa1a3581d80a748157397559396090a51d306ea59a70020223d16f", size = 5359685, upload-time = "2025-09-22T04:01:27.398Z" }, + { url = "https://files.pythonhosted.org/packages/b9/e1/e5df362e9ca4e2f48ed6411bd4b3a0ae737cc842e96877f5bf9428055ab4/lxml-6.0.2-cp312-cp312-manylinux_2_26_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:c33e66d44fe60e72397b487ee92e01da0d09ba2d66df8eae42d77b6d06e5eba0", size = 5654127, upload-time = "2025-09-22T04:01:29.629Z" }, + { url = "https://files.pythonhosted.org/packages/c6/d1/232b3309a02d60f11e71857778bfcd4acbdb86c07db8260caf7d008b08f8/lxml-6.0.2-cp312-cp312-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:90a345bbeaf9d0587a3aaffb7006aa39ccb6ff0e96a57286c0cb2fd1520ea192", size = 5253958, upload-time = "2025-09-22T04:01:31.535Z" }, + { url = "https://files.pythonhosted.org/packages/35/35/d955a070994725c4f7d80583a96cab9c107c57a125b20bb5f708fe941011/lxml-6.0.2-cp312-cp312-manylinux_2_31_armv7l.whl", hash = "sha256:064fdadaf7a21af3ed1dcaa106b854077fbeada827c18f72aec9346847cd65d0", size = 4711541, upload-time = "2025-09-22T04:01:33.801Z" }, + { url = "https://files.pythonhosted.org/packages/1e/be/667d17363b38a78c4bd63cfd4b4632029fd68d2c2dc81f25ce9eb5224dd5/lxml-6.0.2-cp312-cp312-manylinux_2_38_riscv64.manylinux_2_39_riscv64.whl", hash = "sha256:fbc74f42c3525ac4ffa4b89cbdd00057b6196bcefe8bce794abd42d33a018092", size = 5267426, upload-time = "2025-09-22T04:01:35.639Z" }, + { url = "https://files.pythonhosted.org/packages/ea/47/62c70aa4a1c26569bc958c9ca86af2bb4e1f614e8c04fb2989833874f7ae/lxml-6.0.2-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:6ddff43f702905a4e32bc24f3f2e2edfe0f8fde3277d481bffb709a4cced7a1f", size = 5064917, upload-time = "2025-09-22T04:01:37.448Z" }, + { url = "https://files.pythonhosted.org/packages/bd/55/6ceddaca353ebd0f1908ef712c597f8570cc9c58130dbb89903198e441fd/lxml-6.0.2-cp312-cp312-musllinux_1_2_armv7l.whl", hash = "sha256:6da5185951d72e6f5352166e3da7b0dc27aa70bd1090b0eb3f7f7212b53f1bb8", size = 4788795, upload-time = "2025-09-22T04:01:39.165Z" }, + { url = "https://files.pythonhosted.org/packages/cf/e8/fd63e15da5e3fd4c2146f8bbb3c14e94ab850589beab88e547b2dbce22e1/lxml-6.0.2-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:57a86e1ebb4020a38d295c04fc79603c7899e0df71588043eb218722dabc087f", size = 5676759, upload-time = "2025-09-22T04:01:41.506Z" }, + { url = "https://files.pythonhosted.org/packages/76/47/b3ec58dc5c374697f5ba37412cd2728f427d056315d124dd4b61da381877/lxml-6.0.2-cp312-cp312-musllinux_1_2_riscv64.whl", hash = "sha256:2047d8234fe735ab77802ce5f2297e410ff40f5238aec569ad7c8e163d7b19a6", size = 5255666, upload-time = "2025-09-22T04:01:43.363Z" }, + { url = "https://files.pythonhosted.org/packages/19/93/03ba725df4c3d72afd9596eef4a37a837ce8e4806010569bedfcd2cb68fd/lxml-6.0.2-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:6f91fd2b2ea15a6800c8e24418c0775a1694eefc011392da73bc6cef2623b322", size = 5277989, upload-time = "2025-09-22T04:01:45.215Z" }, + { url = "https://files.pythonhosted.org/packages/c6/80/c06de80bfce881d0ad738576f243911fccf992687ae09fd80b734712b39c/lxml-6.0.2-cp312-cp312-win32.whl", hash = "sha256:3ae2ce7d6fedfb3414a2b6c5e20b249c4c607f72cb8d2bb7cc9c6ec7c6f4e849", size = 3611456, upload-time = "2025-09-22T04:01:48.243Z" }, + { url = "https://files.pythonhosted.org/packages/f7/d7/0cdfb6c3e30893463fb3d1e52bc5f5f99684a03c29a0b6b605cfae879cd5/lxml-6.0.2-cp312-cp312-win_amd64.whl", hash = "sha256:72c87e5ee4e58a8354fb9c7c84cbf95a1c8236c127a5d1b7683f04bed8361e1f", size = 4011793, upload-time = "2025-09-22T04:01:50.042Z" }, + { url = "https://files.pythonhosted.org/packages/ea/7b/93c73c67db235931527301ed3785f849c78991e2e34f3fd9a6663ffda4c5/lxml-6.0.2-cp312-cp312-win_arm64.whl", hash = "sha256:61cb10eeb95570153e0c0e554f58df92ecf5109f75eacad4a95baa709e26c3d6", size = 3672836, upload-time = "2025-09-22T04:01:52.145Z" }, +] + [[package]] name = "markdown-it-py" version = "4.0.0" @@ -860,6 +961,22 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/54/20/4d324d65cc6d9205fabedc306948156824eb9f0ee1633355a8f7ec5c66bf/pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746", size = 20538, upload-time = "2025-05-15T12:30:06.134Z" }, ] +[[package]] +name = "primp" +version = "0.15.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/56/0b/a87556189da4de1fc6360ca1aa05e8335509633f836cdd06dd17f0743300/primp-0.15.0.tar.gz", hash = "sha256:1af8ea4b15f57571ff7fc5e282a82c5eb69bc695e19b8ddeeda324397965b30a", size = 113022, upload-time = "2025-04-17T11:41:05.315Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/f5/5a/146ac964b99ea7657ad67eb66f770be6577dfe9200cb28f9a95baffd6c3f/primp-0.15.0-cp38-abi3-macosx_10_12_x86_64.whl", hash = "sha256:1b281f4ca41a0c6612d4c6e68b96e28acfe786d226a427cd944baa8d7acd644f", size = 3178914, upload-time = "2025-04-17T11:40:59.558Z" }, + { url = "https://files.pythonhosted.org/packages/bc/8a/cc2321e32db3ce64d6e32950d5bcbea01861db97bfb20b5394affc45b387/primp-0.15.0-cp38-abi3-macosx_11_0_arm64.whl", hash = "sha256:489cbab55cd793ceb8f90bb7423c6ea64ebb53208ffcf7a044138e3c66d77299", size = 2955079, upload-time = "2025-04-17T11:40:57.398Z" }, + { url = "https://files.pythonhosted.org/packages/c3/7b/cbd5d999a07ff2a21465975d4eb477ae6f69765e8fe8c9087dab250180d8/primp-0.15.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c18b45c23f94016215f62d2334552224236217aaeb716871ce0e4dcfa08eb161", size = 3281018, upload-time = "2025-04-17T11:40:55.308Z" }, + { url = "https://files.pythonhosted.org/packages/1b/6e/a6221c612e61303aec2bcac3f0a02e8b67aee8c0db7bdc174aeb8010f975/primp-0.15.0-cp38-abi3-manylinux_2_34_aarch64.whl", hash = "sha256:e985a9cba2e3f96a323722e5440aa9eccaac3178e74b884778e926b5249df080", size = 3255229, upload-time = "2025-04-17T11:40:47.811Z" }, + { url = "https://files.pythonhosted.org/packages/3b/54/bfeef5aca613dc660a69d0760a26c6b8747d8fdb5a7f20cb2cee53c9862f/primp-0.15.0-cp38-abi3-manylinux_2_34_armv7l.whl", hash = "sha256:6b84a6ffa083e34668ff0037221d399c24d939b5629cd38223af860de9e17a83", size = 3014522, upload-time = "2025-04-17T11:40:50.191Z" }, + { url = "https://files.pythonhosted.org/packages/ac/96/84078e09f16a1dad208f2fe0f8a81be2cf36e024675b0f9eec0c2f6e2182/primp-0.15.0-cp38-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:592f6079646bdf5abbbfc3b0a28dac8de943f8907a250ce09398cda5eaebd260", size = 3418567, upload-time = "2025-04-17T11:41:01.595Z" }, + { url = "https://files.pythonhosted.org/packages/6c/80/8a7a9587d3eb85be3d0b64319f2f690c90eb7953e3f73a9ddd9e46c8dc42/primp-0.15.0-cp38-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:5a728e5a05f37db6189eb413d22c78bd143fa59dd6a8a26dacd43332b3971fe8", size = 3606279, upload-time = "2025-04-17T11:41:03.61Z" }, + { url = "https://files.pythonhosted.org/packages/0c/dd/f0183ed0145e58cf9d286c1b2c14f63ccee987a4ff79ac85acc31b5d86bd/primp-0.15.0-cp38-abi3-win_amd64.whl", hash = "sha256:aeb6bd20b06dfc92cfe4436939c18de88a58c640752cf7f30d9e4ae893cdec32", size = 3149967, upload-time = "2025-04-17T11:41:07.067Z" }, +] + [[package]] name = "propcache" version = "0.3.2" @@ -1255,6 +1372,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235, upload-time = "2024-02-25T23:20:01.196Z" }, ] +[[package]] +name = "socksio" +version = "1.0.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f8/5c/48a7d9495be3d1c651198fd99dbb6ce190e2274d0f28b9051307bdec6b85/socksio-1.0.0.tar.gz", hash = "sha256:f88beb3da5b5c38b9890469de67d0cb0f9d494b78b106ca1845f96c10b91c4ac", size = 19055, upload-time = "2020-04-17T15:50:34.664Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/37/c3/6eeb6034408dac0fa653d126c9204ade96b819c936e136c5e8a6897eee9c/socksio-1.0.0-py3-none-any.whl", hash = "sha256:95dc1f15f9b34e8d7b16f06d74b8ccf48f609af32ab33c608d08761c5dcbb1f3", size = 12763, upload-time = "2020-04-17T15:50:31.878Z" }, +] + [[package]] name = "soupsieve" version = "2.8" @@ -1371,6 +1497,7 @@ source = { virtual = "." } dependencies = [ { name = "agno" }, { name = "coinbase-advanced-py" }, + { name = "ddgs" }, { name = "dotenv" }, { name = "gnews" }, { name = "google-genai" }, @@ -1385,6 +1512,7 @@ dependencies = [ requires-dist = [ { name = "agno" }, { name = "coinbase-advanced-py" }, + { name = "ddgs" }, { name = "dotenv" }, { name = "gnews" }, { name = "google-genai" }, From fcbb312d081e68cd2b4b23fa03c837a4053561eb Mon Sep 17 00:00:00 2001 From: trojanhorse47 Date: Tue, 30 Sep 2025 12:28:44 +0200 Subject: [PATCH 12/22] - Refactor struttura progetto: divisione tra agent e toolkit --- src/__init__.py | 0 src/app.py | 18 ++-- src/app/agents/market_agent.py | 92 ++++++++++++++++++ src/app/agents/news_agent.py | 32 +++++- src/app/agents/social_agent.py | 33 ++++++- src/app/markets/__init__.py | 3 +- src/app/markets/coinbase.py | 3 + src/app/markets/cryptocompare.py | 2 + src/app/models.py | 7 +- src/app/pipeline.py | 84 ++++++++++++++++ src/app/{agents => }/predictor.py | 5 +- src/app/tool.py | 97 ------------------- src/app/toolkits/__init__.py | 0 .../market.py => toolkits/market_toolkit.py} | 2 + tests/agents/test_market.py | 2 +- 15 files changed, 266 insertions(+), 114 deletions(-) create mode 100644 src/__init__.py create mode 100644 src/app/agents/market_agent.py create mode 100644 src/app/pipeline.py rename src/app/{agents => }/predictor.py (99%) delete mode 100644 src/app/tool.py create mode 100644 src/app/toolkits/__init__.py rename src/app/{agents/market.py => toolkits/market_toolkit.py} (99%) diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/app.py b/src/app.py index 983779e..8e9e787 100644 --- a/src/app.py +++ b/src/app.py @@ -1,8 +1,8 @@ import gradio as gr - -from dotenv import load_dotenv -from app.tool import ToolAgent from agno.utils.log import log_info +from dotenv import load_dotenv + +from app.pipeline import Pipeline ######################################## # MAIN APP & GRADIO INTERFACE @@ -16,31 +16,31 @@ if __name__ == "__main__": load_dotenv() ###################################### - tool_agent = ToolAgent() + pipeline = Pipeline() with gr.Blocks() as demo: gr.Markdown("# 🤖 Agente di Analisi e Consulenza Crypto") with gr.Row(): provider = gr.Dropdown( - choices=tool_agent.list_providers(), + choices=pipeline.list_providers(), type="index", label="Modello da usare" ) - provider.change(fn=tool_agent.choose_provider, inputs=provider, outputs=None) + provider.change(fn=pipeline.choose_provider, inputs=provider, outputs=None) style = gr.Dropdown( - choices=tool_agent.list_styles(), + choices=pipeline.list_styles(), type="index", label="Stile di investimento" ) - style.change(fn=tool_agent.choose_style, inputs=style, outputs=None) + style.change(fn=pipeline.choose_style, inputs=style, outputs=None) user_input = gr.Textbox(label="Richiesta utente") output = gr.Textbox(label="Risultato analisi", lines=12) analyze_btn = gr.Button("🔎 Analizza") - analyze_btn.click(fn=tool_agent.interact, inputs=[user_input], outputs=output) + analyze_btn.click(fn=pipeline.interact, inputs=[user_input], outputs=output) server, port = ("0.0.0.0", 8000) log_info(f"Starting UPO AppAI on http://{server}:{port}") diff --git a/src/app/agents/market_agent.py b/src/app/agents/market_agent.py new file mode 100644 index 0000000..94d01d5 --- /dev/null +++ b/src/app/agents/market_agent.py @@ -0,0 +1,92 @@ +from typing import Union, List, Dict, Optional, Any, Iterator, Sequence +from agno.agent import Agent +from agno.models.message import Message +from agno.run.agent import RunOutput, RunOutputEvent +from pydantic import BaseModel + +from src.app.toolkits.market_toolkit import MarketToolkit +from src.app.markets.base import ProductInfo # modello dati già definito nel tuo progetto + + +class MarketAgent(Agent): + """ + Wrapper che trasforma MarketToolkit in un Agent compatibile con Team. + Produce sia output leggibile (content) che dati strutturati (metadata). + """ + + def __init__(self, currency: str = "USD"): + super().__init__() + self.toolkit = MarketToolkit() + self.currency = currency + self.name = "MarketAgent" + + def run( + self, + input: Union[str, List, Dict, Message, BaseModel, List[Message]], + *, + stream: Optional[bool] = None, + stream_intermediate_steps: Optional[bool] = None, + user_id: Optional[str] = None, + session_id: Optional[str] = None, + session_state: Optional[Dict[str, Any]] = None, + audio: Optional[Sequence[Any]] = None, + images: Optional[Sequence[Any]] = None, + videos: Optional[Sequence[Any]] = None, + files: Optional[Sequence[Any]] = None, + retries: Optional[int] = None, + knowledge_filters: Optional[Dict[str, Any]] = None, + add_history_to_context: Optional[bool] = None, + add_dependencies_to_context: Optional[bool] = None, + add_session_state_to_context: Optional[bool] = None, + dependencies: Optional[Dict[str, Any]] = None, + metadata: Optional[Dict[str, Any]] = None, + yield_run_response: bool = False, + debug_mode: Optional[bool] = None, + **kwargs: Any, + ) -> Union[RunOutput, Iterator[Union[RunOutputEvent, RunOutput]]]: + # 1. Estraggo la query dal parametro "input" + if isinstance(input, str): + query = input + elif isinstance(input, dict) and "query" in input: + query = input["query"] + elif isinstance(input, Message): + query = input.content + elif isinstance(input, BaseModel): + query = str(input) + elif isinstance(input, list) and input and isinstance(input[0], Message): + query = input[0].content + else: + query = str(input) + + # 2. Individuo i simboli da analizzare + symbols = [] + for token in query.upper().split(): + if token in ("BTC", "ETH", "XRP", "LTC", "BCH"): # TODO: estendere dinamicamente + symbols.append(token) + + if not symbols: + symbols = ["BTC", "ETH"] # default + + # 3. Recupero i dati dal toolkit + results = [] + products: List[ProductInfo] = [] + + for sym in symbols: + try: + product = self.toolkit.get_current_price(sym) # supponiamo ritorni un ProductInfo o simile + if isinstance(product, list): + products.extend(product) + else: + products.append(product) + + results.append(f"{sym}: {product.price if hasattr(product, 'price') else product}") + except Exception as e: + results.append(f"{sym}: errore ({e})") + + # 4. Preparo output leggibile + metadati strutturati + output_text = "📊 Dati di mercato:\n" + "\n".join(results) + + return RunOutput( + content=output_text, + metadata={"products": products} + ) diff --git a/src/app/agents/news_agent.py b/src/app/agents/news_agent.py index 831d5b3..d6de5e5 100644 --- a/src/app/agents/news_agent.py +++ b/src/app/agents/news_agent.py @@ -1,4 +1,34 @@ -class NewsAgent: +from agno.agent import Agent + +class NewsAgent(Agent): + """ + Gli agenti devono esporre un metodo run con questa firma. + + def run( + self, + input: Union[str, List, Dict, Message, BaseModel, List[Message]], + *, + stream: Optional[bool] = None, + stream_intermediate_steps: Optional[bool] = None, + user_id: Optional[str] = None, + session_id: Optional[str] = None, + session_state: Optional[Dict[str, Any]] = None, + audio: Optional[Sequence[Any]] = None, + images: Optional[Sequence[Any]] = None, + videos: Optional[Sequence[Any]] = None, + files: Optional[Sequence[Any]] = None, + retries: Optional[int] = None, + knowledge_filters: Optional[Dict[str, Any]] = None, + add_history_to_context: Optional[bool] = None, + add_dependencies_to_context: Optional[bool] = None, + add_session_state_to_context: Optional[bool] = None, + dependencies: Optional[Dict[str, Any]] = None, + metadata: Optional[Dict[str, Any]] = None, + yield_run_response: bool = False, + debug_mode: Optional[bool] = None, + **kwargs: Any, + ) -> Union[RunOutput, Iterator[Union[RunOutputEvent, RunOutput]]]: + """ @staticmethod def analyze(query: str) -> str: # Mock analisi news diff --git a/src/app/agents/social_agent.py b/src/app/agents/social_agent.py index 1ec2fb5..cefa7ef 100644 --- a/src/app/agents/social_agent.py +++ b/src/app/agents/social_agent.py @@ -1,4 +1,35 @@ -class SocialAgent: +from agno.agent import Agent + + +class SocialAgent(Agent): + """ + Gli agenti devono esporre un metodo run con questa firma. + + def run( + self, + input: Union[str, List, Dict, Message, BaseModel, List[Message]], + *, + stream: Optional[bool] = None, + stream_intermediate_steps: Optional[bool] = None, + user_id: Optional[str] = None, + session_id: Optional[str] = None, + session_state: Optional[Dict[str, Any]] = None, + audio: Optional[Sequence[Any]] = None, + images: Optional[Sequence[Any]] = None, + videos: Optional[Sequence[Any]] = None, + files: Optional[Sequence[Any]] = None, + retries: Optional[int] = None, + knowledge_filters: Optional[Dict[str, Any]] = None, + add_history_to_context: Optional[bool] = None, + add_dependencies_to_context: Optional[bool] = None, + add_session_state_to_context: Optional[bool] = None, + dependencies: Optional[Dict[str, Any]] = None, + metadata: Optional[Dict[str, Any]] = None, + yield_run_response: bool = False, + debug_mode: Optional[bool] = None, + **kwargs: Any, + ) -> Union[RunOutput, Iterator[Union[RunOutputEvent, RunOutput]]]: + """ @staticmethod def analyze(query: str) -> str: # Mock analisi social diff --git a/src/app/markets/__init__.py b/src/app/markets/__init__.py index cb27b23..b965637 100644 --- a/src/app/markets/__init__.py +++ b/src/app/markets/__init__.py @@ -1,8 +1,9 @@ +from agno.utils.log import log_warning + from src.app.markets.base import BaseWrapper from src.app.markets.coinbase import CoinBaseWrapper from src.app.markets.cryptocompare import CryptoCompareWrapper -from agno.utils.log import log_warning class MarketAPIs(BaseWrapper): """ diff --git a/src/app/markets/coinbase.py b/src/app/markets/coinbase.py index 2e0e779..9d2e4be 100644 --- a/src/app/markets/coinbase.py +++ b/src/app/markets/coinbase.py @@ -1,7 +1,10 @@ import os + from coinbase.rest import RESTClient + from src.app.markets.base import ProductInfo, BaseWrapper, Price + class CoinBaseWrapper(BaseWrapper): """ Wrapper per le API di Coinbase. diff --git a/src/app/markets/cryptocompare.py b/src/app/markets/cryptocompare.py index a6e50a0..5ba44b4 100644 --- a/src/app/markets/cryptocompare.py +++ b/src/app/markets/cryptocompare.py @@ -1,5 +1,7 @@ import os + import requests + from src.app.markets.base import ProductInfo, BaseWrapper, Price BASE_URL = "https://min-api.cryptocompare.com" diff --git a/src/app/models.py b/src/app/models.py index 94f7da2..f7ec9b8 100644 --- a/src/app/models.py +++ b/src/app/models.py @@ -1,13 +1,14 @@ import os -import requests from enum import Enum -from pydantic import BaseModel + +import requests from agno.agent import Agent from agno.models.base import Model from agno.models.google import Gemini from agno.models.ollama import Ollama - from agno.utils.log import log_warning +from pydantic import BaseModel + class AppModels(Enum): """ diff --git a/src/app/pipeline.py b/src/app/pipeline.py new file mode 100644 index 0000000..9e0e2bd --- /dev/null +++ b/src/app/pipeline.py @@ -0,0 +1,84 @@ +from typing import List + +from agno.team import Team +from agno.utils.log import log_info + +from app.agents.market_agent import MarketAgent +from src.app.agents.news_agent import NewsAgent +from src.app.agents.social_agent import SocialAgent +from src.app.markets import MarketAPIs +from src.app.models import AppModels +from src.app.predictor import PredictorStyle, PredictorInput, PredictorOutput, PREDICTOR_INSTRUCTIONS + + +class Pipeline: + """ + Pipeline coordinata: esegue tutti gli agenti del Team, aggrega i risultati e invoca il Predictor. + """ + + def __init__(self): + # Inizializza gli agenti + self.market_agent = MarketAgent() + self.news_agent = NewsAgent() + self.social_agent = SocialAgent() + + # Crea il Team + self.team = Team(name="CryptoAnalysisTeam", members=[self.market_agent, self.news_agent, self.social_agent]) + + # Modelli disponibili e Predictor + self.available_models = AppModels.availables() + self.predictor_model = self.available_models[0] + self.predictor = self.predictor_model.get_agent(PREDICTOR_INSTRUCTIONS, output=PredictorOutput) # type: ignore[arg-type] + + # Stili + self.styles = list(PredictorStyle) + self.style = self.styles[0] + + def choose_provider(self, index: int): + self.predictor_model = self.available_models[index] + self.predictor = self.predictor_model.get_agent(PREDICTOR_INSTRUCTIONS, output=PredictorOutput) # type: ignore[arg-type] + + def choose_style(self, index: int): + self.style = self.styles[index] + + def interact(self, query: str) -> str: + """ + Esegue il Team (Market + News + Social), aggrega i risultati e invoca il Predictor. + """ + # Step 1: raccogli output del Team + team_results = self.team.run(query) + if isinstance(team_results, dict): # alcuni Team possono restituire dict + pieces = [str(v) for v in team_results.values()] + elif isinstance(team_results, list): + pieces = [str(r) for r in team_results] + else: + pieces = [str(team_results)] + aggregated_text = "\n\n".join(pieces) + + # Step 2: prepara input per Predictor + predictor_input = PredictorInput( + data=[], # TODO: mappare meglio i dati di mercato in ProductInfo + style=self.style, + sentiment=aggregated_text + ) + + # Step 3: chiama Predictor + result = self.predictor.run(predictor_input) + prediction: PredictorOutput = result.content + + # Step 4: formatta output finale + portfolio_lines = "\n".join( + [f"{item.asset} ({item.percentage}%): {item.motivation}" for item in prediction.portfolio] + ) + output = ( + f"📊 Strategia ({self.style.value}): {prediction.strategy}\n\n" + f"💼 Portafoglio consigliato:\n{portfolio_lines}" + ) + + return output + + def list_providers(self) -> List[str]: + return [m.name for m in self.available_models] + + def list_styles(self) -> List[str]: + return [s.value for s in self.styles] diff --git a/src/app/agents/predictor.py b/src/app/predictor.py similarity index 99% rename from src/app/agents/predictor.py rename to src/app/predictor.py index 09d7a26..101b4ac 100644 --- a/src/app/agents/predictor.py +++ b/src/app/predictor.py @@ -1,7 +1,10 @@ from enum import Enum -from src.app.markets.base import ProductInfo + from pydantic import BaseModel, Field +from src.app.markets.base import ProductInfo + + class PredictorStyle(Enum): CONSERVATIVE = "Conservativo" AGGRESSIVE = "Aggressivo" diff --git a/src/app/tool.py b/src/app/tool.py deleted file mode 100644 index f1ca560..0000000 --- a/src/app/tool.py +++ /dev/null @@ -1,97 +0,0 @@ -from src.app.agents.news_agent import NewsAgent -from src.app.agents.social_agent import SocialAgent -from src.app.agents.predictor import PredictorStyle, PredictorInput, PredictorOutput, PREDICTOR_INSTRUCTIONS -from src.app.markets import MarketAPIs -from src.app.models import AppModels -from agno.utils.log import log_info - -class ToolAgent: - """ - Classe principale che coordina gli agenti per rispondere alle richieste dell'utente. - """ - - def __init__(self): - """ - Inizializza l'agente con i modelli disponibili, gli stili e l'API di mercato. - """ - self.social_agent = None - self.news_agent = None - self.predictor = None - self.chosen_model = None - self.available_models = AppModels.availables() - self.all_styles = list(PredictorStyle) - self.style = self.all_styles[0] # Default to the first style - - self.market = MarketAPIs(currency="USD") - self.choose_provider(0) # Default to the first model - - def choose_provider(self, index: int): - """ - Sceglie il modello LLM da utilizzare in base all'indice fornito. - - Args: - index: indice del modello nella lista available_models. - """ - # TODO Utilizzare AGNO per gestire i modelli... è molto più semplice e permette di cambiare modello facilmente - # TODO https://docs.agno.com/introduction - # Inoltre permette di creare dei team e workflow di agenti più facilmente - self.chosen_model = self.available_models[index] - self.predictor = self.chosen_model.get_agent(PREDICTOR_INSTRUCTIONS, output=PredictorOutput) - self.news_agent = NewsAgent() - self.social_agent = SocialAgent() - - def choose_style(self, index: int): - """ - Sceglie lo stile di previsione da utilizzare in base all'indice fornito. - - Args: - index: indice dello stile nella lista all_styles. - """ - self.style = self.all_styles[index] - - def interact(self, query: str) -> str: - """ - Funzione principale che coordina gli agenti per rispondere alla richiesta dell'utente. - - Args: - query: richiesta dell'utente (es. "Qual è la previsione per Bitcoin?") - """ - - log_info(f"[model={self.chosen_model.name}] [style={self.style.name}] [query=\"{query.replace('"', "'")}\"]") - # TODO Step 0: ricerca e analisi della richiesta (es. estrazione di criptovalute specifiche) - # Prendere la query dell'utente e fare un'analisi preliminare con una agente o con un team di agenti (social e news) - - # Step 1: raccolta analisi - cryptos = ["BTC", "ETH", "XRP", "LTC", "BCH"] # TODO rendere dinamico in futuro - market_data = self.market.get_products(cryptos) - news_sentiment = self.news_agent.analyze(query) - social_sentiment = self.social_agent.analyze(query) - log_info(f"End of data collection") - - # Step 2: aggrega sentiment - sentiment = f"{news_sentiment}\n{social_sentiment}" - - # Step 3: previsione - inputs = PredictorInput(data=market_data, style=self.style, sentiment=sentiment) - result = self.predictor.run(inputs) - prediction: PredictorOutput = result.content - log_info(f"End of prediction") - - market_data = "\n".join([f"{product.symbol}: {product.price}" for product in market_data]) - output = f"[{prediction.strategy}]\nPortafoglio:\n" + "\n".join( - [f"{item.asset} ({item.percentage}%): {item.motivation}" for item in prediction.portfolio] - ) - - return f"INPUT:\n{market_data}\n{sentiment}\n\n\nOUTPUT:\n{output}" - - def list_providers(self) -> list[str]: - """ - Restituisce la lista dei nomi dei modelli disponibili. - """ - return [model.name for model in self.available_models] - - def list_styles(self) -> list[str]: - """ - Restituisce la lista degli stili di previsione disponibili. - """ - return [style.value for style in self.all_styles] diff --git a/src/app/toolkits/__init__.py b/src/app/toolkits/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/app/agents/market.py b/src/app/toolkits/market_toolkit.py similarity index 99% rename from src/app/agents/market.py rename to src/app/toolkits/market_toolkit.py index a561cb6..f0aef93 100644 --- a/src/app/agents/market.py +++ b/src/app/toolkits/market_toolkit.py @@ -1,6 +1,8 @@ from agno.tools import Toolkit + from src.app.markets import MarketAPIs + # TODO (?) in futuro fare in modo che la LLM faccia da sé per il mercato # Non so se può essere utile, per ora lo lascio qui # per ora mettiamo tutto statico e poi, se abbiamo API-Key senza limiti diff --git a/tests/agents/test_market.py b/tests/agents/test_market.py index cb6c390..ef0b8f7 100644 --- a/tests/agents/test_market.py +++ b/tests/agents/test_market.py @@ -1,6 +1,6 @@ import os import pytest -from src.app.agents.market import MarketToolkit +from src.app.agents.market_toolkit import MarketToolkit from src.app.markets.base import BaseWrapper from src.app.markets.coinbase import CoinBaseWrapper from src.app.markets.cryptocompare import CryptoCompareWrapper From 15182e23c251ed5cf83d983ca2d007e659aa66e0 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Sep 2025 12:41:45 +0200 Subject: [PATCH 13/22] Refactor try_call_all method to return a dictionary of results; update tests for success and partial failures --- src/app/utils/wrapper_handler.py | 6 +++--- tests/utils/test_wrapper_handler.py | 23 +++++++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/app/utils/wrapper_handler.py b/src/app/utils/wrapper_handler.py index 0ea72d6..df86c36 100644 --- a/src/app/utils/wrapper_handler.py +++ b/src/app/utils/wrapper_handler.py @@ -61,7 +61,7 @@ class WrapperHandler(Generic[W]): raise Exception(f"All wrappers failed after retries") - def try_call_all(self, func: Callable[[W], T]) -> list[T]: + def try_call_all(self, func: Callable[[W], T]) -> dict[str, T]: """ Calls the provided function on all wrappers, collecting results. If a wrapper fails, it logs a warning and continues with the next. @@ -73,11 +73,11 @@ class WrapperHandler(Generic[W]): Raises: Exception: If all wrappers fail. """ - results = [] + results = {} for wrapper in self.wrappers: try: result = func(wrapper) - results.append(result) + results[wrapper.__class__] = result except Exception as e: log_warning(f"{wrapper} failed: {e}") if not results: diff --git a/tests/utils/test_wrapper_handler.py b/tests/utils/test_wrapper_handler.py index fd5ffff..4770977 100644 --- a/tests/utils/test_wrapper_handler.py +++ b/tests/utils/test_wrapper_handler.py @@ -5,6 +5,10 @@ class MockWrapper: def do_something(self) -> str: return "Success" +class MockWrapper2(MockWrapper): + def do_something(self) -> str: + return "Success 2" + class FailingWrapper(MockWrapper): def do_something(self): raise Exception("Intentional Failure") @@ -59,19 +63,26 @@ class TestWrapperHandler: assert handler.index == 1 # Should return to the second wrapper after failure assert handler.retry_count == 0 - def test_try_call_all(self): + def test_try_call_all_success(self): + wrappers = [MockWrapper, MockWrapper2] + handler: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers(wrappers, try_per_wrapper=1, retry_delay=0) + results = handler.try_call_all(lambda w: w.do_something()) + assert results == {MockWrapper: "Success", MockWrapper2: "Success 2"} + + def test_try_call_all_partial_failures(self): + # Only the second wrapper should succeed wrappers = [FailingWrapper, MockWrapper, FailingWrapper] handler: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers(wrappers, try_per_wrapper=1, retry_delay=0) - results = handler.try_call_all(lambda w: w.do_something()) - assert results == ["Success"] # Only the second wrapper should succeed + assert results == {MockWrapper: "Success"} - wrappers = [FailingWrapper, MockWrapper, FailingWrapper, MockWrapper] + # Only the second and fourth wrappers should succeed + wrappers = [FailingWrapper, MockWrapper, FailingWrapper, MockWrapper2] handler: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers(wrappers, try_per_wrapper=1, retry_delay=0) - results = handler.try_call_all(lambda w: w.do_something()) - assert results == ["Success", "Success"] # Only the second and fourth wrappers should succeed + assert results == {MockWrapper: "Success", MockWrapper2: "Success 2"} + def test_try_call_all_all_fail(self): # Test when all wrappers fail handler_all_fail: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers([FailingWrapper, FailingWrapper], try_per_wrapper=1, retry_delay=0) with pytest.raises(Exception) as exc_info: From c1952526add6434751961d830281c602b3835ad1 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Sep 2025 12:54:19 +0200 Subject: [PATCH 14/22] Fix class and test method names for DuckDuckGoWrapper --- src/app/news/__init__.py | 2 +- tests/api/test_duckduckgo_news.py | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/app/news/__init__.py b/src/app/news/__init__.py index 3218296..d38cd43 100644 --- a/src/app/news/__init__.py +++ b/src/app/news/__init__.py @@ -13,7 +13,7 @@ class NewsAPIs(NewsWrapper): A wrapper class that aggregates multiple news API wrappers and tries them in order until one succeeds. This class uses the WrapperHandler to manage multiple NewsWrapper instances. It includes, and tries, the following news API wrappers in this order: - - GnewsWrapper + - GoogleNewsWrapper - DuckDuckGoWrapper - NewsApiWrapper - CryptoPanicWrapper diff --git a/tests/api/test_duckduckgo_news.py b/tests/api/test_duckduckgo_news.py index ea38272..e0bb599 100644 --- a/tests/api/test_duckduckgo_news.py +++ b/tests/api/test_duckduckgo_news.py @@ -6,11 +6,11 @@ from app.news import DuckDuckGoWrapper @pytest.mark.api class TestDuckDuckGoNews: - def test_news_api_initialization(self): + def test_duckduckgo_initialization(self): news = DuckDuckGoWrapper() assert news.tool is not None - def test_news_api_get_latest_news(self): + def test_duckduckgo_get_latest_news(self): news = DuckDuckGoWrapper() articles = news.get_latest_news(query="crypto", total=2) assert isinstance(articles, list) @@ -21,8 +21,7 @@ class TestDuckDuckGoNews: assert article.title is not None or article.title != "" assert article.description is not None or article.description != "" - - def test_news_api_get_top_headlines(self): + def test_duckduckgo_get_top_headlines(self): news = DuckDuckGoWrapper() articles = news.get_top_headlines(total=2) assert isinstance(articles, list) From 43b2bddba5fc89fcc53f49d598122b975cdee7aa Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Sep 2025 15:36:37 +0200 Subject: [PATCH 15/22] Add Reddit API wrapper and related tests; update environment configuration --- .env.example | 16 +++++++++--- pyproject.toml | 20 +++++++-------- src/app/social/base.py | 22 +++++++++++++++++ src/app/social/reddit.py | 53 ++++++++++++++++++++++++++++++++++++++++ tests/api/test_reddit.py | 24 ++++++++++++++++++ tests/conftest.py | 1 + uv.lock | 49 +++++++++++++++++++++++++++++++++++++ 7 files changed, 171 insertions(+), 14 deletions(-) create mode 100644 src/app/social/reddit.py create mode 100644 tests/api/test_reddit.py diff --git a/.env.example b/.env.example index f59ad82..0948465 100644 --- a/.env.example +++ b/.env.example @@ -10,11 +10,11 @@ GOOGLE_API_KEY= # Configurazioni per gli agenti di mercato ############################################################################### -# Ottenibili da: https://portal.cdp.coinbase.com/access/api +# https://portal.cdp.coinbase.com/access/api CDP_API_KEY_NAME= CDP_API_PRIVATE_KEY= -# Ottenibile da: https://www.cryptocompare.com/cryptopian/api-keys +# https://www.cryptocompare.com/cryptopian/api-keys CRYPTOCOMPARE_API_KEY= # Binance API per Market Agent (alternativa) @@ -25,8 +25,16 @@ BINANCE_API_SECRET= # Configurazioni per gli agenti di notizie ############################################################################### -# Ottenibile da: https://newsapi.org/docs +# https://newsapi.org/docs NEWS_API_KEY= -# Ottenibile da: https://cryptopanic.com/developers/api/ +# https://cryptopanic.com/developers/api/ CRYPTOPANIC_API_KEY= + +############################################################################### +# Configurazioni per API di social media +############################################################################### + +# https://www.reddit.com/prefs/apps +REDDIT_API_CLIENT_ID= +REDDIT_API_CLIENT_SECRET= diff --git a/pyproject.toml b/pyproject.toml index 8f4cc87..74c0029 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,30 +10,30 @@ requires-python = "==3.12.*" # Per ogni roba ho fatto un commento per evitare di dimenticarmi cosa fa chi. # Inoltre ho messo una emoji per indicare se è raccomandato o meno. dependencies = [ - # ✅ per i test - "pytest", - # ✅ per gestire variabili d'ambiente (generalmente API keys od opzioni) - "dotenv", - # ✅ per fare una UI web semplice con input e output - "gradio", + "pytest", # Test + "dotenv", # Gestire variabili d'ambiente (generalmente API keys od opzioni) + "gradio", # UI web semplice con user_input e output - # ✅ per costruire agenti (ovvero modelli che possono fare più cose tramite tool) https://github.com/agno-agi/agno + # Per costruire agenti (ovvero modelli che possono fare più cose tramite tool) https://github.com/agno-agi/agno # altamente consigliata dato che ha anche tools integrati per fare scraping, calcoli e molto altro # oltre a questa è necessario installare anche le librerie specifiche per i modelli che si vogliono usare "agno", - # ✅ Modelli supportati e installati (aggiungere qui sotto quelli che si vogliono usare) + # Modelli supportati e installati (aggiungere qui sotto quelli che si vogliono usare) "google-genai", "ollama", - # ✅ per interagire con API di exchange di criptovalute + # API di exchange di criptovalute "coinbase-advanced-py", "python-binance", - # ✅ per interagire con API di notizie + # API di notizie "newsapi-python", "gnews", "ddgs", + + # API di social media + "praw", # Reddit ] [tool.pytest.ini_options] diff --git a/src/app/social/base.py b/src/app/social/base.py index e69de29..945cdd5 100644 --- a/src/app/social/base.py +++ b/src/app/social/base.py @@ -0,0 +1,22 @@ +from pydantic import BaseModel + + +class SocialPost(BaseModel): + time: str = "" + title: str = "" + description: str = "" + comments: list["SocialComment"] = [] + + def __str__(self): + return f"Title: {self.title}\nDescription: {self.description}\nComments: {len(self.comments)}\n[{" | ".join(str(c) for c in self.comments)}]" + +class SocialComment(BaseModel): + time: str = "" + description: str = "" + + def __str__(self): + return f"Time: {self.time}\nDescription: {self.description}" + +# TODO IMPLEMENTARLO SE SI USANO PIU' WRAPPER (E QUINDI PIU' SOCIAL) +class SocialWrapper: + pass diff --git a/src/app/social/reddit.py b/src/app/social/reddit.py new file mode 100644 index 0000000..7a3c824 --- /dev/null +++ b/src/app/social/reddit.py @@ -0,0 +1,53 @@ +import os +from praw import Reddit +from praw.models import Submission, MoreComments +from .base import SocialWrapper, SocialPost, SocialComment + +MAX_COMMENTS = 5 + + +def create_social_post(post: Submission) -> SocialPost: + social = SocialPost() + social.time = str(post.created) + social.title = post.title + social.description = post.selftext + + for i, top_comment in enumerate(post.comments): + if i >= MAX_COMMENTS: + break + if isinstance(top_comment, MoreComments): #skip MoreComments objects + continue + + comment = SocialComment() + comment.time = str(top_comment.created) + comment.description = top_comment.body + social.comments.append(comment) + return social + +class RedditWrapper(SocialWrapper): + """ + A wrapper for the Reddit API using PRAW (Python Reddit API Wrapper). + Requires the following environment variables to be set: + - REDDIT_API_CLIENT_ID + - REDDIT_API_CLIENT_SECRET + You can get them by creating an app at https://www.reddit.com/prefs/apps + """ + + def __init__(self): + self.client_id = os.getenv("REDDIT_API_CLIENT_ID") + assert self.client_id is not None, "REDDIT_API_CLIENT_ID environment variable is not set" + + self.client_secret = os.getenv("REDDIT_API_CLIENT_SECRET") + assert self.client_secret is not None, "REDDIT_API_CLIENT_SECRET environment variable is not set" + + self.tool = Reddit( + client_id=self.client_id, + client_secret=self.client_secret, + user_agent="upo-appAI", + ) + + def get_top_crypto_posts(self, limit=5) -> list[SocialPost]: + subreddit = self.tool.subreddit("CryptoCurrency") + top_posts = subreddit.top(limit=limit, time_filter="week") + return [create_social_post(post) for post in top_posts] + diff --git a/tests/api/test_reddit.py b/tests/api/test_reddit.py new file mode 100644 index 0000000..84c66da --- /dev/null +++ b/tests/api/test_reddit.py @@ -0,0 +1,24 @@ +import pytest +from praw import Reddit +from app.social.reddit import MAX_COMMENTS, RedditWrapper + +@pytest.mark.social +@pytest.mark.api +class TestRedditWrapper: + def test_initialization(self): + wrapper = RedditWrapper() + assert wrapper.client_id is not None + assert wrapper.client_secret is not None + assert isinstance(wrapper.tool, Reddit) + + def test_get_top_crypto_posts(self): + wrapper = RedditWrapper() + posts = wrapper.get_top_crypto_posts(limit=2) + assert isinstance(posts, list) + assert len(posts) == 2 + for post in posts: + assert post.title != "" + assert isinstance(post.comments, list) + assert len(post.comments) <= MAX_COMMENTS + for comment in post.comments: + assert comment.description != "" diff --git a/tests/conftest.py b/tests/conftest.py index 9bd9589..40d5aab 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -21,6 +21,7 @@ def pytest_configure(config:pytest.Config): ("ollama_gpt", "marks tests that use Ollama GPT model"), ("ollama_qwen", "marks tests that use Ollama Qwen model"), ("news", "marks tests that use news"), + ("social", "marks tests that use social media"), ("limited", "marks tests that have limited execution due to API constraints"), ("wrapper", "marks tests for wrapper handler"), ] diff --git a/uv.lock b/uv.lock index ce4499b..2d7d6a1 100644 --- a/uv.lock +++ b/uv.lock @@ -961,6 +961,32 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/54/20/4d324d65cc6d9205fabedc306948156824eb9f0ee1633355a8f7ec5c66bf/pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746", size = 20538, upload-time = "2025-05-15T12:30:06.134Z" }, ] +[[package]] +name = "praw" +version = "7.8.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "prawcore" }, + { name = "update-checker" }, + { name = "websocket-client" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/4c/52/7dd0b3d9ccb78e90236420ef6c51b6d9b2400a7229442f0cfcf2258cce21/praw-7.8.1.tar.gz", hash = "sha256:3c5767909f71e48853eb6335fef7b50a43cbe3da728cdfb16d3be92904b0a4d8", size = 154106, upload-time = "2024-10-25T21:49:33.16Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/73/ca/60ec131c3b43bff58261167045778b2509b83922ce8f935ac89d871bd3ea/praw-7.8.1-py3-none-any.whl", hash = "sha256:15917a81a06e20ff0aaaf1358481f4588449fa2421233040cb25e5c8202a3e2f", size = 189338, upload-time = "2024-10-25T21:49:31.109Z" }, +] + +[[package]] +name = "prawcore" +version = "2.4.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "requests" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/8a/62/d4c99cf472205f1e5da846b058435a6a7c988abf8eb6f7d632a7f32f4a77/prawcore-2.4.0.tar.gz", hash = "sha256:b7b2b5a1d04406e086ab4e79988dc794df16059862f329f4c6a43ed09986c335", size = 15862, upload-time = "2023-10-01T23:30:49.408Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/96/5c/8af904314e42d5401afcfaff69940dc448e974f80f7aa39b241a4fbf0cf1/prawcore-2.4.0-py3-none-any.whl", hash = "sha256:29af5da58d85704b439ad3c820873ad541f4535e00bb98c66f0fbcc8c603065a", size = 17203, upload-time = "2023-10-01T23:30:47.651Z" }, +] + [[package]] name = "primp" version = "0.15.0" @@ -1490,6 +1516,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/c2/14/e2a54fabd4f08cd7af1c07030603c3356b74da07f7cc056e600436edfa17/tzlocal-5.3.1-py3-none-any.whl", hash = "sha256:eb1a66c3ef5847adf7a834f1be0800581b683b5608e74f86ecbcef8ab91bb85d", size = 18026, upload-time = "2025-03-05T21:17:39.857Z" }, ] +[[package]] +name = "update-checker" +version = "0.18.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "requests" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/5c/0b/1bec4a6cc60d33ce93d11a7bcf1aeffc7ad0aa114986073411be31395c6f/update_checker-0.18.0.tar.gz", hash = "sha256:6a2d45bb4ac585884a6b03f9eade9161cedd9e8111545141e9aa9058932acb13", size = 6699, upload-time = "2020-08-04T07:08:50.429Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/0c/ba/8dd7fa5f0b1c6a8ac62f8f57f7e794160c1f86f31c6d0fb00f582372a3e4/update_checker-0.18.0-py3-none-any.whl", hash = "sha256:cbba64760a36fe2640d80d85306e8fe82b6816659190993b7bdabadee4d4bbfd", size = 7008, upload-time = "2020-08-04T07:08:49.51Z" }, +] + [[package]] name = "upo-app-ai" version = "0.1.0" @@ -1504,6 +1542,7 @@ dependencies = [ { name = "gradio" }, { name = "newsapi-python" }, { name = "ollama" }, + { name = "praw" }, { name = "pytest" }, { name = "python-binance" }, ] @@ -1519,6 +1558,7 @@ requires-dist = [ { name = "gradio" }, { name = "newsapi-python" }, { name = "ollama" }, + { name = "praw" }, { name = "pytest" }, { name = "python-binance" }, ] @@ -1545,6 +1585,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/d2/e2/dc81b1bd1dcfe91735810265e9d26bc8ec5da45b4c0f6237e286819194c3/uvicorn-0.35.0-py3-none-any.whl", hash = "sha256:197535216b25ff9b785e29a0b79199f55222193d47f820816e7da751e9bc8d4a", size = 66406, upload-time = "2025-06-28T16:15:44.816Z" }, ] +[[package]] +name = "websocket-client" +version = "1.8.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/e6/30/fba0d96b4b5fbf5948ed3f4681f7da2f9f64512e1d303f94b4cc174c24a5/websocket_client-1.8.0.tar.gz", hash = "sha256:3239df9f44da632f96012472805d40a23281a991027ce11d2f45a6f24ac4c3da", size = 54648, upload-time = "2024-04-23T22:16:16.976Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/5a/84/44687a29792a70e111c5c477230a72c4b957d88d16141199bf9acb7537a3/websocket_client-1.8.0-py3-none-any.whl", hash = "sha256:17b44cc997f5c498e809b22cdf2d9c7a9e71c02c8cc2b6c56e7c2d1239bfa526", size = 58826, upload-time = "2024-04-23T22:16:14.422Z" }, +] + [[package]] name = "websockets" version = "13.1" From 8304cf9ea898aff6de2fcadf5b38c2a5f8dcff77 Mon Sep 17 00:00:00 2001 From: Simone Garau <20005068@studenti.uniupo.it> Date: Tue, 30 Sep 2025 15:36:50 +0200 Subject: [PATCH 16/22] pre merge con giacomo --- demos/market_providers_api_demo.py | 2 +- src/app/agents/market_agent.py | 22 +++--- src/app/markets/error_handler.py | 3 +- src/app/toolkits/market_toolkit.py | 4 +- src/app/utils/wrapper_handler.py | 110 +++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+), 16 deletions(-) create mode 100644 src/app/utils/wrapper_handler.py diff --git a/demos/market_providers_api_demo.py b/demos/market_providers_api_demo.py index 39055bb..16817d5 100644 --- a/demos/market_providers_api_demo.py +++ b/demos/market_providers_api_demo.py @@ -26,7 +26,7 @@ project_root = Path(__file__).parent.parent sys.path.insert(0, str(project_root / "src")) from dotenv import load_dotenv -from app.markets import ( +from src.app.markets import ( CoinBaseWrapper, CryptoCompareWrapper, BinanceWrapper, diff --git a/src/app/agents/market_agent.py b/src/app/agents/market_agent.py index 94d01d5..b2c4cfd 100644 --- a/src/app/agents/market_agent.py +++ b/src/app/agents/market_agent.py @@ -71,18 +71,16 @@ class MarketAgent(Agent): results = [] products: List[ProductInfo] = [] - for sym in symbols: - try: - product = self.toolkit.get_current_price(sym) # supponiamo ritorni un ProductInfo o simile - if isinstance(product, list): - products.extend(product) - else: - products.append(product) - - results.append(f"{sym}: {product.price if hasattr(product, 'price') else product}") - except Exception as e: - results.append(f"{sym}: errore ({e})") - + try: + products.extend(self.toolkit.get_current_prices(symbols)) # supponiamo ritorni un ProductInfo o simile + # Usa list comprehension per iterare symbols e products insieme + results.extend([ + f"{symbol}: ${product.price:.2f}" if hasattr(product, 'price') and product.price else f"{symbol}: N/A" + for symbol, product in zip(symbols, products) + ]) + except Exception as e: + results.extend(f"Errore: Impossibile recuperare i dati di mercato\n{str(e)}") + # 4. Preparo output leggibile + metadati strutturati output_text = "📊 Dati di mercato:\n" + "\n".join(results) diff --git a/src/app/markets/error_handler.py b/src/app/markets/error_handler.py index 38aa47f..22b6aeb 100644 --- a/src/app/markets/error_handler.py +++ b/src/app/markets/error_handler.py @@ -14,6 +14,7 @@ from functools import wraps from typing import Any, Callable, Optional, Type, Union, List from requests.exceptions import RequestException, Timeout, ConnectionError from binance.exceptions import BinanceAPIException, BinanceRequestException +from base import ProductInfo # Configurazione logging logger = logging.getLogger(__name__) @@ -168,7 +169,7 @@ class ProviderFallback: method_name: str, *args, **kwargs - ) -> Any: + ) -> list[ProductInfo]: """ Esegue un metodo su tutti i provider fino a trovarne uno che funziona. diff --git a/src/app/toolkits/market_toolkit.py b/src/app/toolkits/market_toolkit.py index f0aef93..f366986 100644 --- a/src/app/toolkits/market_toolkit.py +++ b/src/app/toolkits/market_toolkit.py @@ -16,14 +16,14 @@ class MarketToolkit(Toolkit): name="Market Toolkit", tools=[ self.get_historical_data, - self.get_current_price, + self.get_current_prices, ], ) def get_historical_data(self, symbol: str): return self.market_api.get_historical_prices(symbol) - def get_current_price(self, symbol: str): + def get_current_prices(self, symbol: list): return self.market_api.get_products(symbol) def prepare_inputs(): diff --git a/src/app/utils/wrapper_handler.py b/src/app/utils/wrapper_handler.py new file mode 100644 index 0000000..df86c36 --- /dev/null +++ b/src/app/utils/wrapper_handler.py @@ -0,0 +1,110 @@ +import time +from typing import TypeVar, Callable, Generic, Iterable, Type +from agno.utils.log import log_warning + +W = TypeVar("W") +T = TypeVar("T") + +class WrapperHandler(Generic[W]): + """ + A handler for managing multiple wrappers with retry logic. + It attempts to call a function on the current wrapper, and if it fails, + it retries a specified number of times before switching to the next wrapper. + If all wrappers fail, it raises an exception. + + Note: use `build_wrappers` to create an instance of this class for better error handling. + """ + + def __init__(self, wrappers: list[W], try_per_wrapper: int = 3, retry_delay: int = 2): + """ + Initializes the WrapperHandler with a list of wrappers and retry settings.\n + Use `build_wrappers` to create an instance of this class for better error handling. + Args: + wrappers (list[W]): A list of wrapper instances to manage. + try_per_wrapper (int): Number of retries per wrapper before switching to the next. + retry_delay (int): Delay in seconds between retries. + """ + self.wrappers = wrappers + self.retry_per_wrapper = try_per_wrapper + self.retry_delay = retry_delay + self.index = 0 + self.retry_count = 0 + + def try_call(self, func: Callable[[W], T]) -> T: + """ + Attempts to call the provided function on the current wrapper. + If it fails, it retries a specified number of times before switching to the next wrapper. + If all wrappers fail, it raises an exception. + Args: + func (Callable[[W], T]): A function that takes a wrapper and returns a result. + Returns: + T: The result of the function call. + Raises: + Exception: If all wrappers fail after retries. + """ + iterations = 0 + while iterations < len(self.wrappers): + try: + wrapper = self.wrappers[self.index] + result = func(wrapper) + self.retry_count = 0 + return result + except Exception as e: + self.retry_count += 1 + if self.retry_count >= self.retry_per_wrapper: + self.index = (self.index + 1) % len(self.wrappers) + self.retry_count = 0 + iterations += 1 + else: + log_warning(f"{wrapper} failed {self.retry_count}/{self.retry_per_wrapper}: {e}") + time.sleep(self.retry_delay) + + raise Exception(f"All wrappers failed after retries") + + def try_call_all(self, func: Callable[[W], T]) -> dict[str, T]: + """ + Calls the provided function on all wrappers, collecting results. + If a wrapper fails, it logs a warning and continues with the next. + If all wrappers fail, it raises an exception. + Args: + func (Callable[[W], T]): A function that takes a wrapper and returns a result. + Returns: + list[T]: A list of results from the function calls. + Raises: + Exception: If all wrappers fail. + """ + results = {} + for wrapper in self.wrappers: + try: + result = func(wrapper) + results[wrapper.__class__] = result + except Exception as e: + log_warning(f"{wrapper} failed: {e}") + if not results: + raise Exception("All wrappers failed") + return results + + @staticmethod + def build_wrappers(constructors: Iterable[Type[W]], try_per_wrapper: int = 3, retry_delay: int = 2) -> 'WrapperHandler[W]': + """ + Builds a WrapperHandler instance with the given wrapper constructors. + It attempts to initialize each wrapper and logs a warning if any cannot be initialized. + Only successfully initialized wrappers are included in the handler. + Args: + constructors (Iterable[Type[W]]): An iterable of wrapper classes to instantiate. e.g. [WrapperA, WrapperB] + try_per_wrapper (int): Number of retries per wrapper before switching to the next. + retry_delay (int): Delay in seconds between retries. + Returns: + WrapperHandler[W]: An instance of WrapperHandler with the initialized wrappers. + Raises: + Exception: If no wrappers could be initialized. + """ + result = [] + for wrapper_class in constructors: + try: + wrapper = wrapper_class() + result.append(wrapper) + except Exception as e: + log_warning(f"{wrapper_class} cannot be initialized: {e}") + + return WrapperHandler(result, try_per_wrapper, retry_delay) \ No newline at end of file From 3074b58ea5061de3558233d92d03e76af07355ab Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Sep 2025 15:58:52 +0200 Subject: [PATCH 17/22] Fix import statements --- src/app/agents/market_agent.py | 4 ++-- src/app/markets/__init__.py | 2 +- src/app/markets/error_handler.py | 2 +- src/app/models.py | 3 +-- src/app/pipeline.py | 10 +++++----- src/app/predictor.py | 4 +--- src/app/toolkits/market_toolkit.py | 3 +-- 7 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/app/agents/market_agent.py b/src/app/agents/market_agent.py index b2c4cfd..12f9eab 100644 --- a/src/app/agents/market_agent.py +++ b/src/app/agents/market_agent.py @@ -4,8 +4,8 @@ from agno.models.message import Message from agno.run.agent import RunOutput, RunOutputEvent from pydantic import BaseModel -from src.app.toolkits.market_toolkit import MarketToolkit -from src.app.markets.base import ProductInfo # modello dati già definito nel tuo progetto +from app.toolkits.market_toolkit import MarketToolkit +from app.markets.base import ProductInfo # modello dati già definito nel tuo progetto class MarketAgent(Agent): diff --git a/src/app/markets/__init__.py b/src/app/markets/__init__.py index 20e29ee..0469327 100644 --- a/src/app/markets/__init__.py +++ b/src/app/markets/__init__.py @@ -1,4 +1,4 @@ -from base import BaseWrapper +from .base import BaseWrapper from app.markets.coinbase import CoinBaseWrapper from app.markets.cryptocompare import CryptoCompareWrapper from app.markets.binance import BinanceWrapper diff --git a/src/app/markets/error_handler.py b/src/app/markets/error_handler.py index 22b6aeb..c98301a 100644 --- a/src/app/markets/error_handler.py +++ b/src/app/markets/error_handler.py @@ -14,7 +14,7 @@ from functools import wraps from typing import Any, Callable, Optional, Type, Union, List from requests.exceptions import RequestException, Timeout, ConnectionError from binance.exceptions import BinanceAPIException, BinanceRequestException -from base import ProductInfo +from .base import ProductInfo # Configurazione logging logger = logging.getLogger(__name__) diff --git a/src/app/models.py b/src/app/models.py index f7ec9b8..c1bff9b 100644 --- a/src/app/models.py +++ b/src/app/models.py @@ -1,7 +1,6 @@ import os -from enum import Enum - import requests +from enum import Enum from agno.agent import Agent from agno.models.base import Model from agno.models.google import Gemini diff --git a/src/app/pipeline.py b/src/app/pipeline.py index 9e0e2bd..f515053 100644 --- a/src/app/pipeline.py +++ b/src/app/pipeline.py @@ -4,11 +4,11 @@ from agno.team import Team from agno.utils.log import log_info from app.agents.market_agent import MarketAgent -from src.app.agents.news_agent import NewsAgent -from src.app.agents.social_agent import SocialAgent -from src.app.markets import MarketAPIs -from src.app.models import AppModels -from src.app.predictor import PredictorStyle, PredictorInput, PredictorOutput, PREDICTOR_INSTRUCTIONS +from app.agents.news_agent import NewsAgent +from app.agents.social_agent import SocialAgent +from app.markets import MarketAPIs +from app.models import AppModels +from app.predictor import PredictorStyle, PredictorInput, PredictorOutput, PREDICTOR_INSTRUCTIONS class Pipeline: diff --git a/src/app/predictor.py b/src/app/predictor.py index 101b4ac..38780de 100644 --- a/src/app/predictor.py +++ b/src/app/predictor.py @@ -1,8 +1,6 @@ from enum import Enum - from pydantic import BaseModel, Field - -from src.app.markets.base import ProductInfo +from app.markets.base import ProductInfo class PredictorStyle(Enum): diff --git a/src/app/toolkits/market_toolkit.py b/src/app/toolkits/market_toolkit.py index f366986..ff6e48d 100644 --- a/src/app/toolkits/market_toolkit.py +++ b/src/app/toolkits/market_toolkit.py @@ -1,6 +1,5 @@ from agno.tools import Toolkit - -from src.app.markets import MarketAPIs +from app.markets import MarketAPIs # TODO (?) in futuro fare in modo che la LLM faccia da sé per il mercato From a5ef982e124a568bd63a8e8bef2e1797929021d6 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Sep 2025 16:38:25 +0200 Subject: [PATCH 18/22] Fixes - separated tests - fix tests - fix bugs reintroduced my previous merge --- src/app/markets/base.py | 2 +- src/app/markets/coinbase.py | 38 +++++++++++--- src/app/markets/cryptocompare.py | 10 ++-- tests/agents/test_market.py | 85 +++++--------------------------- tests/api/test_binance.py | 7 +++ tests/api/test_coinbase.py | 54 ++++++++++++++++++++ tests/api/test_cryptocompare.py | 56 +++++++++++++++++++++ tests/conftest.py | 22 +-------- 8 files changed, 165 insertions(+), 109 deletions(-) create mode 100644 tests/api/test_binance.py create mode 100644 tests/api/test_coinbase.py create mode 100644 tests/api/test_cryptocompare.py diff --git a/src/app/markets/base.py b/src/app/markets/base.py index 032f8aa..b15b125 100644 --- a/src/app/markets/base.py +++ b/src/app/markets/base.py @@ -12,7 +12,7 @@ class BaseWrapper: raise NotImplementedError def get_all_products(self) -> list['ProductInfo']: raise NotImplementedError - def get_historical_prices(self, asset_id: str = "BTC") -> list['Price']: + def get_historical_prices(self, asset_id: str = "BTC", limit: int = 100) -> list['Price']: raise NotImplementedError class ProductInfo(BaseModel): diff --git a/src/app/markets/coinbase.py b/src/app/markets/coinbase.py index aac556d..38614aa 100644 --- a/src/app/markets/coinbase.py +++ b/src/app/markets/coinbase.py @@ -1,19 +1,32 @@ import os +from enum import Enum +from datetime import datetime, timedelta from coinbase.rest import RESTClient from app.markets.base import ProductInfo, BaseWrapper, Price + +class Granularity(Enum): + UNKNOWN_GRANULARITY = 0 + ONE_MINUTE = 60 + FIVE_MINUTE = 300 + FIFTEEN_MINUTE = 900 + THIRTY_MINUTE = 1800 + ONE_HOUR = 3600 + TWO_HOUR = 7200 + FOUR_HOUR = 14400 + SIX_HOUR = 21600 + ONE_DAY = 86400 + class CoinBaseWrapper(BaseWrapper): """ Wrapper per le API di Coinbase. La documentazione delle API è disponibile qui: https://docs.cdp.coinbase.com/api-reference/advanced-trade-api/rest-api/introduction """ - def __init__(self, api_key:str = None, api_private_key:str = None, currency: str = "USD"): - if api_key is None: - api_key = os.getenv("COINBASE_API_KEY") + def __init__(self, currency: str = "USD"): + api_key = os.getenv("COINBASE_API_KEY") assert api_key is not None, "API key is required" - if api_private_key is None: - api_private_key = os.getenv("COINBASE_API_SECRET") + api_private_key = os.getenv("COINBASE_API_SECRET") assert api_private_key is not None, "API private key is required" self.currency = currency @@ -32,14 +45,23 @@ class CoinBaseWrapper(BaseWrapper): def get_products(self, asset_ids: list[str]) -> list[ProductInfo]: all_asset_ids = [self.__format(asset_id) for asset_id in asset_ids] - assets = self.client.get_products(all_asset_ids) + assets = self.client.get_products(product_ids=all_asset_ids) return [ProductInfo.from_coinbase(asset) for asset in assets.products] def get_all_products(self) -> list[ProductInfo]: assets = self.client.get_products() return [ProductInfo.from_coinbase(asset) for asset in assets.products] - def get_historical_prices(self, asset_id: str = "BTC") -> list[Price]: + def get_historical_prices(self, asset_id: str = "BTC", limit: int = 100) -> list[Price]: asset_id = self.__format(asset_id) - data = self.client.get_candles(product_id=asset_id) + end_time = datetime.now() + start_time = end_time - timedelta(days=14) + + data = self.client.get_candles( + product_id=asset_id, + granularity=Granularity.ONE_HOUR.name, + start=str(int(start_time.timestamp())), + end=str(int(end_time.timestamp())), + limit=limit + ) return [Price.from_coinbase(candle) for candle in data.candles] diff --git a/src/app/markets/cryptocompare.py b/src/app/markets/cryptocompare.py index 188a2c2..b4a092d 100644 --- a/src/app/markets/cryptocompare.py +++ b/src/app/markets/cryptocompare.py @@ -10,9 +10,8 @@ class CryptoCompareWrapper(BaseWrapper): La documentazione delle API è disponibile qui: https://developers.coindesk.com/documentation/legacy/Price/SingleSymbolPriceEndpoint !!ATTENZIONE!! sembra essere una API legacy e potrebbe essere deprecata in futuro. """ - def __init__(self, api_key:str = None, currency:str='USD'): - if api_key is None: - api_key = os.getenv("CRYPTOCOMPARE_API_KEY") + def __init__(self, currency:str='USD'): + api_key = os.getenv("CRYPTOCOMPARE_API_KEY") assert api_key is not None, "API key is required" self.api_key = api_key @@ -49,12 +48,11 @@ class CryptoCompareWrapper(BaseWrapper): def get_all_products(self) -> list[ProductInfo]: raise NotImplementedError("CryptoCompare does not support fetching all assets") - def get_historical_prices(self, asset_id: str, day_back: int = 10) -> list[dict]: - assert day_back <= 30, "day_back should be less than or equal to 30" + def get_historical_prices(self, asset_id: str, limit: int = 100) -> list[dict]: response = self.__request("/data/v2/histohour", params = { "fsym": asset_id, "tsym": self.currency, - "limit": day_back * 24 + "limit": limit-1 # because the API returns limit+1 items (limit + current) }) data = response.get('Data', {}).get('Data', []) diff --git a/tests/agents/test_market.py b/tests/agents/test_market.py index 56931b3..b3e7f46 100644 --- a/tests/agents/test_market.py +++ b/tests/agents/test_market.py @@ -1,34 +1,21 @@ import os import pytest from app.agents.market import MarketToolkit -from app.markets.base import BaseWrapper -from app.markets.coinbase import CoinBaseWrapper -from app.markets.cryptocompare import CryptoCompareWrapper from app.markets import MarketAPIs class TestMarketSystem: """Test suite per il sistema di mercato (wrappers + toolkit)""" - @pytest.fixture(scope="class") - def market_wrapper(self) -> BaseWrapper: - return MarketAPIs("USD") - - def test_wrapper_initialization(self, market_wrapper): + def test_wrapper_initialization(self): + market_wrapper = MarketAPIs("USD") assert market_wrapper is not None assert hasattr(market_wrapper, 'get_product') assert hasattr(market_wrapper, 'get_products') assert hasattr(market_wrapper, 'get_all_products') assert hasattr(market_wrapper, 'get_historical_prices') - def test_providers_configuration(self): - available_providers = [] - if os.getenv('CDP_API_KEY_NAME') and os.getenv('CDP_API_PRIVATE_KEY'): - available_providers.append('coinbase') - if os.getenv('CRYPTOCOMPARE_API_KEY'): - available_providers.append('cryptocompare') - assert len(available_providers) > 0 - - def test_wrapper_capabilities(self, market_wrapper): + def test_wrapper_capabilities(self): + market_wrapper = MarketAPIs("USD") capabilities = [] if hasattr(market_wrapper, 'get_product'): capabilities.append('single_product') @@ -38,14 +25,15 @@ class TestMarketSystem: capabilities.append('historical_data') assert len(capabilities) > 0 - def test_market_data_retrieval(self, market_wrapper): + def test_market_data_retrieval(self): + market_wrapper = MarketAPIs("USD") btc_product = market_wrapper.get_product("BTC") assert btc_product is not None assert hasattr(btc_product, 'symbol') assert hasattr(btc_product, 'price') assert btc_product.price > 0 - def test_market_toolkit_integration(self, market_wrapper): + def test_market_toolkit_integration(self): try: toolkit = MarketToolkit() assert toolkit is not None @@ -59,59 +47,6 @@ class TestMarketSystem: print(f"MarketToolkit test failed: {e}") # Non fail completamente - il toolkit potrebbe avere dipendenze specifiche - @pytest.mark.skipif( - not os.getenv('CRYPTOCOMPARE_API_KEY'), - reason="CRYPTOCOMPARE_API_KEY not configured" - ) - def test_cryptocompare_wrapper(self): - try: - api_key = os.getenv('CRYPTOCOMPARE_API_KEY') - wrapper = CryptoCompareWrapper(api_key=api_key, currency="USD") - - btc_product = wrapper.get_product("BTC") - assert btc_product is not None - assert btc_product.symbol == "BTC" - assert btc_product.price > 0 - - products = wrapper.get_products(["BTC", "ETH"]) - assert isinstance(products, list) - assert len(products) > 0 - - for product in products: - if product.symbol in ["BTC", "ETH"]: - assert product.price > 0 - - except Exception as e: - print(f"CryptoCompare test failed: {e}") - # Non fail il test se c'è un errore di rete o API - - @pytest.mark.skipif( - not (os.getenv('CDP_API_KEY_NAME') and os.getenv('CDP_API_PRIVATE_KEY')), - reason="Coinbase credentials not configured" - ) - def test_coinbase_wrapper(self): - try: - api_key = os.getenv('CDP_API_KEY_NAME') - api_secret = os.getenv('CDP_API_PRIVATE_KEY') - wrapper = CoinBaseWrapper( - api_key=api_key, - api_private_key=api_secret, - currency="USD" - ) - - btc_product = wrapper.get_product("BTC") - assert btc_product is not None - assert btc_product.symbol == "BTC" - assert btc_product.price > 0 - - products = wrapper.get_products(["BTC", "ETH"]) - assert isinstance(products, list) - assert len(products) > 0 - - except Exception as e: - print(f"Coinbase test failed: {e}") - # Non fail il test se c'è un errore di credenziali o rete - def test_provider_selection_mechanism(self): potential_providers = 0 if os.getenv('CDP_API_KEY_NAME') and os.getenv('CDP_API_PRIVATE_KEY'): @@ -127,8 +62,9 @@ class TestMarketSystem: assert wrapper is not None assert hasattr(wrapper, 'get_product') - def test_error_handling(self, market_wrapper): + def test_error_handling(self): try: + market_wrapper = MarketAPIs("USD") fake_product = market_wrapper.get_product("NONEXISTENT_CRYPTO_SYMBOL_12345") assert fake_product is None or fake_product.price == 0 except Exception as e: @@ -140,7 +76,8 @@ class TestMarketSystem: except Exception as e: pass - def test_wrapper_currency_support(self, market_wrapper): + def test_wrapper_currency_support(self): + market_wrapper = MarketAPIs("USD") assert hasattr(market_wrapper, 'currency') assert isinstance(market_wrapper.currency, str) assert len(market_wrapper.currency) >= 3 # USD, EUR, etc. diff --git a/tests/api/test_binance.py b/tests/api/test_binance.py new file mode 100644 index 0000000..0a73c15 --- /dev/null +++ b/tests/api/test_binance.py @@ -0,0 +1,7 @@ +import pytest + +@pytest.mark.market +@pytest.mark.api +class TestBinance: + # TODO fare dei test veri e propri + pass \ No newline at end of file diff --git a/tests/api/test_coinbase.py b/tests/api/test_coinbase.py new file mode 100644 index 0000000..b5f92e8 --- /dev/null +++ b/tests/api/test_coinbase.py @@ -0,0 +1,54 @@ +import os +import pytest +from app.markets import CoinBaseWrapper + +@pytest.mark.market +@pytest.mark.api +@pytest.mark.skipif(not(os.getenv('COINBASE_API_KEY')) or not(os.getenv('COINBASE_API_SECRET')), reason="COINBASE_API_KEY or COINBASE_API_SECRET not set in environment variables") +class TestCoinBase: + + def test_coinbase_init(self): + market = CoinBaseWrapper() + assert market is not None + assert hasattr(market, 'currency') + assert market.currency == "USD" + + def test_coinbase_get_product(self): + market = CoinBaseWrapper() + product = market.get_product("BTC") + assert product is not None + assert hasattr(product, 'symbol') + assert product.symbol == "BTC" + assert hasattr(product, 'price') + assert product.price > 0 + + def test_coinbase_get_products(self): + market = CoinBaseWrapper() + products = market.get_products(["BTC", "ETH"]) + assert products is not None + assert isinstance(products, list) + assert len(products) == 2 + symbols = [p.symbol for p in products] + assert "BTC" in symbols + assert "ETH" in symbols + for product in products: + assert hasattr(product, 'price') + assert product.price > 0 + + def test_coinbase_invalid_product(self): + market = CoinBaseWrapper() + with pytest.raises(Exception): + _ = market.get_product("INVALID") + + def test_coinbase_history(self): + market = CoinBaseWrapper() + history = market.get_historical_prices("BTC", limit=5) + assert history is not None + assert isinstance(history, list) + assert len(history) == 5 + for entry in history: + assert hasattr(entry, 'time') + assert hasattr(entry, 'close') + assert hasattr(entry, 'high') + assert entry.close > 0 + assert entry.high > 0 diff --git a/tests/api/test_cryptocompare.py b/tests/api/test_cryptocompare.py new file mode 100644 index 0000000..52aef9a --- /dev/null +++ b/tests/api/test_cryptocompare.py @@ -0,0 +1,56 @@ +import os +import pytest +from app.markets import CryptoCompareWrapper + +@pytest.mark.market +@pytest.mark.api +@pytest.mark.skipif(not os.getenv('CRYPTOCOMPARE_API_KEY'), reason="CRYPTOCOMPARE_API_KEY not set in environment variables") +class TestCryptoCompare: + + def test_cryptocompare_init(self): + market = CryptoCompareWrapper() + assert market is not None + assert hasattr(market, 'api_key') + assert market.api_key == os.getenv('CRYPTOCOMPARE_API_KEY') + assert hasattr(market, 'currency') + assert market.currency == "USD" + + def test_cryptocompare_get_product(self): + market = CryptoCompareWrapper() + product = market.get_product("BTC") + assert product is not None + assert hasattr(product, 'symbol') + assert product.symbol == "BTC" + assert hasattr(product, 'price') + assert product.price > 0 + + def test_cryptocompare_get_products(self): + market = CryptoCompareWrapper() + products = market.get_products(["BTC", "ETH"]) + assert products is not None + assert isinstance(products, list) + assert len(products) == 2 + symbols = [p.symbol for p in products] + assert "BTC" in symbols + assert "ETH" in symbols + for product in products: + assert hasattr(product, 'price') + assert product.price > 0 + + def test_cryptocompare_invalid_product(self): + market = CryptoCompareWrapper() + with pytest.raises(Exception): + _ = market.get_product("INVALID") + + def test_cryptocompare_history(self): + market = CryptoCompareWrapper() + history = market.get_historical_prices("BTC", limit=5) + assert history is not None + assert isinstance(history, list) + assert len(history) == 5 + for entry in history: + assert hasattr(entry, 'time') + assert hasattr(entry, 'close') + assert hasattr(entry, 'high') + assert entry.close > 0 + assert entry.high > 0 diff --git a/tests/conftest.py b/tests/conftest.py index 40d5aab..e65e86f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -15,8 +15,7 @@ def pytest_configure(config:pytest.Config): markers = [ ("slow", "marks tests as slow (deselect with '-m \"not slow\"')"), ("api", "marks tests that require API access"), - ("coinbase", "marks tests that require Coinbase credentials"), - ("cryptocompare", "marks tests that require CryptoCompare credentials"), + ("market", "marks tests that use market data"), ("gemini", "marks tests that use Gemini model"), ("ollama_gpt", "marks tests that use Ollama GPT model"), ("ollama_qwen", "marks tests that use Ollama Qwen model"), @@ -30,24 +29,7 @@ def pytest_configure(config:pytest.Config): config.addinivalue_line("markers", line) def pytest_collection_modifyitems(config, items): - """Modifica automaticamente gli item di test aggiungendogli marker basati sul nome""" - - markers_to_add = { - "coinbase": pytest.mark.api, - "cryptocompare": pytest.mark.api, - "overview": pytest.mark.slow, - "analysis": pytest.mark.slow, - "gemini": pytest.mark.gemini, - "ollama_gpt": pytest.mark.ollama_gpt, - "ollama_qwen": pytest.mark.ollama_qwen, - } - - for item in items: - name = item.name.lower() - for key, marker in markers_to_add.items(): - if key in name: - item.add_marker(marker) - + """Modifica automaticamente degli item di test rimovendoli""" # Rimuovo i test "limited" e "slow" se non richiesti esplicitamente mark_to_remove = ['limited', 'slow'] for mark in mark_to_remove: From f7dec6fdb61025f327c3fc2d5753c15db8e886b1 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Sep 2025 17:30:14 +0200 Subject: [PATCH 19/22] Refactor market API wrappers to streamline product and price retrieval methods --- src/app/markets/base.py | 76 +----------- src/app/markets/binance.py | 194 +++++++++---------------------- src/app/markets/coinbase.py | 31 ++++- src/app/markets/cryptocompare.py | 29 ++++- tests/api/test_binance.py | 49 +++++++- 5 files changed, 157 insertions(+), 222 deletions(-) diff --git a/src/app/markets/base.py b/src/app/markets/base.py index 7a52a03..2690c4f 100644 --- a/src/app/markets/base.py +++ b/src/app/markets/base.py @@ -1,4 +1,4 @@ -from coinbase.rest.types.product_types import Candle, GetProductResponse, Product + from pydantic import BaseModel class BaseWrapper: @@ -27,58 +27,6 @@ class ProductInfo(BaseModel): status: str = "" quote_currency: str = "" - @staticmethod - def from_coinbase(product_data: GetProductResponse) -> 'ProductInfo': - product = ProductInfo() - product.id = product_data.product_id or "" - product.symbol = product_data.base_currency_id or "" - product.price = float(product_data.price) if product_data.price else 0.0 - product.volume_24h = float(product_data.volume_24h) if product_data.volume_24h else 0.0 - # TODO Check what status means in Coinbase - product.status = product_data.status or "" - return product - - @staticmethod - def from_coinbase_product(product_data: Product) -> 'ProductInfo': - product = ProductInfo() - product.id = product_data.product_id or "" - product.symbol = product_data.base_currency_id or "" - product.price = float(product_data.price) if product_data.price else 0.0 - product.volume_24h = float(product_data.volume_24h) if product_data.volume_24h else 0.0 - product.status = product_data.status or "" - return product - - @staticmethod - def from_cryptocompare(asset_data: dict) -> 'ProductInfo': - product = ProductInfo() - product.id = asset_data['FROMSYMBOL'] + '-' + asset_data['TOSYMBOL'] - product.symbol = asset_data['FROMSYMBOL'] - product.price = float(asset_data['PRICE']) - product.volume_24h = float(asset_data['VOLUME24HOUR']) - product.status = "" # Cryptocompare does not provide status - return product - - @staticmethod - def from_binance(ticker_data: dict, ticker_24h_data: dict) -> 'ProductInfo': - """ - Crea un oggetto ProductInfo da dati Binance. - - Args: - ticker_data: Dati del ticker di prezzo - ticker_24h_data: Dati del ticker 24h - - Returns: - Oggetto ProductInfo - """ - product = ProductInfo() - product.id = ticker_data['symbol'] - product.symbol = ticker_data['symbol'].replace('USDT', '').replace('BUSD', '') - product.price = float(ticker_data['price']) - product.volume_24h = float(ticker_24h_data['volume']) - product.status = "TRADING" # Binance non fornisce status esplicito - product.quote_currency = "USDT" # Assumiamo USDT come default - return product - class Price(BaseModel): """ Rappresenta i dati di prezzo per un asset, come ottenuti dalle API di mercato. @@ -90,25 +38,3 @@ class Price(BaseModel): close: float = 0.0 volume: float = 0.0 time: str = "" - - @staticmethod - def from_coinbase(candle_data: Candle) -> 'Price': - price = Price() - price.high = float(candle_data.high) if candle_data.high else 0.0 - price.low = float(candle_data.low) if candle_data.low else 0.0 - price.open = float(candle_data.open) if candle_data.open else 0.0 - price.close = float(candle_data.close) if candle_data.close else 0.0 - price.volume = float(candle_data.volume) if candle_data.volume else 0.0 - price.time = str(candle_data.start) if candle_data.start else "" - return price - - @staticmethod - def from_cryptocompare(price_data: dict) -> 'Price': - price = Price() - price.high = float(price_data['high']) - price.low = float(price_data['low']) - price.open = float(price_data['open']) - price.close = float(price_data['close']) - price.volume = float(price_data['volumeto']) - price.time = str(price_data['time']) - return price diff --git a/src/app/markets/binance.py b/src/app/markets/binance.py index 8bc1101..6b6b6d3 100644 --- a/src/app/markets/binance.py +++ b/src/app/markets/binance.py @@ -1,37 +1,31 @@ import os -from typing import Optional -from datetime import datetime, timedelta +from datetime import datetime from binance.client import Client from .base import ProductInfo, BaseWrapper, Price -from .error_handler import retry_on_failure, handle_api_errors, MarketAPIError +def get_product(currency: str, ticker_data: dict[str, str]) -> 'ProductInfo': + product = ProductInfo() + product.id = ticker_data.get('symbol') + product.symbol = ticker_data.get('symbol', '').replace(currency, '') + product.price = float(ticker_data.get('price', 0)) + product.volume_24h = float(ticker_data.get('volume', 0)) + product.status = "TRADING" # Binance non fornisce status esplicito + product.quote_currency = currency + return product class BinanceWrapper(BaseWrapper): """ - Wrapper per le API autenticate di Binance. - + Wrapper per le API autenticate di Binance.\n Implementa l'interfaccia BaseWrapper per fornire accesso unificato - ai dati di mercato di Binance tramite le API REST con autenticazione. - - La documentazione delle API è disponibile qui: + ai dati di mercato di Binance tramite le API REST con autenticazione.\n https://binance-docs.github.io/apidocs/spot/en/ """ - - def __init__(self, api_key: Optional[str] = None, api_secret: Optional[str] = None, currency: str = "USDT"): - """ - Inizializza il wrapper con le credenziali API. - - Args: - api_key: Chiave API di Binance (se None, usa variabile d'ambiente) - api_secret: Secret API di Binance (se None, usa variabile d'ambiente) - currency: Valuta di quotazione di default (default: USDT) - """ - if api_key is None: - api_key = os.getenv("BINANCE_API_KEY") + + def __init__(self, currency: str = "USDT"): + api_key = os.getenv("BINANCE_API_KEY") assert api_key is not None, "API key is required" - if api_secret is None: - api_secret = os.getenv("BINANCE_API_SECRET") + api_secret = os.getenv("BINANCE_API_SECRET") assert api_secret is not None, "API secret is required" self.currency = currency @@ -40,130 +34,58 @@ class BinanceWrapper(BaseWrapper): def __format_symbol(self, asset_id: str) -> str: """ Formatta l'asset_id nel formato richiesto da Binance. - - Args: - asset_id: ID dell'asset (es. "BTC" o "BTC-USDT") - - Returns: - Simbolo formattato per Binance (es. "BTCUSDT") """ - if '-' in asset_id: - # Se già nel formato "BTC-USDT", converte in "BTCUSDT" - return asset_id.replace('-', '') - else: - # Se solo "BTC", aggiunge la valuta di default - return f"{asset_id}{self.currency}" + return asset_id.replace('-', '') if '-' in asset_id else f"{asset_id}{self.currency}" - @retry_on_failure(max_retries=3, delay=1.0) - @handle_api_errors def get_product(self, asset_id: str) -> ProductInfo: - """ - Ottiene informazioni su un singolo prodotto. - - Args: - asset_id: ID dell'asset da recuperare - - Returns: - Oggetto ProductInfo con le informazioni del prodotto - """ symbol = self.__format_symbol(asset_id) + ticker = self.client.get_symbol_ticker(symbol=symbol) ticker_24h = self.client.get_ticker(symbol=symbol) - - return ProductInfo.from_binance(ticker, ticker_24h) + ticker['volume'] = ticker_24h.get('volume', 0) # Aggiunge volume 24h ai dati del ticker + + return get_product(self.currency, ticker) - @retry_on_failure(max_retries=3, delay=1.0) - @handle_api_errors def get_products(self, asset_ids: list[str]) -> list[ProductInfo]: - """ - Ottiene informazioni su più prodotti. - - Args: - asset_ids: Lista di ID degli asset da recuperare - - Returns: - Lista di oggetti ProductInfo - """ + symbols = [self.__format_symbol(asset_id) for asset_id in asset_ids] + symbols_str = f"[\"{'","'.join(symbols)}\"]" + + tickers = self.client.get_symbol_ticker(symbols=symbols_str) + tickers_24h = self.client.get_ticker(symbols=symbols_str) # un po brutale, ma va bene così + for t, t24 in zip(tickers, tickers_24h): + t['volume'] = t24.get('volume', 0) + + return [get_product(self.currency, ticker) for ticker in tickers] + + def get_all_products(self) -> list[ProductInfo]: + all_tickers = self.client.get_ticker() products = [] - for asset_id in asset_ids: - try: - product = self.get_product(asset_id) + + for ticker in all_tickers: + # Filtra solo i simboli che terminano con la valuta di default + if ticker['symbol'].endswith(self.currency): + product = get_product(self.currency, ticker) products.append(product) - except Exception as e: - print(f"Errore nel recupero di {asset_id}: {e}") - continue return products - @retry_on_failure(max_retries=3, delay=1.0) - @handle_api_errors - def get_all_products(self) -> list[ProductInfo]: - """ - Ottiene informazioni su tutti i prodotti disponibili. - - Returns: - Lista di oggetti ProductInfo per tutti i prodotti - """ - try: - # Ottiene tutti i ticker 24h che contengono le informazioni necessarie - all_tickers = self.client.get_ticker() - products = [] - - for ticker in all_tickers: - # Filtra solo i simboli che terminano con la valuta di default - if ticker['symbol'].endswith(self.currency): - try: - # Crea ProductInfo direttamente dal ticker 24h - product = ProductInfo() - product.id = ticker['symbol'] - product.symbol = ticker['symbol'].replace(self.currency, '') - product.price = float(ticker['lastPrice']) - product.volume_24h = float(ticker['volume']) - product.status = "TRADING" # Binance non fornisce status esplicito - product.quote_currency = self.currency - products.append(product) - except (ValueError, KeyError) as e: - print(f"Errore nel parsing di {ticker['symbol']}: {e}") - continue - - return products - except Exception as e: - print(f"Errore nel recupero di tutti i prodotti: {e}") - return [] - - @retry_on_failure(max_retries=3, delay=1.0) - @handle_api_errors - def get_historical_prices(self, asset_id: str = "BTC") -> list[Price]: - """ - Ottiene i prezzi storici per un asset. - - Args: - asset_id: ID dell'asset (default: "BTC") - - Returns: - Lista di oggetti Price con i dati storici - """ + def get_historical_prices(self, asset_id: str = "BTC", limit: int = 100) -> list[Price]: symbol = self.__format_symbol(asset_id) - - try: - # Ottiene candele orarie degli ultimi 30 giorni - klines = self.client.get_historical_klines( - symbol=symbol, - interval=Client.KLINE_INTERVAL_1HOUR, - start_str="30 days ago UTC" - ) - - prices = [] - for kline in klines: - price = Price() - price.open = float(kline[1]) - price.high = float(kline[2]) - price.low = float(kline[3]) - price.close = float(kline[4]) - price.volume = float(kline[5]) - price.time = str(datetime.fromtimestamp(kline[0] / 1000)) - prices.append(price) - - return prices - except Exception as e: - print(f"Errore nel recupero dei prezzi storici per {symbol}: {e}") - return [] + + # Ottiene candele orarie degli ultimi 30 giorni + klines = self.client.get_historical_klines( + symbol=symbol, + interval=Client.KLINE_INTERVAL_1HOUR, + limit=limit, + ) + + prices = [] + for kline in klines: + price = Price() + price.open = float(kline[1]) + price.high = float(kline[2]) + price.low = float(kline[3]) + price.close = float(kline[4]) + price.volume = float(kline[5]) + price.time = str(datetime.fromtimestamp(kline[0] / 1000)) + prices.append(price) + return prices diff --git a/src/app/markets/coinbase.py b/src/app/markets/coinbase.py index 95c6261..286ec6f 100644 --- a/src/app/markets/coinbase.py +++ b/src/app/markets/coinbase.py @@ -2,8 +2,29 @@ import os from enum import Enum from datetime import datetime, timedelta from coinbase.rest import RESTClient +from coinbase.rest.types.product_types import Candle, GetProductResponse, Product from .base import ProductInfo, BaseWrapper, Price -from .error_handler import retry_on_failure, handle_api_errors, MarketAPIError, RateLimitError + + +def get_product(product_data: GetProductResponse | Product) -> 'ProductInfo': + product = ProductInfo() + product.id = product_data.product_id or "" + product.symbol = product_data.base_currency_id or "" + product.price = float(product_data.price) if product_data.price else 0.0 + product.volume_24h = float(product_data.volume_24h) if product_data.volume_24h else 0.0 + # TODO Check what status means in Coinbase + product.status = product_data.status or "" + return product + +def get_price(candle_data: Candle) -> 'Price': + price = Price() + price.high = float(candle_data.high) if candle_data.high else 0.0 + price.low = float(candle_data.low) if candle_data.low else 0.0 + price.open = float(candle_data.open) if candle_data.open else 0.0 + price.close = float(candle_data.close) if candle_data.close else 0.0 + price.volume = float(candle_data.volume) if candle_data.volume else 0.0 + price.time = str(candle_data.start) if candle_data.start else "" + return price class Granularity(Enum): @@ -45,16 +66,16 @@ class CoinBaseWrapper(BaseWrapper): def get_product(self, asset_id: str) -> ProductInfo: asset_id = self.__format(asset_id) asset = self.client.get_product(asset_id) - return ProductInfo.from_coinbase(asset) + return get_product(asset) def get_products(self, asset_ids: list[str]) -> list[ProductInfo]: all_asset_ids = [self.__format(asset_id) for asset_id in asset_ids] assets = self.client.get_products(product_ids=all_asset_ids) - return [ProductInfo.from_coinbase(asset) for asset in assets.products] + return [get_product(asset) for asset in assets.products] def get_all_products(self) -> list[ProductInfo]: assets = self.client.get_products() - return [ProductInfo.from_coinbase_product(asset) for asset in assets.products] + return [get_product(asset) for asset in assets.products] def get_historical_prices(self, asset_id: str = "BTC", limit: int = 100) -> list[Price]: asset_id = self.__format(asset_id) @@ -68,4 +89,4 @@ class CoinBaseWrapper(BaseWrapper): end=str(int(end_time.timestamp())), limit=limit ) - return [Price.from_coinbase(candle) for candle in data.candles] + return [get_price(candle) for candle in data.candles] diff --git a/src/app/markets/cryptocompare.py b/src/app/markets/cryptocompare.py index 8b3cd08..c81a3bb 100644 --- a/src/app/markets/cryptocompare.py +++ b/src/app/markets/cryptocompare.py @@ -2,7 +2,27 @@ import os import requests from typing import Optional, Dict, Any from .base import ProductInfo, BaseWrapper, Price -from .error_handler import retry_on_failure, handle_api_errors, MarketAPIError + + +def get_product(asset_data: dict) -> 'ProductInfo': + product = ProductInfo() + product.id = asset_data['FROMSYMBOL'] + '-' + asset_data['TOSYMBOL'] + product.symbol = asset_data['FROMSYMBOL'] + product.price = float(asset_data['PRICE']) + product.volume_24h = float(asset_data['VOLUME24HOUR']) + product.status = "" # Cryptocompare does not provide status + return product + +def get_price(price_data: dict) -> 'Price': + price = Price() + price.high = float(price_data['high']) + price.low = float(price_data['low']) + price.open = float(price_data['open']) + price.close = float(price_data['close']) + price.volume = float(price_data['volumeto']) + price.time = str(price_data['time']) + return price + BASE_URL = "https://min-api.cryptocompare.com" @@ -33,7 +53,7 @@ class CryptoCompareWrapper(BaseWrapper): "tsyms": self.currency }) data = response.get('RAW', {}).get(asset_id, {}).get(self.currency, {}) - return ProductInfo.from_cryptocompare(data) + return get_product(data) def get_products(self, asset_ids: list[str]) -> list[ProductInfo]: response = self.__request("/data/pricemultifull", params = { @@ -44,10 +64,11 @@ class CryptoCompareWrapper(BaseWrapper): data = response.get('RAW', {}) for asset_id in asset_ids: asset_data = data.get(asset_id, {}).get(self.currency, {}) - assets.append(ProductInfo.from_cryptocompare(asset_data)) + assets.append(get_product(asset_data)) return assets def get_all_products(self) -> list[ProductInfo]: + # TODO serve davvero il workaroud qui? Possiamo prendere i dati da un altro endpoint intanto raise NotImplementedError("get_all_products is not supported by CryptoCompare API") def get_historical_prices(self, asset_id: str, limit: int = 100) -> list[dict]: @@ -58,5 +79,5 @@ class CryptoCompareWrapper(BaseWrapper): }) data = response.get('Data', {}).get('Data', []) - prices = [Price.from_cryptocompare(price_data) for price_data in data] + prices = [get_price(price_data) for price_data in data] return prices diff --git a/tests/api/test_binance.py b/tests/api/test_binance.py index 0a73c15..e4e0c20 100644 --- a/tests/api/test_binance.py +++ b/tests/api/test_binance.py @@ -1,7 +1,52 @@ import pytest +from app.markets.binance import BinanceWrapper @pytest.mark.market @pytest.mark.api class TestBinance: - # TODO fare dei test veri e propri - pass \ No newline at end of file + + def test_binance_init(self): + market = BinanceWrapper() + assert market is not None + assert hasattr(market, 'currency') + assert market.currency == "USDT" + + def test_binance_get_product(self): + market = BinanceWrapper() + product = market.get_product("BTC") + assert product is not None + assert hasattr(product, 'symbol') + assert product.symbol == "BTC" + assert hasattr(product, 'price') + assert product.price > 0 + + def test_binance_get_products(self): + market = BinanceWrapper() + products = market.get_products(["BTC", "ETH"]) + assert products is not None + assert isinstance(products, list) + assert len(products) == 2 + symbols = [p.symbol for p in products] + assert "BTC" in symbols + assert "ETH" in symbols + for product in products: + assert hasattr(product, 'price') + assert product.price > 0 + + def test_binance_invalid_product(self): + market = BinanceWrapper() + with pytest.raises(Exception): + _ = market.get_product("INVALID") + + def test_binance_history(self): + market = BinanceWrapper() + history = market.get_historical_prices("BTC", limit=5) + assert history is not None + assert isinstance(history, list) + assert len(history) == 5 + for entry in history: + assert hasattr(entry, 'time') + assert hasattr(entry, 'close') + assert hasattr(entry, 'high') + assert entry.close > 0 + assert entry.high > 0 From e8bf835fb3680342834c891292fab209c0aa9941 Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 30 Sep 2025 17:37:36 +0200 Subject: [PATCH 20/22] Add BinanceWrapper to market API exports --- src/app/markets/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/markets/__init__.py b/src/app/markets/__init__.py index 86a40e8..57f04b7 100644 --- a/src/app/markets/__init__.py +++ b/src/app/markets/__init__.py @@ -1,9 +1,10 @@ from .base import BaseWrapper from .coinbase import CoinBaseWrapper +from .binance import BinanceWrapper from .cryptocompare import CryptoCompareWrapper from app.utils.wrapper_handler import WrapperHandler -__all__ = [ "MarketAPIs", "BaseWrapper", "CoinBaseWrapper", "CryptoCompareWrapper" ] +__all__ = [ "MarketAPIs", "BinanceWrapper", "CoinBaseWrapper", "CryptoCompareWrapper" ] # TODO se si vuole usare un aggregatore di dati di mercato, si può aggiungere qui facendo una classe extra (simile a questa) che per ogni chiamata chiama tutti i wrapper e aggrega i risultati From 31057007fb6b6651ee951b92d474019f92059bbb Mon Sep 17 00:00:00 2001 From: Simone Garau <20005068@studenti.uniupo.it> Date: Wed, 1 Oct 2025 13:01:51 +0200 Subject: [PATCH 21/22] Finito ISSUE 3 --- demos/market_providers_api_demo.py | 2 +- pyproject.toml | 1 - src/app/markets/__init__.py | 93 ++++++++-- src/app/markets/binance_public.py | 9 - src/app/markets/error_handler.py | 237 ------------------------ src/app/pipeline.py | 1 - src/app/toolkits/market_toolkit.py | 4 +- src/app/utils/aggregated_models.py | 184 ++++++++++++++++++ src/app/utils/market_data_aggregator.py | 184 ++++++++++++++++++ tests/agents/test_market.py | 14 +- tests/test_market_data_aggregator.py | 90 +++++++++ 11 files changed, 547 insertions(+), 272 deletions(-) delete mode 100644 src/app/markets/error_handler.py create mode 100644 src/app/utils/aggregated_models.py create mode 100644 src/app/utils/market_data_aggregator.py create mode 100644 tests/test_market_data_aggregator.py diff --git a/demos/market_providers_api_demo.py b/demos/market_providers_api_demo.py index 16817d5..2c3a8f3 100644 --- a/demos/market_providers_api_demo.py +++ b/demos/market_providers_api_demo.py @@ -26,7 +26,7 @@ project_root = Path(__file__).parent.parent sys.path.insert(0, str(project_root / "src")) from dotenv import load_dotenv -from src.app.markets import ( +from app.markets import ( CoinBaseWrapper, CryptoCompareWrapper, BinanceWrapper, diff --git a/pyproject.toml b/pyproject.toml index 74c0029..e091aba 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,6 @@ dependencies = [ # API di social media "praw", # Reddit ] - [tool.pytest.ini_options] pythonpath = ["src"] testpaths = ["tests"] diff --git a/src/app/markets/__init__.py b/src/app/markets/__init__.py index 57f04b7..e5853d5 100644 --- a/src/app/markets/__init__.py +++ b/src/app/markets/__init__.py @@ -1,31 +1,96 @@ -from .base import BaseWrapper +from .base import BaseWrapper, ProductInfo, Price from .coinbase import CoinBaseWrapper from .binance import BinanceWrapper from .cryptocompare import CryptoCompareWrapper +from .binance_public import PublicBinanceAgent from app.utils.wrapper_handler import WrapperHandler - -__all__ = [ "MarketAPIs", "BinanceWrapper", "CoinBaseWrapper", "CryptoCompareWrapper" ] +from typing import List, Optional +from agno.tools import Toolkit -# TODO se si vuole usare un aggregatore di dati di mercato, si può aggiungere qui facendo una classe extra (simile a questa) che per ogni chiamata chiama tutti i wrapper e aggrega i risultati -class MarketAPIs(BaseWrapper): +__all__ = [ "MarketAPIs", "BinanceWrapper", "CoinBaseWrapper", "CryptoCompareWrapper", "PublicBinanceAgent" ] + + +class MarketAPIsTool(BaseWrapper, Toolkit): """ Classe per gestire le API di mercato disponibili. - Permette di ottenere un'istanza della prima API disponibile in base alla priorità specificata. - Supporta operazioni come ottenere informazioni su singoli prodotti, liste di prodotti e dati storici. - Usa un WrapperHandler per gestire più wrapper e tentare chiamate in modo resiliente. + + Supporta due modalità: + 1. **Modalità standard** (default): usa il primo wrapper disponibile + 2. **Modalità aggregazione**: aggrega dati da tutte le fonti disponibili + + L'aggregazione può essere abilitata/disabilitata dinamicamente. """ - def __init__(self, currency: str = "USD"): + def __init__(self, currency: str = "USD", enable_aggregation: bool = False): self.currency = currency - wrappers = [ CoinBaseWrapper, CryptoCompareWrapper ] + wrappers = [ BinanceWrapper, CoinBaseWrapper, CryptoCompareWrapper ] self.wrappers: WrapperHandler[BaseWrapper] = WrapperHandler.build_wrappers(wrappers) + + # Inizializza l'aggregatore solo se richiesto (lazy initialization) + self._aggregator = None + self._aggregation_enabled = enable_aggregation + + Toolkit.__init__( + self, + name="Market APIs Toolkit", + tools=[ + self.get_product, + self.get_products, + self.get_all_products, + self.get_historical_prices, + ], + ) + + def _get_aggregator(self): + """Lazy initialization dell'aggregatore""" + if self._aggregator is None: + from app.utils.market_data_aggregator import MarketDataAggregator + self._aggregator = MarketDataAggregator(self.currency) + self._aggregator.enable_aggregation(self._aggregation_enabled) + return self._aggregator - def get_product(self, asset_id): + def get_product(self, asset_id: str) -> Optional[ProductInfo]: + """Ottieni informazioni su un prodotto specifico""" + if self._aggregation_enabled: + return self._get_aggregator().get_product(asset_id) return self.wrappers.try_call(lambda w: w.get_product(asset_id)) - def get_products(self, asset_ids: list): + + def get_products(self, asset_ids: List[str]) -> List[ProductInfo]: + """Ottieni informazioni su multiple prodotti""" + if self._aggregation_enabled: + return self._get_aggregator().get_products(asset_ids) return self.wrappers.try_call(lambda w: w.get_products(asset_ids)) - def get_all_products(self): + + def get_all_products(self) -> List[ProductInfo]: + """Ottieni tutti i prodotti disponibili""" + if self._aggregation_enabled: + return self._get_aggregator().get_all_products() return self.wrappers.try_call(lambda w: w.get_all_products()) - def get_historical_prices(self, asset_id = "BTC", limit: int = 100): + + def get_historical_prices(self, asset_id: str = "BTC", limit: int = 100) -> List[Price]: + """Ottieni dati storici dei prezzi""" + if self._aggregation_enabled: + return self._get_aggregator().get_historical_prices(asset_id, limit) return self.wrappers.try_call(lambda w: w.get_historical_prices(asset_id, limit)) + + # Metodi per controllare l'aggregazione + def enable_aggregation(self, enabled: bool = True): + """Abilita/disabilita la modalità aggregazione""" + self._aggregation_enabled = enabled + if self._aggregator: + self._aggregator.enable_aggregation(enabled) + + def is_aggregation_enabled(self) -> bool: + """Verifica se l'aggregazione è abilitata""" + return self._aggregation_enabled + + # Metodo speciale per debugging (opzionale) + def get_aggregated_product_with_debug(self, asset_id: str) -> dict: + """ + Metodo speciale per ottenere dati aggregati con informazioni di debug. + Disponibile solo quando l'aggregazione è abilitata. + """ + if not self._aggregation_enabled: + raise RuntimeError("L'aggregazione deve essere abilitata per usare questo metodo") + return self._get_aggregator().get_aggregated_product_with_debug(asset_id) diff --git a/src/app/markets/binance_public.py b/src/app/markets/binance_public.py index 598840b..c1d9896 100644 --- a/src/app/markets/binance_public.py +++ b/src/app/markets/binance_public.py @@ -9,7 +9,6 @@ from typing import Optional, Dict, Any from datetime import datetime, timedelta from binance.client import Client from .base import BaseWrapper, ProductInfo, Price -from .error_handler import retry_on_failure, handle_api_errors, MarketAPIError class PublicBinanceAgent(BaseWrapper): @@ -38,8 +37,6 @@ class PublicBinanceAgent(BaseWrapper): return asset_id return f"{asset_id}USDT" - @retry_on_failure(max_retries=3, delay=1.0) - @handle_api_errors def get_product(self, asset_id: str) -> ProductInfo: """ Ottiene informazioni su un singolo prodotto. @@ -59,8 +56,6 @@ class PublicBinanceAgent(BaseWrapper): print(f"Errore nel recupero del prodotto {asset_id}: {e}") return ProductInfo(id=asset_id, symbol=asset_id) - @retry_on_failure(max_retries=3, delay=1.0) - @handle_api_errors def get_products(self, asset_ids: list[str]) -> list[ProductInfo]: """ Ottiene informazioni su più prodotti. @@ -77,8 +72,6 @@ class PublicBinanceAgent(BaseWrapper): products.append(product) return products - @retry_on_failure(max_retries=3, delay=1.0) - @handle_api_errors def get_all_products(self) -> list[ProductInfo]: """ Ottiene informazioni su tutti i prodotti disponibili. @@ -90,8 +83,6 @@ class PublicBinanceAgent(BaseWrapper): major_assets = ["BTC", "ETH", "BNB", "ADA", "DOT", "LINK", "LTC", "XRP"] return self.get_products(major_assets) - @retry_on_failure(max_retries=3, delay=1.0) - @handle_api_errors def get_historical_prices(self, asset_id: str = "BTC") -> list[Price]: """ Ottiene i prezzi storici per un asset. diff --git a/src/app/markets/error_handler.py b/src/app/markets/error_handler.py deleted file mode 100644 index c98301a..0000000 --- a/src/app/markets/error_handler.py +++ /dev/null @@ -1,237 +0,0 @@ -""" -Modulo per la gestione robusta degli errori nei market providers. - -Fornisce decoratori e utilità per: -- Retry automatico con backoff esponenziale -- Logging standardizzato degli errori -- Gestione di timeout e rate limiting -- Fallback tra provider multipli -""" - -import time -import logging -from functools import wraps -from typing import Any, Callable, Optional, Type, Union, List -from requests.exceptions import RequestException, Timeout, ConnectionError -from binance.exceptions import BinanceAPIException, BinanceRequestException -from .base import ProductInfo - -# Configurazione logging -logger = logging.getLogger(__name__) - -class MarketAPIError(Exception): - """Eccezione base per errori delle API di mercato.""" - pass - -class RateLimitError(MarketAPIError): - """Eccezione per errori di rate limiting.""" - pass - -class AuthenticationError(MarketAPIError): - """Eccezione per errori di autenticazione.""" - pass - -class DataNotFoundError(MarketAPIError): - """Eccezione quando i dati richiesti non sono disponibili.""" - pass - -def retry_on_failure( - max_retries: int = 3, - delay: float = 1.0, - backoff_factor: float = 2.0, - exceptions: tuple = (RequestException, BinanceAPIException, BinanceRequestException) -) -> Callable: - """ - Decoratore per retry automatico con backoff esponenziale. - - Args: - max_retries: Numero massimo di tentativi - delay: Delay iniziale in secondi - backoff_factor: Fattore di moltiplicazione per il delay - exceptions: Tuple di eccezioni da catturare per il retry - - Returns: - Decoratore per la funzione - """ - def decorator(func: Callable) -> Callable: - @wraps(func) - def wrapper(*args, **kwargs) -> Any: - last_exception = None - current_delay = delay - - for attempt in range(max_retries + 1): - try: - return func(*args, **kwargs) - except exceptions as e: - last_exception = e - - if attempt == max_retries: - logger.error( - f"Function {func.__name__} failed after {max_retries + 1} attempts. " - f"Last error: {str(e)}" - ) - raise MarketAPIError(f"Max retries exceeded: {str(e)}") from e - - logger.warning( - f"Attempt {attempt + 1}/{max_retries + 1} failed for {func.__name__}: {str(e)}. " - f"Retrying in {current_delay:.1f}s..." - ) - - time.sleep(current_delay) - current_delay *= backoff_factor - except Exception as e: - # Per eccezioni non previste, non fare retry - logger.error(f"Unexpected error in {func.__name__}: {str(e)}") - raise - - # Questo non dovrebbe mai essere raggiunto - if last_exception: - raise last_exception - else: - raise MarketAPIError("Unknown error occurred") - - return wrapper - return decorator - -def handle_api_errors(func: Callable) -> Callable: - """ - Decoratore per gestione standardizzata degli errori API. - - Converte errori specifici dei provider in eccezioni standardizzate. - """ - @wraps(func) - def wrapper(*args, **kwargs) -> Any: - try: - return func(*args, **kwargs) - except BinanceAPIException as e: - if e.code == -1021: # Timestamp error - raise MarketAPIError(f"Binance timestamp error: {e.message}") - elif e.code == -1003: # Rate limit - raise RateLimitError(f"Binance rate limit exceeded: {e.message}") - elif e.code in [-2014, -2015]: # API key errors - raise AuthenticationError(f"Binance authentication error: {e.message}") - else: - raise MarketAPIError(f"Binance API error [{e.code}]: {e.message}") - except ConnectionError as e: - raise MarketAPIError(f"Connection error: {str(e)}") - except Timeout as e: - raise MarketAPIError(f"Request timeout: {str(e)}") - except RequestException as e: - raise MarketAPIError(f"Request error: {str(e)}") - except Exception as e: - logger.error(f"Unexpected error in {func.__name__}: {str(e)}") - raise MarketAPIError(f"Unexpected error: {str(e)}") from e - - return wrapper - -def safe_execute( - func: Callable, - default_value: Any = None, - log_errors: bool = True, - error_message: Optional[str] = None -) -> Any: - """ - Esegue una funzione in modo sicuro, restituendo un valore di default in caso di errore. - - Args: - func: Funzione da eseguire - default_value: Valore da restituire in caso di errore - log_errors: Se loggare gli errori - error_message: Messaggio di errore personalizzato - - Returns: - Risultato della funzione o valore di default - """ - try: - return func() - except Exception as e: - if log_errors: - message = error_message or f"Error executing {func.__name__}" - logger.warning(f"{message}: {str(e)}") - return default_value - -class ProviderFallback: - """ - Classe per gestire il fallback tra provider multipli. - """ - - def __init__(self, providers: List[Any]): - """ - Inizializza con una lista di provider ordinati per priorità. - - Args: - providers: Lista di provider ordinati per priorità - """ - self.providers = providers - - def execute_with_fallback( - self, - method_name: str, - *args, - **kwargs - ) -> list[ProductInfo]: - """ - Esegue un metodo su tutti i provider fino a trovarne uno che funziona. - - Args: - method_name: Nome del metodo da chiamare - *args: Argomenti posizionali - **kwargs: Argomenti nominali - - Returns: - Risultato del primo provider che funziona - - Raises: - MarketAPIError: Se tutti i provider falliscono - """ - last_error = None - - for i, provider in enumerate(self.providers): - try: - if hasattr(provider, method_name): - method = getattr(provider, method_name) - result = method(*args, **kwargs) - - if i > 0: # Se non è il primo provider - logger.info(f"Fallback successful: used provider {type(provider).__name__}") - - return result - else: - logger.warning(f"Provider {type(provider).__name__} doesn't have method {method_name}") - continue - - except Exception as e: - last_error = e - logger.warning( - f"Provider {type(provider).__name__} failed for {method_name}: {str(e)}" - ) - continue - - # Se arriviamo qui, tutti i provider hanno fallito - raise MarketAPIError( - f"All providers failed for method {method_name}. Last error: {str(last_error)}" - ) - -def validate_response_data(data: Any, required_fields: Optional[List[str]] = None) -> bool: - """ - Valida che i dati di risposta contengano i campi richiesti. - - Args: - data: Dati da validare - required_fields: Lista di campi richiesti - - Returns: - True se i dati sono validi, False altrimenti - """ - if data is None: - return False - - if required_fields is None: - return True - - if isinstance(data, dict): - return all(field in data for field in required_fields) - elif hasattr(data, '__dict__'): - return all(hasattr(data, field) for field in required_fields) - - return False \ No newline at end of file diff --git a/src/app/pipeline.py b/src/app/pipeline.py index f515053..7a440de 100644 --- a/src/app/pipeline.py +++ b/src/app/pipeline.py @@ -6,7 +6,6 @@ from agno.utils.log import log_info from app.agents.market_agent import MarketAgent from app.agents.news_agent import NewsAgent from app.agents.social_agent import SocialAgent -from app.markets import MarketAPIs from app.models import AppModels from app.predictor import PredictorStyle, PredictorInput, PredictorOutput, PREDICTOR_INSTRUCTIONS diff --git a/src/app/toolkits/market_toolkit.py b/src/app/toolkits/market_toolkit.py index ff6e48d..61a4d9f 100644 --- a/src/app/toolkits/market_toolkit.py +++ b/src/app/toolkits/market_toolkit.py @@ -1,5 +1,5 @@ from agno.tools import Toolkit -from app.markets import MarketAPIs +from app.markets import MarketAPIsTool # TODO (?) in futuro fare in modo che la LLM faccia da sé per il mercato @@ -9,7 +9,7 @@ from app.markets import MarketAPIs # in base alle sue proprie chiamate API class MarketToolkit(Toolkit): def __init__(self): - self.market_api = MarketAPIs("USD") # change currency if needed + self.market_api = MarketAPIsTool("USD") # change currency if needed super().__init__( name="Market Toolkit", diff --git a/src/app/utils/aggregated_models.py b/src/app/utils/aggregated_models.py new file mode 100644 index 0000000..8eba8a5 --- /dev/null +++ b/src/app/utils/aggregated_models.py @@ -0,0 +1,184 @@ +import statistics +from typing import Dict, List, Optional, Set +from pydantic import BaseModel, Field, PrivateAttr +from app.markets.base import ProductInfo + +class AggregationMetadata(BaseModel): + """Metadati nascosti per debugging e audit trail""" + sources_used: Set[str] = Field(default_factory=set, description="Exchange usati nell'aggregazione") + sources_ignored: Set[str] = Field(default_factory=set, description="Exchange ignorati (errori)") + aggregation_timestamp: str = Field(default="", description="Timestamp dell'aggregazione") + confidence_score: float = Field(default=0.0, description="Score 0-1 sulla qualità dei dati") + + class Config: + # Nasconde questi campi dalla serializzazione di default + extra = "forbid" + +class AggregatedProductInfo(ProductInfo): + """ + Versione aggregata di ProductInfo che mantiene la trasparenza per l'utente finale + mentre fornisce metadati di debugging opzionali. + """ + + # Override dei campi con logica di aggregazione + id: str = Field(description="ID aggregato basato sul simbolo standardizzato") + status: str = Field(description="Status aggregato (majority vote o conservative)") + + # Campi privati per debugging (non visibili di default) + _metadata: Optional[AggregationMetadata] = PrivateAttr(default=None) + _source_data: Optional[Dict[str, ProductInfo]] = PrivateAttr(default=None) + + @classmethod + def from_multiple_sources(cls, products: List[ProductInfo]) -> 'AggregatedProductInfo': + """ + Crea un AggregatedProductInfo da una lista di ProductInfo. + Usa strategie intelligenti per gestire ID e status. + """ + if not products: + raise ValueError("Nessun prodotto da aggregare") + + # Raggruppa per symbol (la chiave vera per l'aggregazione) + symbol_groups = {} + for product in products: + if product.symbol not in symbol_groups: + symbol_groups[product.symbol] = [] + symbol_groups[product.symbol].append(product) + + # Per ora gestiamo un symbol alla volta + if len(symbol_groups) > 1: + raise ValueError(f"Simboli multipli non supportati: {list(symbol_groups.keys())}") + + symbol_products = list(symbol_groups.values())[0] + + # Estrai tutte le fonti + sources = [] + for product in symbol_products: + # Determina la fonte dall'ID o da altri metadati se disponibili + source = cls._detect_source(product) + sources.append(source) + + # Aggrega i dati + aggregated_data = cls._aggregate_products(symbol_products, sources) + + # Crea l'istanza e assegna gli attributi privati + instance = cls(**aggregated_data) + instance._metadata = aggregated_data.get("_metadata") + instance._source_data = aggregated_data.get("_source_data") + + return instance + + @staticmethod + def _detect_source(product: ProductInfo) -> str: + """Rileva la fonte da un ProductInfo""" + # Strategia semplice: usa pattern negli ID + if "coinbase" in product.id.lower() or "cb" in product.id.lower(): + return "coinbase" + elif "binance" in product.id.lower() or "bn" in product.id.lower(): + return "binance" + elif "crypto" in product.id.lower() or "cc" in product.id.lower(): + return "cryptocompare" + else: + return "unknown" + + @classmethod + def _aggregate_products(cls, products: List[ProductInfo], sources: List[str]) -> dict: + """ + Logica di aggregazione principale. + Gestisce ID, status e altri campi numerici. + """ + import statistics + from datetime import datetime + + # ID: usa il symbol come chiave standardizzata + symbol = products[0].symbol + aggregated_id = f"{symbol}_AGG" + + # Status: strategia "conservativa" - il più restrittivo vince + # Ordine: trading_only < limit_only < auction < maintenance < offline + status_priority = { + "trading": 1, + "limit_only": 2, + "auction": 3, + "maintenance": 4, + "offline": 5, + "": 0 # Default se non specificato + } + + statuses = [p.status for p in products if p.status] + if statuses: + # Prendi lo status con priorità più alta (più restrittivo) + aggregated_status = max(statuses, key=lambda s: status_priority.get(s, 0)) + else: + aggregated_status = "trading" # Default ottimistico + + # Prezzo: media semplice (uso diretto del campo price come float) + prices = [p.price for p in products if p.price > 0] + aggregated_price = statistics.mean(prices) if prices else 0.0 + + # Volume: somma (assumendo che i volumi siano esclusivi per exchange) + volumes = [p.volume_24h for p in products if p.volume_24h > 0] + total_volume = sum(volumes) + aggregated_volume = sum(price_i * volume_i for price_i, volume_i in zip((p.price for p in products), (volume for volume in volumes))) / total_volume + aggregated_volume = round(aggregated_volume, 5) + # aggregated_volume = sum(volumes) if volumes else 0.0 # NOTE old implementation + + # Valuta: prendi la prima (dovrebbero essere tutte uguali) + quote_currency = next((p.quote_currency for p in products if p.quote_currency), "USD") + + # Calcola confidence score + confidence = cls._calculate_confidence(products, sources) + + # Crea metadati per debugging + metadata = AggregationMetadata( + sources_used=set(sources), + aggregation_timestamp=datetime.now().isoformat(), + confidence_score=confidence + ) + + # Salva dati sorgente per debugging + source_data = dict(zip(sources, products)) + + return { + "symbol": symbol, + "price": aggregated_price, + "volume_24h": aggregated_volume, + "quote_currency": quote_currency, + "id": aggregated_id, + "status": aggregated_status, + "_metadata": metadata, + "_source_data": source_data + } + + @staticmethod + def _calculate_confidence(products: List[ProductInfo], sources: List[str]) -> float: + """Calcola un punteggio di confidenza 0-1""" + if not products: + return 0.0 + + score = 1.0 + + # Riduci score se pochi dati + if len(products) < 2: + score *= 0.7 + + # Riduci score se prezzi troppo diversi + prices = [p.price for p in products if p.price > 0] + if len(prices) > 1: + price_std = (max(prices) - min(prices)) / statistics.mean(prices) + if price_std > 0.05: # >5% variazione + score *= 0.8 + + # Riduci score se fonti sconosciute + unknown_sources = sum(1 for s in sources if s == "unknown") + if unknown_sources > 0: + score *= (1 - unknown_sources / len(sources)) + + return max(0.0, min(1.0, score)) + + def get_debug_info(self) -> dict: + """Metodo opzionale per ottenere informazioni di debug""" + return { + "aggregated_product": self.dict(), + "metadata": self._metadata.dict() if self._metadata else None, + "sources": list(self._source_data.keys()) if self._source_data else [] + } \ No newline at end of file diff --git a/src/app/utils/market_data_aggregator.py b/src/app/utils/market_data_aggregator.py new file mode 100644 index 0000000..f72e91c --- /dev/null +++ b/src/app/utils/market_data_aggregator.py @@ -0,0 +1,184 @@ +from typing import List, Optional, Dict, Any +from app.markets.base import ProductInfo, Price +from app.utils.aggregated_models import AggregatedProductInfo + +class MarketDataAggregator: + """ + Aggregatore di dati di mercato che mantiene la trasparenza per l'utente. + + Compone MarketAPIs per fornire gli stessi metodi, ma restituisce dati aggregati + da tutte le fonti disponibili. L'utente finale non vede la complessità. + """ + + def __init__(self, currency: str = "USD"): + # Import lazy per evitare circular import + from app.markets import MarketAPIs + self._market_apis = MarketAPIs(currency) + self._aggregation_enabled = True + + def get_product(self, asset_id: str) -> ProductInfo: + """ + Override che aggrega dati da tutte le fonti disponibili. + Per l'utente sembra un normale ProductInfo. + """ + if not self._aggregation_enabled: + return self._market_apis.get_product(asset_id) + + # Raccogli dati da tutte le fonti + try: + raw_results = self.wrappers.try_call_all( + lambda wrapper: wrapper.get_product(asset_id) + ) + + # Converti in ProductInfo se necessario + products = [] + for wrapper_class, result in raw_results.items(): + if isinstance(result, ProductInfo): + products.append(result) + elif isinstance(result, dict): + # Converti dizionario in ProductInfo + products.append(ProductInfo(**result)) + + if not products: + raise Exception("Nessun dato disponibile") + + # Aggrega i risultati + aggregated = AggregatedProductInfo.from_multiple_sources(products) + + # Restituisci come ProductInfo normale (nascondi la complessità) + return ProductInfo(**aggregated.dict(exclude={"_metadata", "_source_data"})) + + except Exception as e: + # Fallback: usa il comportamento normale se l'aggregazione fallisce + return self._market_apis.get_product(asset_id) + + def get_products(self, asset_ids: List[str]) -> List[ProductInfo]: + """ + Aggrega dati per multiple asset. + """ + if not self._aggregation_enabled: + return self._market_apis.get_products(asset_ids) + + aggregated_products = [] + + for asset_id in asset_ids: + try: + product = self.get_product(asset_id) + aggregated_products.append(product) + except Exception as e: + # Salta asset che non riescono ad aggregare + continue + + return aggregated_products + + def get_all_products(self) -> List[ProductInfo]: + """ + Aggrega tutti i prodotti disponibili. + """ + if not self._aggregation_enabled: + return self._market_apis.get_all_products() + + # Raccogli tutti i prodotti da tutte le fonti + try: + all_products_by_source = self.wrappers.try_call_all( + lambda wrapper: wrapper.get_all_products() + ) + + # Raggruppa per symbol per aggregare + symbol_groups = {} + for wrapper_class, products in all_products_by_source.items(): + if not isinstance(products, list): + continue + + for product in products: + if isinstance(product, dict): + product = ProductInfo(**product) + + if product.symbol not in symbol_groups: + symbol_groups[product.symbol] = [] + symbol_groups[product.symbol].append(product) + + # Aggrega ogni gruppo + aggregated_products = [] + for symbol, products in symbol_groups.items(): + try: + aggregated = AggregatedProductInfo.from_multiple_sources(products) + # Restituisci come ProductInfo normale + aggregated_products.append( + ProductInfo(**aggregated.dict(exclude={"_metadata", "_source_data"})) + ) + except Exception: + # Se l'aggregazione fallisce, usa il primo disponibile + if products: + aggregated_products.append(products[0]) + + return aggregated_products + + except Exception as e: + # Fallback: usa il comportamento normale + return self._market_apis.get_all_products() + + def get_historical_prices(self, asset_id: str = "BTC", limit: int = 100) -> List[Price]: + """ + Per i dati storici, usa una strategia diversa: + prendi i dati dalla fonte più affidabile o aggrega se possibile. + """ + if not self._aggregation_enabled: + return self._market_apis.get_historical_prices(asset_id, limit) + + # Per dati storici, usa il primo wrapper che funziona + # (l'aggregazione di dati storici è più complessa) + try: + return self.wrappers.try_call( + lambda wrapper: wrapper.get_historical_prices(asset_id, limit) + ) + except Exception as e: + # Fallback: usa il comportamento normale + return self._market_apis.get_historical_prices(asset_id, limit) + + def enable_aggregation(self, enabled: bool = True): + """Abilita o disabilita l'aggregazione""" + self._aggregation_enabled = enabled + + def is_aggregation_enabled(self) -> bool: + """Controlla se l'aggregazione è abilitata""" + return self._aggregation_enabled + + # Metodi proxy per completare l'interfaccia BaseWrapper + @property + def wrappers(self): + """Accesso al wrapper handler per compatibilità""" + return self._market_apis.wrappers + + def get_aggregated_product_with_debug(self, asset_id: str) -> Dict[str, Any]: + """ + Metodo speciale per debugging: restituisce dati aggregati con metadati. + Usato solo per testing e monitoraggio. + """ + try: + raw_results = self.wrappers.try_call_all( + lambda wrapper: wrapper.get_product(asset_id) + ) + + products = [] + for wrapper_class, result in raw_results.items(): + if isinstance(result, ProductInfo): + products.append(result) + elif isinstance(result, dict): + products.append(ProductInfo(**result)) + + if not products: + raise Exception("Nessun dato disponibile") + + aggregated = AggregatedProductInfo.from_multiple_sources(products) + + return { + "product": aggregated.dict(exclude={"_metadata", "_source_data"}), + "debug": aggregated.get_debug_info() + } + + except Exception as e: + return { + "error": str(e), + "debug": {"error": str(e)} + } \ No newline at end of file diff --git a/tests/agents/test_market.py b/tests/agents/test_market.py index 5fef76e..10e5cb4 100644 --- a/tests/agents/test_market.py +++ b/tests/agents/test_market.py @@ -1,12 +1,12 @@ import os import pytest from app.agents.market_agent import MarketToolkit -from app.markets import MarketAPIs +from app.markets import MarketAPIsTool @pytest.mark.limited # usa molte api calls e non voglio esaurire le chiavi api -class TestMarketAPIs: +class TestMarketAPIsTool: def test_wrapper_initialization(self): - market_wrapper = MarketAPIs("USD") + market_wrapper = MarketAPIsTool("USD") assert market_wrapper is not None assert hasattr(market_wrapper, 'get_product') assert hasattr(market_wrapper, 'get_products') @@ -14,7 +14,7 @@ class TestMarketAPIs: assert hasattr(market_wrapper, 'get_historical_prices') def test_wrapper_capabilities(self): - market_wrapper = MarketAPIs("USD") + market_wrapper = MarketAPIsTool("USD") capabilities = [] if hasattr(market_wrapper, 'get_product'): capabilities.append('single_product') @@ -25,7 +25,7 @@ class TestMarketAPIs: assert len(capabilities) > 0 def test_market_data_retrieval(self): - market_wrapper = MarketAPIs("USD") + market_wrapper = MarketAPIsTool("USD") btc_product = market_wrapper.get_product("BTC") assert btc_product is not None assert hasattr(btc_product, 'symbol') @@ -55,14 +55,14 @@ class TestMarketAPIs: def test_error_handling(self): try: - market_wrapper = MarketAPIs("USD") + market_wrapper = MarketAPIsTool("USD") fake_product = market_wrapper.get_product("NONEXISTENT_CRYPTO_SYMBOL_12345") assert fake_product is None or fake_product.price == 0 except Exception as e: pass def test_wrapper_currency_support(self): - market_wrapper = MarketAPIs("USD") + market_wrapper = MarketAPIsTool("USD") assert hasattr(market_wrapper, 'currency') assert isinstance(market_wrapper.currency, str) assert len(market_wrapper.currency) >= 3 # USD, EUR, etc. diff --git a/tests/test_market_data_aggregator.py b/tests/test_market_data_aggregator.py new file mode 100644 index 0000000..604362c --- /dev/null +++ b/tests/test_market_data_aggregator.py @@ -0,0 +1,90 @@ +import pytest + +from app.utils.market_data_aggregator import MarketDataAggregator +from app.utils.aggregated_models import AggregatedProductInfo +from app.markets.base import ProductInfo, Price + + +class TestMarketDataAggregator: + + def test_initialization(self): + """Test che il MarketDataAggregator si inizializzi correttamente""" + aggregator = MarketDataAggregator() + assert aggregator is not None + assert aggregator.is_aggregation_enabled() == True + + def test_aggregation_toggle(self): + """Test del toggle dell'aggregazione""" + aggregator = MarketDataAggregator() + + # Disabilita aggregazione + aggregator.enable_aggregation(False) + assert aggregator.is_aggregation_enabled() == False + + # Riabilita aggregazione + aggregator.enable_aggregation(True) + assert aggregator.is_aggregation_enabled() == True + + def test_aggregated_product_info_creation(self): + """Test creazione AggregatedProductInfo da fonti multiple""" + + # Crea dati di esempio + product1 = ProductInfo( + id="BTC-USD", + symbol="BTC-USD", + price=50000.0, + volume_24h=1000.0, + status="active", + quote_currency="USD" + ) + + product2 = ProductInfo( + id="BTC-USD", + symbol="BTC-USD", + price=50100.0, + volume_24h=1100.0, + status="active", + quote_currency="USD" + ) + + # Aggrega i prodotti + aggregated = AggregatedProductInfo.from_multiple_sources([product1, product2]) + + assert aggregated.symbol == "BTC-USD" + assert aggregated.price == pytest.approx(50050.0, rel=1e-3) # media tra 50000 e 50100 + assert aggregated.volume_24h == 50052.38095 # somma dei volumi + assert aggregated.status == "active" # majority vote + assert aggregated.id == "BTC-USD_AGG" # mapping_id con suffisso aggregazione + + def test_confidence_calculation(self): + """Test del calcolo della confidence""" + + product1 = ProductInfo( + id="BTC-USD", + symbol="BTC-USD", + price=50000.0, + volume_24h=1000.0, + status="active", + quote_currency="USD" + ) + + product2 = ProductInfo( + id="BTC-USD", + symbol="BTC-USD", + price=50100.0, + volume_24h=1100.0, + status="active", + quote_currency="USD" + ) + + aggregated = AggregatedProductInfo.from_multiple_sources([product1, product2]) + + # Verifica che ci siano metadati + assert aggregated._metadata is not None + assert len(aggregated._metadata.sources_used) > 0 + assert aggregated._metadata.aggregation_timestamp != "" + # La confidence può essere 0.0 se ci sono fonti "unknown" + + +if __name__ == "__main__": + pytest.main([__file__]) \ No newline at end of file From 42690acfbb886e34b81e39d0fb74f8fe178a75c8 Mon Sep 17 00:00:00 2001 From: Simone Garau <20005068@studenti.uniupo.it> Date: Wed, 1 Oct 2025 15:46:46 +0200 Subject: [PATCH 22/22] feat(markets): add yfinance integration for stock and crypto data - Add yfinance wrapper with support for stocks and cryptocurrencies - Update aggregated models to recognize yfinance products - Include yfinance in market APIs tool and demo script - Add comprehensive tests for yfinance functionality - Update dependencies to include yfinance and required packages --- demos/market_providers_api_demo.py | 9 + pyproject.toml | 1 + src/app/markets/__init__.py | 5 +- src/app/markets/binance.py | 4 +- src/app/markets/yfinance.py | 214 ++++++++++++++++++ src/app/utils/aggregated_models.py | 2 + src/app/utils/market_aggregator.py | 71 ------ src/app/utils/market_data_aggregator.py | 4 +- tests/api/test_yfinance.py | 93 ++++++++ .../test_market_data_aggregator.py | 2 +- uv.lock | 91 ++++++++ 11 files changed, 418 insertions(+), 78 deletions(-) create mode 100644 src/app/markets/yfinance.py delete mode 100644 src/app/utils/market_aggregator.py create mode 100644 tests/api/test_yfinance.py rename tests/{ => utils}/test_market_data_aggregator.py (99%) diff --git a/demos/market_providers_api_demo.py b/demos/market_providers_api_demo.py index 2c3a8f3..c3718fa 100644 --- a/demos/market_providers_api_demo.py +++ b/demos/market_providers_api_demo.py @@ -8,6 +8,7 @@ Questo script dimostra l'utilizzo di tutti i wrapper che implementano BaseWrappe - CryptoCompareWrapper (richiede API key) - BinanceWrapper (richiede credenziali) - PublicBinanceAgent (accesso pubblico) +- YFinanceWrapper (accesso gratuito a dati azionari e crypto) Lo script effettua chiamate GET a diversi provider e visualizza i dati in modo strutturato con informazioni dettagliate su timestamp, stato @@ -31,6 +32,7 @@ from app.markets import ( CryptoCompareWrapper, BinanceWrapper, PublicBinanceAgent, + YFinanceWrapper, BaseWrapper ) @@ -276,6 +278,13 @@ def initialize_providers() -> Dict[str, BaseWrapper]: else: print("⚠️ BinanceWrapper saltato: credenziali Binance non complete") + # YFinanceWrapper (sempre disponibile - dati azionari e crypto gratuiti) + try: + providers["YFinance"] = YFinanceWrapper() + print("✅ YFinanceWrapper inizializzato con successo") + except Exception as e: + print(f"❌ Errore nell'inizializzazione di YFinanceWrapper: {e}") + return providers def print_summary(results: List[Dict[str, Any]]): diff --git a/pyproject.toml b/pyproject.toml index e091aba..2e90e39 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,7 @@ dependencies = [ # API di exchange di criptovalute "coinbase-advanced-py", "python-binance", + "yfinance", # API di notizie "newsapi-python", diff --git a/src/app/markets/__init__.py b/src/app/markets/__init__.py index e5853d5..eefc442 100644 --- a/src/app/markets/__init__.py +++ b/src/app/markets/__init__.py @@ -2,13 +2,14 @@ from .base import BaseWrapper, ProductInfo, Price from .coinbase import CoinBaseWrapper from .binance import BinanceWrapper from .cryptocompare import CryptoCompareWrapper +from .yfinance import YFinanceWrapper from .binance_public import PublicBinanceAgent from app.utils.wrapper_handler import WrapperHandler from typing import List, Optional from agno.tools import Toolkit -__all__ = [ "MarketAPIs", "BinanceWrapper", "CoinBaseWrapper", "CryptoCompareWrapper", "PublicBinanceAgent" ] +__all__ = [ "MarketAPIs", "BinanceWrapper", "CoinBaseWrapper", "CryptoCompareWrapper", "YFinanceWrapper", "PublicBinanceAgent" ] class MarketAPIsTool(BaseWrapper, Toolkit): @@ -24,7 +25,7 @@ class MarketAPIsTool(BaseWrapper, Toolkit): def __init__(self, currency: str = "USD", enable_aggregation: bool = False): self.currency = currency - wrappers = [ BinanceWrapper, CoinBaseWrapper, CryptoCompareWrapper ] + wrappers = [ BinanceWrapper, CoinBaseWrapper, CryptoCompareWrapper, YFinanceWrapper ] self.wrappers: WrapperHandler[BaseWrapper] = WrapperHandler.build_wrappers(wrappers) # Inizializza l'aggregatore solo se richiesto (lazy initialization) diff --git a/src/app/markets/binance.py b/src/app/markets/binance.py index 6b6b6d3..3d3619e 100644 --- a/src/app/markets/binance.py +++ b/src/app/markets/binance.py @@ -23,10 +23,10 @@ class BinanceWrapper(BaseWrapper): def __init__(self, currency: str = "USDT"): api_key = os.getenv("BINANCE_API_KEY") - assert api_key is not None, "API key is required" + assert api_key is None, "API key is required" api_secret = os.getenv("BINANCE_API_SECRET") - assert api_secret is not None, "API secret is required" + assert api_secret is None, "API secret is required" self.currency = currency self.client = Client(api_key=api_key, api_secret=api_secret) diff --git a/src/app/markets/yfinance.py b/src/app/markets/yfinance.py new file mode 100644 index 0000000..f0e5d6d --- /dev/null +++ b/src/app/markets/yfinance.py @@ -0,0 +1,214 @@ +import json +from agno.tools.yfinance import YFinanceTools +from .base import BaseWrapper, ProductInfo, Price + + +def create_product_info(symbol: str, stock_data: dict) -> ProductInfo: + """ + Converte i dati di YFinanceTools in ProductInfo. + """ + product = ProductInfo() + + # ID univoco per yfinance + product.id = f"yfinance_{symbol}" + product.symbol = symbol + + # Estrai il prezzo corrente - gestisci diversi formati + if 'currentPrice' in stock_data: + product.price = float(stock_data['currentPrice']) + elif 'regularMarketPrice' in stock_data: + product.price = float(stock_data['regularMarketPrice']) + elif 'Current Stock Price' in stock_data: + # Formato: "254.63 USD" - estrai solo il numero + price_str = stock_data['Current Stock Price'].split()[0] + try: + product.price = float(price_str) + except ValueError: + product.price = 0.0 + else: + product.price = 0.0 + + # Volume 24h + if 'volume' in stock_data: + product.volume_24h = float(stock_data['volume']) + elif 'regularMarketVolume' in stock_data: + product.volume_24h = float(stock_data['regularMarketVolume']) + else: + product.volume_24h = 0.0 + + # Status basato sulla disponibilità dei dati + product.status = "trading" if product.price > 0 else "offline" + + # Valuta (default USD) + product.quote_currency = stock_data.get('currency', 'USD') or 'USD' + + return product + + +def create_price_from_history(hist_data: dict, timestamp: str) -> Price: + """ + Converte i dati storici di YFinanceTools in Price. + """ + price = Price() + + if timestamp in hist_data: + day_data = hist_data[timestamp] + price.high = float(day_data.get('High', 0.0)) + price.low = float(day_data.get('Low', 0.0)) + price.open = float(day_data.get('Open', 0.0)) + price.close = float(day_data.get('Close', 0.0)) + price.volume = float(day_data.get('Volume', 0.0)) + price.time = timestamp + + return price + + +class YFinanceWrapper(BaseWrapper): + """ + Wrapper per YFinanceTools che fornisce dati di mercato per azioni, ETF e criptovalute. + Implementa l'interfaccia BaseWrapper per compatibilità con il sistema esistente. + Usa YFinanceTools dalla libreria agno per coerenza con altri wrapper. + """ + + def __init__(self, currency: str = "USD"): + self.currency = currency + # Inizializza YFinanceTools - non richiede parametri specifici + self.tool = YFinanceTools() + + def _format_symbol(self, asset_id: str) -> str: + """ + Formatta il simbolo per yfinance. + Per crypto, aggiunge '-USD' se non presente. + """ + asset_id = asset_id.upper() + + # Se è già nel formato corretto (es: BTC-USD), usa così + if '-' in asset_id: + return asset_id + + # Per crypto singole (BTC, ETH), aggiungi -USD + if asset_id in ['BTC', 'ETH', 'ADA', 'SOL', 'DOT', 'LINK', 'UNI', 'AAVE']: + return f"{asset_id}-USD" + + # Per azioni, usa il simbolo così com'è + return asset_id + + def get_product(self, asset_id: str) -> ProductInfo: + """ + Recupera le informazioni di un singolo prodotto. + """ + symbol = self._format_symbol(asset_id) + + # Usa YFinanceTools per ottenere i dati + try: + # Ottieni le informazioni base dello stock + stock_info = self.tool.get_company_info(symbol) + + # Se il risultato è una stringa JSON, parsala + if isinstance(stock_info, str): + try: + stock_data = json.loads(stock_info) + except json.JSONDecodeError: + # Se non è JSON valido, prova a ottenere solo il prezzo + price_data_str = self.tool.get_current_stock_price(symbol) + if price_data_str and price_data_str.replace('.', '').replace('-', '').isdigit(): + price = float(price_data_str) + stock_data = {'currentPrice': price, 'currency': 'USD'} + else: + raise Exception("Dati non validi") + else: + stock_data = stock_info + + return create_product_info(symbol, stock_data) + + except Exception as e: + # Fallback: prova a ottenere solo il prezzo + try: + price_data_str = self.tool.get_current_stock_price(symbol) + if price_data_str and price_data_str.replace('.', '').replace('-', '').isdigit(): + price = float(price_data_str) + minimal_data = { + 'currentPrice': price, + 'currency': 'USD' + } + return create_product_info(symbol, minimal_data) + else: + raise Exception("Prezzo non disponibile") + except Exception: + # Se tutto fallisce, restituisci un prodotto vuoto + product = ProductInfo() + product.symbol = symbol + product.status = "offline" + return product + + def get_products(self, asset_ids: list[str]) -> list[ProductInfo]: + """ + Recupera le informazioni di multiple assets. + """ + products = [] + + for asset_id in asset_ids: + try: + product = self.get_product(asset_id) + products.append(product) + except Exception as e: + # Se un asset non è disponibile, continua con gli altri + continue + + return products + + def get_all_products(self) -> list[ProductInfo]: + """ + Recupera tutti i prodotti disponibili. + Restituisce una lista predefinita di asset popolari. + """ + # Lista di asset popolari (azioni, ETF, crypto) + popular_assets = [ + 'BTC', 'ETH', 'ADA', 'SOL', 'DOT', + 'AAPL', 'GOOGL', 'MSFT', 'TSLA', 'AMZN', + 'SPY', 'QQQ', 'VTI', 'GLD', 'VIX' + ] + + return self.get_products(popular_assets) + + def get_historical_prices(self, asset_id: str = "BTC", limit: int = 100) -> list[Price]: + """ + Recupera i dati storici di prezzo per un asset. + """ + symbol = self._format_symbol(asset_id) + + try: + # Determina il periodo appropriato in base al limite + if limit <= 7: + period = "1d" + interval = "15m" + elif limit <= 30: + period = "5d" + interval = "1h" + elif limit <= 90: + period = "1mo" + interval = "1d" + else: + period = "3mo" + interval = "1d" + + # Ottieni i dati storici + hist_data = self.tool.get_historical_stock_prices(symbol, period=period, interval=interval) + + if isinstance(hist_data, str): + hist_data = json.loads(hist_data) + + # Il formato dei dati è {timestamp: {Open: x, High: y, Low: z, Close: w, Volume: v}} + prices = [] + timestamps = sorted(hist_data.keys())[-limit:] # Prendi gli ultimi 'limit' timestamp + + for timestamp in timestamps: + price = create_price_from_history(hist_data, timestamp) + if price.close > 0: # Solo se ci sono dati validi + prices.append(price) + + return prices + + except Exception as e: + # Se fallisce, restituisci lista vuota + return [] \ No newline at end of file diff --git a/src/app/utils/aggregated_models.py b/src/app/utils/aggregated_models.py index 8eba8a5..ee9f3ef 100644 --- a/src/app/utils/aggregated_models.py +++ b/src/app/utils/aggregated_models.py @@ -77,6 +77,8 @@ class AggregatedProductInfo(ProductInfo): return "binance" elif "crypto" in product.id.lower() or "cc" in product.id.lower(): return "cryptocompare" + elif "yfinance" in product.id.lower() or "yf" in product.id.lower(): + return "yfinance" else: return "unknown" diff --git a/src/app/utils/market_aggregator.py b/src/app/utils/market_aggregator.py deleted file mode 100644 index 639bb9b..0000000 --- a/src/app/utils/market_aggregator.py +++ /dev/null @@ -1,71 +0,0 @@ -import statistics -from typing import Dict, Any - -class MarketAggregator: - """ - Aggrega dati di mercato da più provider e genera segnali e analisi avanzate. - """ - @staticmethod - def aggregate(symbol: str, sources: Dict[str, Dict[str, Any]]) -> Dict[str, Any]: - prices = [] - volumes = [] - price_map = {} - for provider, data in sources.items(): - price = data.get('price') - if price is not None: - prices.append(price) - price_map[provider] = price - volume = data.get('volume') - if volume is not None: - volumes.append(MarketAggregator._parse_volume(volume)) - - # Aggregated price (mean) - agg_price = statistics.mean(prices) if prices else None - # Spread analysis - spread = (max(prices) - min(prices)) / agg_price if prices and agg_price else 0 - # Confidence - stddev = statistics.stdev(prices) if len(prices) > 1 else 0 - confidence = max(0.5, 1 - (stddev / agg_price)) if agg_price else 0 - if spread < 0.005: - confidence += 0.1 - if len(prices) >= 3: - confidence += 0.05 - confidence = min(confidence, 1.0) - # Volume trend - total_volume = sum(volumes) if volumes else None - # Price divergence - max_deviation = (max(prices) - min(prices)) / agg_price if prices and agg_price else 0 - # Signals - signals = { - "spread_analysis": f"Low spread ({spread:.2%}) indicates healthy liquidity" if spread < 0.01 else f"Spread {spread:.2%} - check liquidity", - "volume_trend": f"Combined volume: {total_volume:.2f}" if total_volume else "Volume data not available", - "price_divergence": f"Max deviation: {max_deviation:.2%} - {'Normal range' if max_deviation < 0.01 else 'High divergence'}" - } - return { - "aggregated_data": { - f"{symbol}_USD": { - "price": round(agg_price, 2) if agg_price else None, - "confidence": round(confidence, 2), - "sources_count": len(prices) - } - }, - "individual_sources": price_map, - "market_signals": signals - } - @staticmethod - def _parse_volume(volume: Any) -> float: - # Supporta stringhe tipo "1.2M" o numeri - if isinstance(volume, (int, float)): - return float(volume) - if isinstance(volume, str): - v = volume.upper().replace(' ', '') - if v.endswith('M'): - return float(v[:-1]) * 1_000_000 - if v.endswith('K'): - return float(v[:-1]) * 1_000 - try: - return float(v) - except Exception as e: - print(f"Errore nel parsing del volume: {e}") - return 0.0 - return 0.0 diff --git a/src/app/utils/market_data_aggregator.py b/src/app/utils/market_data_aggregator.py index f72e91c..ea2d7c0 100644 --- a/src/app/utils/market_data_aggregator.py +++ b/src/app/utils/market_data_aggregator.py @@ -12,8 +12,8 @@ class MarketDataAggregator: def __init__(self, currency: str = "USD"): # Import lazy per evitare circular import - from app.markets import MarketAPIs - self._market_apis = MarketAPIs(currency) + from app.markets import MarketAPIsTool + self._market_apis = MarketAPIsTool(currency) self._aggregation_enabled = True def get_product(self, asset_id: str) -> ProductInfo: diff --git a/tests/api/test_yfinance.py b/tests/api/test_yfinance.py new file mode 100644 index 0000000..c0e9ba2 --- /dev/null +++ b/tests/api/test_yfinance.py @@ -0,0 +1,93 @@ +import os +import pytest +from app.markets import YFinanceWrapper + +@pytest.mark.market +@pytest.mark.api +class TestYFinance: + + def test_yfinance_init(self): + market = YFinanceWrapper() + assert market is not None + assert hasattr(market, 'currency') + assert market.currency == "USD" + assert hasattr(market, 'tool') + assert market.tool is not None + + def test_yfinance_get_product(self): + market = YFinanceWrapper() + product = market.get_product("AAPL") + assert product is not None + assert hasattr(product, 'symbol') + assert product.symbol == "AAPL" + assert hasattr(product, 'price') + assert product.price > 0 + assert hasattr(product, 'status') + assert product.status == "trading" + + def test_yfinance_get_crypto_product(self): + market = YFinanceWrapper() + product = market.get_product("BTC") + assert product is not None + assert hasattr(product, 'symbol') + # BTC verrà convertito in BTC-USD dal formattatore + assert product.symbol in ["BTC", "BTC-USD"] + assert hasattr(product, 'price') + assert product.price > 0 + + def test_yfinance_get_products(self): + market = YFinanceWrapper() + products = market.get_products(["AAPL", "GOOGL"]) + assert products is not None + assert isinstance(products, list) + assert len(products) == 2 + symbols = [p.symbol for p in products] + assert "AAPL" in symbols + assert "GOOGL" in symbols + for product in products: + assert hasattr(product, 'price') + assert product.price > 0 + + def test_yfinance_get_all_products(self): + market = YFinanceWrapper() + products = market.get_all_products() + assert products is not None + assert isinstance(products, list) + assert len(products) > 0 + # Dovrebbe contenere asset popolari + symbols = [p.symbol for p in products] + assert "AAPL" in symbols # Apple dovrebbe essere nella lista + for product in products: + assert hasattr(product, 'symbol') + assert hasattr(product, 'price') + + def test_yfinance_invalid_product(self): + market = YFinanceWrapper() + # Per YFinance, un prodotto invalido dovrebbe restituire un prodotto offline + product = market.get_product("INVALIDSYMBOL123") + assert product is not None + assert product.status == "offline" + + def test_yfinance_history(self): + market = YFinanceWrapper() + history = market.get_historical_prices("AAPL", limit=5) + assert history is not None + assert isinstance(history, list) + assert len(history) == 5 + for entry in history: + assert hasattr(entry, 'time') + assert hasattr(entry, 'close') + assert hasattr(entry, 'high') + assert entry.close > 0 + assert entry.high > 0 + + def test_yfinance_crypto_history(self): + market = YFinanceWrapper() + history = market.get_historical_prices("BTC", limit=3) + assert history is not None + assert isinstance(history, list) + assert len(history) == 3 + for entry in history: + assert hasattr(entry, 'time') + assert hasattr(entry, 'close') + assert entry.close > 0 \ No newline at end of file diff --git a/tests/test_market_data_aggregator.py b/tests/utils/test_market_data_aggregator.py similarity index 99% rename from tests/test_market_data_aggregator.py rename to tests/utils/test_market_data_aggregator.py index 604362c..af00e61 100644 --- a/tests/test_market_data_aggregator.py +++ b/tests/utils/test_market_data_aggregator.py @@ -4,7 +4,7 @@ from app.utils.market_data_aggregator import MarketDataAggregator from app.utils.aggregated_models import AggregatedProductInfo from app.markets.base import ProductInfo, Price - +@pytest.mark.aggregator class TestMarketDataAggregator: def test_initialization(self): diff --git a/uv.lock b/uv.lock index 2d7d6a1..389bf03 100644 --- a/uv.lock +++ b/uv.lock @@ -325,6 +325,27 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/ff/e8/77d17d00981cdd27cc493e81e1749a0b8bbfb843780dbd841e30d7f50743/cryptography-46.0.1-cp38-abi3-win_arm64.whl", hash = "sha256:efc9e51c3e595267ff84adf56e9b357db89ab2279d7e375ffcaf8f678606f3d9", size = 2923149, upload-time = "2025-09-17T00:10:13.236Z" }, ] +[[package]] +name = "curl-cffi" +version = "0.13.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "certifi" }, + { name = "cffi" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/4e/3d/f39ca1f8fdf14408888e7c25e15eed63eac5f47926e206fb93300d28378c/curl_cffi-0.13.0.tar.gz", hash = "sha256:62ecd90a382bd5023750e3606e0aa7cb1a3a8ba41c14270b8e5e149ebf72c5ca", size = 151303, upload-time = "2025-08-06T13:05:42.988Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/19/d1/acabfd460f1de26cad882e5ef344d9adde1507034528cb6f5698a2e6a2f1/curl_cffi-0.13.0-cp39-abi3-macosx_10_9_x86_64.whl", hash = "sha256:434cadbe8df2f08b2fc2c16dff2779fb40b984af99c06aa700af898e185bb9db", size = 5686337, upload-time = "2025-08-06T13:05:28.985Z" }, + { url = "https://files.pythonhosted.org/packages/2c/1c/cdb4fb2d16a0e9de068e0e5bc02094e105ce58a687ff30b4c6f88e25a057/curl_cffi-0.13.0-cp39-abi3-macosx_11_0_arm64.whl", hash = "sha256:59afa877a9ae09efa04646a7d068eeea48915a95d9add0a29854e7781679fcd7", size = 2994613, upload-time = "2025-08-06T13:05:31.027Z" }, + { url = "https://files.pythonhosted.org/packages/04/3e/fdf617c1ec18c3038b77065d484d7517bb30f8fb8847224eb1f601a4e8bc/curl_cffi-0.13.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d06ed389e45a7ca97b17c275dbedd3d6524560270e675c720e93a2018a766076", size = 7931353, upload-time = "2025-08-06T13:05:32.273Z" }, + { url = "https://files.pythonhosted.org/packages/3d/10/6f30c05d251cf03ddc2b9fd19880f3cab8c193255e733444a2df03b18944/curl_cffi-0.13.0-cp39-abi3-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:b4e0de45ab3b7a835c72bd53640c2347415111b43421b5c7a1a0b18deae2e541", size = 7486378, upload-time = "2025-08-06T13:05:33.672Z" }, + { url = "https://files.pythonhosted.org/packages/77/81/5bdb7dd0d669a817397b2e92193559bf66c3807f5848a48ad10cf02bf6c7/curl_cffi-0.13.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8eb4083371bbb94e9470d782de235fb5268bf43520de020c9e5e6be8f395443f", size = 8328585, upload-time = "2025-08-06T13:05:35.28Z" }, + { url = "https://files.pythonhosted.org/packages/ce/c1/df5c6b4cfad41c08442e0f727e449f4fb5a05f8aa564d1acac29062e9e8e/curl_cffi-0.13.0-cp39-abi3-musllinux_1_1_aarch64.whl", hash = "sha256:28911b526e8cd4aa0e5e38401bfe6887e8093907272f1f67ca22e6beb2933a51", size = 8739831, upload-time = "2025-08-06T13:05:37.078Z" }, + { url = "https://files.pythonhosted.org/packages/1a/91/6dd1910a212f2e8eafe57877bcf97748eb24849e1511a266687546066b8a/curl_cffi-0.13.0-cp39-abi3-musllinux_1_1_x86_64.whl", hash = "sha256:6d433ffcb455ab01dd0d7bde47109083aa38b59863aa183d29c668ae4c96bf8e", size = 8711908, upload-time = "2025-08-06T13:05:38.741Z" }, + { url = "https://files.pythonhosted.org/packages/6d/e4/15a253f9b4bf8d008c31e176c162d2704a7e0c5e24d35942f759df107b68/curl_cffi-0.13.0-cp39-abi3-win_amd64.whl", hash = "sha256:66a6b75ce971de9af64f1b6812e275f60b88880577bac47ef1fa19694fa21cd3", size = 1614510, upload-time = "2025-08-06T13:05:40.451Z" }, + { url = "https://files.pythonhosted.org/packages/f9/0f/9c5275f17ad6ff5be70edb8e0120fdc184a658c9577ca426d4230f654beb/curl_cffi-0.13.0-cp39-abi3-win_arm64.whl", hash = "sha256:d438a3b45244e874794bc4081dc1e356d2bb926dcc7021e5a8fef2e2105ef1d8", size = 1365753, upload-time = "2025-08-06T13:05:41.879Z" }, +] + [[package]] name = "dateparser" version = "1.2.2" @@ -428,6 +449,16 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/42/14/42b2651a2f46b022ccd948bca9f2d5af0fd8929c4eec235b8d6d844fbe67/filelock-3.19.1-py3-none-any.whl", hash = "sha256:d38e30481def20772f5baf097c122c3babc4fcdb7e14e57049eb9d88c6dc017d", size = 15988, upload-time = "2025-08-14T16:56:01.633Z" }, ] +[[package]] +name = "frozendict" +version = "2.4.6" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/bb/59/19eb300ba28e7547538bdf603f1c6c34793240a90e1a7b61b65d8517e35e/frozendict-2.4.6.tar.gz", hash = "sha256:df7cd16470fbd26fc4969a208efadc46319334eb97def1ddf48919b351192b8e", size = 316416, upload-time = "2024-10-13T12:15:32.449Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/04/13/d9839089b900fa7b479cce495d62110cddc4bd5630a04d8469916c0e79c5/frozendict-2.4.6-py311-none-any.whl", hash = "sha256:d065db6a44db2e2375c23eac816f1a022feb2fa98cbb50df44a9e83700accbea", size = 16148, upload-time = "2024-10-13T12:15:26.839Z" }, + { url = "https://files.pythonhosted.org/packages/ba/d0/d482c39cee2ab2978a892558cf130681d4574ea208e162da8958b31e9250/frozendict-2.4.6-py312-none-any.whl", hash = "sha256:49344abe90fb75f0f9fdefe6d4ef6d4894e640fadab71f11009d52ad97f370b9", size = 16146, upload-time = "2024-10-13T12:15:28.16Z" }, +] + [[package]] name = "frozenlist" version = "1.7.0" @@ -836,6 +867,12 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/fd/69/b547032297c7e63ba2af494edba695d781af8a0c6e89e4d06cf848b21d80/multidict-6.6.4-py3-none-any.whl", hash = "sha256:27d8f8e125c07cb954e54d75d04905a9bba8a439c1d84aca94949d4d03d8601c", size = 12313, upload-time = "2025-08-11T12:08:46.891Z" }, ] +[[package]] +name = "multitasking" +version = "0.0.12" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/17/0d/74f0293dfd7dcc3837746d0138cbedd60b31701ecc75caec7d3f281feba0/multitasking-0.0.12.tar.gz", hash = "sha256:2fba2fa8ed8c4b85e227c5dd7dc41c7d658de3b6f247927316175a57349b84d1", size = 19984, upload-time = "2025-07-20T21:27:51.636Z" } + [[package]] name = "newsapi-python" version = "0.2.7" @@ -933,6 +970,12 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/28/30/8114832daff7489f179971dbc1d854109b7f4365a546e3ea75b6516cea95/pandas-2.3.2-cp312-cp312-win_amd64.whl", hash = "sha256:8c13b81a9347eb8c7548f53fd9a4f08d4dfe996836543f805c987bafa03317ae", size = 10983326, upload-time = "2025-08-21T10:27:31.901Z" }, ] +[[package]] +name = "peewee" +version = "3.18.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/04/89/76f6f1b744c8608e0d416b588b9d63c2a500ff800065ae610f7c80f532d6/peewee-3.18.2.tar.gz", hash = "sha256:77a54263eb61aff2ea72f63d2eeb91b140c25c1884148e28e4c0f7c4f64996a0", size = 949220, upload-time = "2025-07-08T12:52:03.941Z" } + [[package]] name = "pillow" version = "11.3.0" @@ -952,6 +995,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/16/8f/b13447d1bf0b1f7467ce7d86f6e6edf66c0ad7cf44cf5c87a37f9bed9936/pillow-11.3.0-cp312-cp312-win_arm64.whl", hash = "sha256:2aceea54f957dd4448264f9bf40875da0415c83eb85f55069d89c0ed436e3542", size = 2423067, upload-time = "2025-07-01T09:14:33.709Z" }, ] +[[package]] +name = "platformdirs" +version = "4.4.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/23/e8/21db9c9987b0e728855bd57bff6984f67952bea55d6f75e055c46b5383e8/platformdirs-4.4.0.tar.gz", hash = "sha256:ca753cf4d81dc309bc67b0ea38fd15dc97bc30ce419a7f58d13eb3bf14c4febf", size = 21634, upload-time = "2025-08-26T14:32:04.268Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/40/4b/2028861e724d3bd36227adfa20d3fd24c3fc6d52032f4a93c133be5d17ce/platformdirs-4.4.0-py3-none-any.whl", hash = "sha256:abd01743f24e5287cd7a5db3752faf1a2d65353f38ec26d98e25a6db65958c85", size = 18654, upload-time = "2025-08-26T14:32:02.735Z" }, +] + [[package]] name = "pluggy" version = "1.6.0" @@ -1028,6 +1080,20 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/cc/35/cc0aaecf278bb4575b8555f2b137de5ab821595ddae9da9d3cd1da4072c7/propcache-0.3.2-py3-none-any.whl", hash = "sha256:98f1ec44fb675f5052cccc8e609c46ed23a35a1cfd18545ad4e29002d858a43f", size = 12663, upload-time = "2025-06-09T22:56:04.484Z" }, ] +[[package]] +name = "protobuf" +version = "6.32.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/fa/a4/cc17347aa2897568beece2e674674359f911d6fe21b0b8d6268cd42727ac/protobuf-6.32.1.tar.gz", hash = "sha256:ee2469e4a021474ab9baafea6cd070e5bf27c7d29433504ddea1a4ee5850f68d", size = 440635, upload-time = "2025-09-11T21:38:42.935Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/c0/98/645183ea03ab3995d29086b8bf4f7562ebd3d10c9a4b14ee3f20d47cfe50/protobuf-6.32.1-cp310-abi3-win32.whl", hash = "sha256:a8a32a84bc9f2aad712041b8b366190f71dde248926da517bde9e832e4412085", size = 424411, upload-time = "2025-09-11T21:38:27.427Z" }, + { url = "https://files.pythonhosted.org/packages/8c/f3/6f58f841f6ebafe076cebeae33fc336e900619d34b1c93e4b5c97a81fdfa/protobuf-6.32.1-cp310-abi3-win_amd64.whl", hash = "sha256:b00a7d8c25fa471f16bc8153d0e53d6c9e827f0953f3c09aaa4331c718cae5e1", size = 435738, upload-time = "2025-09-11T21:38:30.959Z" }, + { url = "https://files.pythonhosted.org/packages/10/56/a8a3f4e7190837139e68c7002ec749190a163af3e330f65d90309145a210/protobuf-6.32.1-cp39-abi3-macosx_10_9_universal2.whl", hash = "sha256:d8c7e6eb619ffdf105ee4ab76af5a68b60a9d0f66da3ea12d1640e6d8dab7281", size = 426454, upload-time = "2025-09-11T21:38:34.076Z" }, + { url = "https://files.pythonhosted.org/packages/3f/be/8dd0a927c559b37d7a6c8ab79034fd167dcc1f851595f2e641ad62be8643/protobuf-6.32.1-cp39-abi3-manylinux2014_aarch64.whl", hash = "sha256:2f5b80a49e1eb7b86d85fcd23fe92df154b9730a725c3b38c4e43b9d77018bf4", size = 322874, upload-time = "2025-09-11T21:38:35.509Z" }, + { url = "https://files.pythonhosted.org/packages/5c/f6/88d77011b605ef979aace37b7703e4eefad066f7e84d935e5a696515c2dd/protobuf-6.32.1-cp39-abi3-manylinux2014_x86_64.whl", hash = "sha256:b1864818300c297265c83a4982fd3169f97122c299f56a56e2445c3698d34710", size = 322013, upload-time = "2025-09-11T21:38:37.017Z" }, + { url = "https://files.pythonhosted.org/packages/97/b7/15cc7d93443d6c6a84626ae3258a91f4c6ac8c0edd5df35ea7658f71b79c/protobuf-6.32.1-py3-none-any.whl", hash = "sha256:2601b779fc7d32a866c6b4404f9d42a3f67c5b9f3f15b4db3cccabe06b95c346", size = 169289, upload-time = "2025-09-11T21:38:41.234Z" }, +] + [[package]] name = "pyasn1" version = "0.6.1" @@ -1545,6 +1611,7 @@ dependencies = [ { name = "praw" }, { name = "pytest" }, { name = "python-binance" }, + { name = "yfinance" }, ] [package.metadata] @@ -1561,6 +1628,7 @@ requires-dist = [ { name = "praw" }, { name = "pytest" }, { name = "python-binance" }, + { name = "yfinance" }, ] [[package]] @@ -1644,3 +1712,26 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/eb/83/5d9092950565481b413b31a23e75dd3418ff0a277d6e0abf3729d4d1ce25/yarl-1.20.1-cp312-cp312-win_amd64.whl", hash = "sha256:48ea7d7f9be0487339828a4de0360d7ce0efc06524a48e1810f945c45b813698", size = 86710, upload-time = "2025-06-10T00:44:16.716Z" }, { url = "https://files.pythonhosted.org/packages/b4/2d/2345fce04cfd4bee161bf1e7d9cdc702e3e16109021035dbb24db654a622/yarl-1.20.1-py3-none-any.whl", hash = "sha256:83b8eb083fe4683c6115795d9fc1cfaf2cbbefb19b3a1cb68f6527460f483a77", size = 46542, upload-time = "2025-06-10T00:46:07.521Z" }, ] + +[[package]] +name = "yfinance" +version = "0.2.66" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "beautifulsoup4" }, + { name = "curl-cffi" }, + { name = "frozendict" }, + { name = "multitasking" }, + { name = "numpy" }, + { name = "pandas" }, + { name = "peewee" }, + { name = "platformdirs" }, + { name = "protobuf" }, + { name = "pytz" }, + { name = "requests" }, + { name = "websockets" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/59/73/50450b9906c5137d2d02fde6f7360865366c72baea1f8d0550cc990829ce/yfinance-0.2.66.tar.gz", hash = "sha256:fae354cc1649109444b2c84194724afcc52c2a7799551ce44c739424ded6af9c", size = 132820, upload-time = "2025-09-17T11:22:35.422Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/16/bf/7c0c89ff8ba53592b9cb5157f70e90d8bbb04d60094fc4f10035e158b981/yfinance-0.2.66-py2.py3-none-any.whl", hash = "sha256:511a1a40a687f277aae3a02543009a8aeaa292fce5509671f58915078aebb5c7", size = 123427, upload-time = "2025-09-17T11:22:33.972Z" }, +]