File size: 2,088 Bytes
c6d67ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import requests
from bs4 import BeautifulSoup
from langchain_core.tools import tool

@tool
def get_recent_news(ticker: str) -> str:
    """
    Fetches the most recent news headlines for a given stock ticker.
    CRITICAL INSTRUCTIONS:
    1. 'ticker': Must be the official uppercase ticker symbol (e.g., AAPL). DO NOT pass the full company name.
    2. Use this tool to gauge current market sentiment, breaking news, and short-term catalysts.
    """
    try:
        ticker = ticker.upper()
        print(f"\n[System: Fetching latest news for {ticker} via Yahoo Finance RSS...]")
        
        # Hit the official Yahoo Finance RSS endpoint
        url = f"https://feeds.finance.yahoo.com/rss/2.0/headline?s={ticker}&region=US&lang=en-US"
        
        # A standard web browser User-Agent so Yahoo doesn't block us
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        
        # Parse the XML feed using BeautifulSoup
        soup = BeautifulSoup(response.content, features="xml")
        items = soup.find_all("item")
        
        if not items:
            return f"No recent news found for {ticker}."
            
        summary = f"Recent News Headlines for {ticker}:\n\n"
        
        # Grab the top 5 most recent articles
        for i, item in enumerate(items[:10]): 
            title = item.title.text if item.title else "No Title"
            # RSS provides nicely formatted publication dates
            pub_date = item.pubDate.text if item.pubDate else "Recent" 
            
            summary += f"{i+1}. [{pub_date}] {title}\n"
            
        return summary
        
    except Exception as e:
        return f"Error fetching news for {ticker}: {str(e)}"

# Quick test block
if __name__ == "__main__":
    test_ticker = "NVDA"
    print("Testing News Pipeline...")
    print(get_recent_news.invoke({"ticker": test_ticker}))