Add CryptoPanic API integration and related tests; update .env.example and test configurations

This commit is contained in:
2025-09-30 01:54:26 +02:00
parent c17a948ae0
commit 6aa9d4969f
7 changed files with 143 additions and 20 deletions

View File

@@ -1,4 +1,5 @@
from .news_api import NewsApiWrapper
from .gnews_api import GnewsWrapper
from .cryptopanic_api import CryptoPanicWrapper
__all__ = ["NewsApiWrapper", "GnewsWrapper"]
__all__ = ["NewsApiWrapper", "GnewsWrapper", "CryptoPanicWrapper"]

View File

@@ -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