Create nyt_api.py
Browse files- utils/nyt_api.py +64 -0
utils/nyt_api.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
from typing import List, Dict, Any, Optional
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
# Use environment variable or Streamlit secrets for API key
|
| 7 |
+
@st.cache_data(ttl=3600) # Cache results for 1 hour
|
| 8 |
+
def get_top_stories(category: str = "home") -> List[Dict[str, Any]]:
|
| 9 |
+
"""
|
| 10 |
+
Fetch top stories from the NYT API for a given category.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
category: News category (e.g., "home", "world", "politics")
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
List of article dictionaries
|
| 17 |
+
"""
|
| 18 |
+
# Try to get API key from environment or secrets
|
| 19 |
+
api_key = os.environ.get("NYT_API_KEY")
|
| 20 |
+
|
| 21 |
+
# If not found in environment, check streamlit secrets
|
| 22 |
+
if not api_key:
|
| 23 |
+
try:
|
| 24 |
+
api_key = st.secrets["NYT_API_KEY"]
|
| 25 |
+
except Exception:
|
| 26 |
+
st.error("NYT API key not found. Please set the NYT_API_KEY environment variable or in Streamlit secrets.")
|
| 27 |
+
return []
|
| 28 |
+
|
| 29 |
+
# API endpoint
|
| 30 |
+
url = f"https://api.nytimes.com/svc/topstories/v2/{category}.json"
|
| 31 |
+
|
| 32 |
+
# Make the request
|
| 33 |
+
try:
|
| 34 |
+
response = requests.get(url, params={"api-key": api_key})
|
| 35 |
+
response.raise_for_status() # Raise exception for non-200 responses
|
| 36 |
+
|
| 37 |
+
# Parse JSON response
|
| 38 |
+
data = response.json()
|
| 39 |
+
|
| 40 |
+
# Filter articles that have an image
|
| 41 |
+
articles = [
|
| 42 |
+
article for article in data.get("results", [])
|
| 43 |
+
if article.get("multimedia") and len(article.get("multimedia", [])) > 0
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
# Process articles to extract needed fields
|
| 47 |
+
processed_articles = []
|
| 48 |
+
for article in articles[:10]: # Limit to 10 articles
|
| 49 |
+
processed_article = {
|
| 50 |
+
"title": article.get("title", ""),
|
| 51 |
+
"abstract": article.get("abstract", ""),
|
| 52 |
+
"url": article.get("url", ""),
|
| 53 |
+
"image_url": next((media.get("url") for media in article.get("multimedia", [])
|
| 54 |
+
if media.get("format") == "superJumbo"), None),
|
| 55 |
+
"section": article.get("section", ""),
|
| 56 |
+
"published_date": article.get("published_date", "")
|
| 57 |
+
}
|
| 58 |
+
processed_articles.append(processed_article)
|
| 59 |
+
|
| 60 |
+
return processed_articles
|
| 61 |
+
|
| 62 |
+
except requests.exceptions.RequestException as e:
|
| 63 |
+
st.error(f"Error fetching NYT articles: {e}")
|
| 64 |
+
return []
|