gotti_signal_gen / src /news_scraper /adapters /yahoo_finance_adapter.py
Papaflessas's picture
Deploy Signal Generator app
3fe0726
from datetime import datetime
from .base_adapter import BaseAdapter
from news_scraper.models.article import Article
import yfinance as yf
from news_scraper.services.sentiment_analysis_gemini import analyze_sentiment
from news_scraper.helpers.news_db_logger import NewsDBLogger
class YahooFinanceAdapter(BaseAdapter):
def __init__(self):
super().__init__()
self.db_logger = NewsDBLogger()
def fetch_articles(self):
# Logic to scrape articles from Yahoo Finance
return(yf.Search('AAPL',news_count=1).news)
def parse_article(self, raw_article):
# Logic to parse a raw article into an Article object
publish_time = datetime.fromtimestamp(raw_article['providerPublishTime'])
article= Article(url=raw_article['link'], title=raw_article['title'],description=None, score=None, ticker=raw_article['relatedTickers'],time=publish_time)
print(article.__repr__())
return article
def analyze_article(self, article):
# Use Gemini sentiment analysis on the article
sentiment_result = analyze_sentiment(url=article.url)
# Log the article and its sentiment to database
self.db_logger.log_news_with_sentiment(article, sentiment_result)
# Continue with the original analysis
return super().analyze_article(article)
"""
Format of the raw article object returned by Yahoo Finance API:
[{'uuid': '637daa50-f230-3ddd-a251-1b9840c38c10',
'title': 'Apple Inc. (AAPL): Alibaba Partnership to Bring AI to iPhones in China',
'publisher': 'Insider Monkey',
'link': 'https://finance.yahoo.com/news/apple-inc-aapl-alibaba-partnership-044613466.html',
'providerPublishTime': 1739421973,
'type': 'STORY',
'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/fTPNobtT0bdcSxj2xXAamw--~B/aD04MTY7dz0xNDU2O2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/insidermonkey.com/bd47dc2e86b4a3086f845d200d9daf1f', 'width': 1456, 'height': 816, 'tag': 'original'}, {'url': 'https://s.yimg.com/uu/api/res/1.2/nIyTyNRglKMNfFFMGli.7A--~B/Zmk9ZmlsbDtoPTE0MDtweW9mZj0wO3c9MTQwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/insidermonkey.com/bd47dc2e86b4a3086f845d200d9daf1f', 'width': 140, 'height': 140, 'tag': '140x140'}]},
'relatedTickers': ['AAPL']}]
"""