Spaces:
Sleeping
Sleeping
Create utils/news_utils.py
Browse files- utils/news_utils.py +48 -0
utils/news_utils.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import feedparser
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
class NewsUtils:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.headers = {
|
| 8 |
+
"User-Agent": "StockResearchMVP/1.0"
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
def get_yahoo_news(self, ticker):
|
| 12 |
+
"""Get news from Yahoo Finance RSS"""
|
| 13 |
+
try:
|
| 14 |
+
url = f"https://feeds.finance.yahoo.com/rss/2.0/headline?s={ticker}®ion=US&lang=en-US"
|
| 15 |
+
feed = feedparser.parse(url)
|
| 16 |
+
|
| 17 |
+
if not feed.entries:
|
| 18 |
+
return f"β No news found for {ticker}"
|
| 19 |
+
|
| 20 |
+
result = f"π° **Latest News for {ticker}**\n\n"
|
| 21 |
+
|
| 22 |
+
for i, entry in enumerate(feed.entries[:5]): # Latest 5 news
|
| 23 |
+
title = entry.title
|
| 24 |
+
link = entry.link
|
| 25 |
+
pub_date = entry.published if hasattr(entry, 'published') else "Unknown date"
|
| 26 |
+
|
| 27 |
+
result += f"{i+1}. **{title}**\n"
|
| 28 |
+
result += f" π
{pub_date}\n"
|
| 29 |
+
result += f" π [Read More]({link})\n\n"
|
| 30 |
+
|
| 31 |
+
return result
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"β Error fetching news: {str(e)}"
|
| 35 |
+
|
| 36 |
+
def get_finviz_news(self, ticker):
|
| 37 |
+
"""Get news headlines from Finviz RSS"""
|
| 38 |
+
try:
|
| 39 |
+
url = f"https://finviz.com/quote.ashx?t={ticker}"
|
| 40 |
+
# This is a simple approach - in production you'd want proper parsing
|
| 41 |
+
result = f"π **Market News for {ticker}**\n\n"
|
| 42 |
+
result += "β’ Check [Finviz News](https://finviz.com/news.ashx) for latest updates\n"
|
| 43 |
+
result += f"β’ Ticker-specific: [Finviz {ticker}](https://finviz.com/quote.ashx?t={ticker})\n\n"
|
| 44 |
+
|
| 45 |
+
return result
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"β Error fetching market news: {str(e)}"
|