generated from axioma-ai-labs/python-template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
105 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Python: AA Core", | ||
"type": "python", | ||
"request": "launch", | ||
"module": "src.main", | ||
"console": "integratedTerminal", | ||
"justMyCode": true, | ||
"python": "${command:python.interpreterPath}", | ||
"env": { | ||
"PYTHONPATH": "${workspaceFolder}" | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,68 @@ | ||
from datetime import datetime, timedelta | ||
from typing import Dict | ||
|
||
import httpx | ||
from loguru import logger | ||
|
||
from src.core.config import settings | ||
from src.tools.search_with_perplexity import search_with_perplexity | ||
from src.core.exceptions import CoinstatsError | ||
|
||
#: Coinstatst API | ||
COINSTATS_BASE_URL = "https://openapiv1.coinstats.app" | ||
|
||
#: Coinstats API headers | ||
COINSTATS_HEADERS = { | ||
"accept": "application/json", | ||
"X-API-KEY": settings.COINSTATS_API_KEY, | ||
} | ||
|
||
|
||
async def get_coinstats_news() -> Dict[str, str]: | ||
"""get news from coinstats api""" | ||
logger.debug("RETRIEVING NEWS") | ||
try: | ||
now = datetime.now() | ||
yesterday = now - timedelta(days=1) | ||
url = ( | ||
f"{COINSTATS_BASE_URL}/news?limit=30&" | ||
f"from={yesterday.strftime('%Y-%m-%d')}&" | ||
f"to={now.strftime('%Y-%m-%d')}" | ||
) | ||
|
||
async with httpx.AsyncClient() as client: | ||
response = await client.get(url, headers=COINSTATS_HEADERS) | ||
response.raise_for_status() | ||
data = response.json() | ||
|
||
logger.debug(f"COINSTATS NEWS | SUCCESSFULLY RETRIEVED {len(data['result'])} ARTICLES") | ||
return data | ||
except Exception as e: | ||
logger.error(f"ERROR RETRIEVING NEWS: {str(e)}") | ||
raise CoinstatsError("News data currently unavailable") | ||
|
||
|
||
async def fetch_signal() -> dict: | ||
""" | ||
Fetch a crypto signal from the coingecko API endpoint. | ||
Fetch a crypto signal from the Coinstats API. | ||
Returns: | ||
dict: Parsed JSON response containing actionable crypto news or updates. | ||
Format: {"status": str, "news": str} | ||
- status: "new_data", "no_data", or "error" | ||
- news: title of the latest news article if status is "new_data" | ||
""" | ||
api_url = "https://api.coingecko.com/api/v3/news" | ||
try: | ||
async with httpx.AsyncClient() as client: | ||
response = await client.get(api_url) | ||
if response.status_code == 200: | ||
data = response.json() | ||
logger.debug(f"Signal fetched: {data}") | ||
return {"status": "new_data", "news": data[0]["title"]} | ||
else: | ||
logger.error(f"Failed to fetch signal. Status code: {response.status_code}") | ||
return {"status": "no_data"} | ||
except Exception as e: | ||
logger.error(f"Error fetching signal from API: {e}") | ||
data = await get_coinstats_news() | ||
if data and data.get("result") and len(data["result"]) > 0: | ||
latest_news = data["result"][0] | ||
logger.debug(f"Signal fetched: {latest_news}") | ||
signal = latest_news.get("title", None) # type: ignore | ||
if not signal: | ||
return {"status": "error"} | ||
return {"status": "new_data", "news": signal} | ||
else: | ||
logger.error("No news data available in the response") | ||
return {"status": "no_data"} | ||
except CoinstatsError as e: | ||
logger.error(f"Error fetching signal from Coinstats: {e}") | ||
return {"status": "error"} | ||
if data: | ||
logger.info(f"Signal fetched: {data}") | ||
return {"status": "new_data", "news": data} | ||
else: | ||
logger.info(f"No data available: {data}") | ||
return {"status": "no_data"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters