File size: 2,373 Bytes
ae3568e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
import requests
import os
from typing import List, Dict, Any, Optional
import streamlit as st

# Use environment variable or Streamlit secrets for API key
@st.cache_data(ttl=3600)  # Cache results for 1 hour
def get_top_stories(category: str = "home") -> List[Dict[str, Any]]:
    """
    Fetch top stories from the NYT API for a given category.
    
    Args:
        category: News category (e.g., "home", "world", "politics")
        
    Returns:
        List of article dictionaries
    """
    # Try to get API key from environment or secrets
    api_key = os.environ.get("NYT_API_KEY")
    
    # If not found in environment, check streamlit secrets
    if not api_key:
        try:
            api_key = st.secrets["NYT_API_KEY"]
        except Exception:
            st.error("NYT API key not found. Please set the NYT_API_KEY environment variable or in Streamlit secrets.")
            return []
    
    # API endpoint
    url = f"https://api.nytimes.com/svc/topstories/v2/{category}.json"
    
    # Make the request
    try:
        response = requests.get(url, params={"api-key": api_key})
        response.raise_for_status()  # Raise exception for non-200 responses
        
        # Parse JSON response
        data = response.json()
        
        # Filter articles that have an image
        articles = [
            article for article in data.get("results", [])
            if article.get("multimedia") and len(article.get("multimedia", [])) > 0
        ]
        
        # Process articles to extract needed fields
        processed_articles = []
        for article in articles[:10]:  # Limit to 10 articles
            processed_article = {
                "title": article.get("title", ""),
                "abstract": article.get("abstract", ""),
                "url": article.get("url", ""),
                "image_url": next((media.get("url") for media in article.get("multimedia", []) 
                                   if media.get("format") == "superJumbo"), None),
                "section": article.get("section", ""),
                "published_date": article.get("published_date", "")
            }
            processed_articles.append(processed_article)
            
        return processed_articles
    
    except requests.exceptions.RequestException as e:
        st.error(f"Error fetching NYT articles: {e}")
        return []