9 enhancement con financialdatasettool e yfinance #11

Merged
Berack96 merged 26 commits from 9-enhancement-con-financialdatasettool-e-yfinance into main 2025-10-01 16:19:21 +02:00
3 changed files with 23 additions and 6 deletions
Showing only changes of commit fc4753a245 - Show all commits

16
demos/news_api.py Normal file
View File

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

View File

@@ -17,16 +17,17 @@ class NewsAPI:
assert api_key is not None, "NEWS_API_KEY environment variable not set" assert api_key is not None, "NEWS_API_KEY environment variable not set"
self.client = newsapi.NewsApiClient(api_key=api_key) self.client = newsapi.NewsApiClient(api_key=api_key)
self.category = "business" self.category = "business" # Cryptocurrency is under business
self.language = "en" self.language = "en" # TODO Only English articles for now?
self.page_size = 100 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.page_size, total) page_size = min(self.max_page_size, total)
pages = (total // page_size) + (1 if total % page_size > 0 else 0) pages = (total // page_size) + (1 if total % page_size > 0 else 0)
articles = [] articles = []
for page in range(1, pages + 1): 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", [])] results = [result_to_article(article) for article in headlines.get("articles", [])]
articles.extend(results) articles.extend(results)
return articles return articles

View File

@@ -10,7 +10,7 @@ class TestNewsAPI:
news_api = NewsAPI() news_api = NewsAPI()
articles = news_api.get_top_headlines(query="crypto", total=2) articles = news_api.get_top_headlines(query="crypto", total=2)
assert isinstance(articles, list) 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: for article in articles:
assert hasattr(article, 'source') assert hasattr(article, 'source')
assert hasattr(article, 'time') assert hasattr(article, 'time')