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
This commit is contained in:
3
src/app/news/__init__.py
Normal file
3
src/app/news/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .news_api import NewsAPI
|
||||
|
||||
__all__ = ["NewsAPI"]
|
||||
8
src/app/news/base.py
Normal file
8
src/app/news/base.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class Article(BaseModel):
|
||||
source: str = ""
|
||||
time: str = ""
|
||||
title: str = ""
|
||||
description: str = ""
|
||||
|
||||
34
src/app/news/news_api.py
Normal file
34
src/app/news/news_api.py
Normal file
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user