-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnews_api.py
More file actions
190 lines (159 loc) · 7.12 KB
/
news_api.py
File metadata and controls
190 lines (159 loc) · 7.12 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import requests
import streamlit as st
from datetime import datetime, timedelta
import os
from config import NEWS_API_KEY
def get_news_client():
"""Check if News API key is configured"""
api_key = os.environ.get("NEWS_API_KEY")
if not api_key or api_key == "YOUR_NEWS_API_KEY":
st.warning("News API key not configured. News features will be unavailable.", icon="⚠️")
return None
return api_key
def extract_keywords(text, max_keywords=5):
"""Extract important keywords from text for news search"""
# Simple implementation: split by spaces and get most common words
# In a real app, you'd use NLP techniques for better keyword extraction
if not text:
return []
# Remove common words (simple stopwords)
stopwords = ["the", "a", "an", "and", "in", "on", "at", "to", "for", "of", "with", "by", "as", "is", "are", "was", "were"]
words = text.lower().split()
words = [word for word in words if word not in stopwords and len(word) > 3]
# Count word frequency
word_freq = {}
for word in words:
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1
# Sort by frequency
sorted_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)
# Return top keywords
return [word for word, _ in sorted_words[:max_keywords]]
def fetch_related_news(summary, api_key, max_results=6):
"""Fetch news related to Meetings and News summary content"""
if not api_key or not summary:
return []
try:
# Extract keywords from summary
keywords = extract_keywords(summary)
query = " OR ".join(keywords)
if not query:
# Default to technology news if no keywords extracted
return fetch_latest_news(api_key, category="technology", max_results=max_results)
# Calculate date one week ago for freshness
week_ago = (datetime.now() - timedelta(days=7)).strftime('%Y-%m-%d')
# Make request to News API
url = f"https://newsapi.org/v2/everything"
params = {
"q": query,
"apiKey": api_key,
"language": "en",
"sortBy": "relevancy",
"from": week_ago,
"pageSize": max_results
}
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
articles = data.get('articles', [])
# Format the news articles
formatted_articles = []
for article in articles:
formatted_articles.append({
"title": article.get('title', 'No title'),
"source": article.get('source', {}).get('name', 'Unknown source'),
"url": article.get('url', '#'),
"publishedAt": article.get('publishedAt', ''),
"description": article.get('description', 'No description available'),
"urlToImage": article.get('urlToImage') # Add image URL
})
return formatted_articles
else:
st.error(f"Error fetching news: {response.status_code}")
return []
except Exception as e:
st.error(f"Error fetching related news: {e}")
return []
def fetch_latest_news(api_key, category="general", max_results=6):
"""Fetch the latest news in a specific category"""
if not api_key:
return []
try:
# Make request to News API
url = f"https://newsapi.org/v2/top-headlines"
params = {
"category": category,
"apiKey": api_key,
"language": "en",
"pageSize": max_results
}
response = requests.get(url, params=params)
if response.status_code == 200:
data = response.json()
articles = data.get('articles', [])
# Format the news articles
formatted_articles = []
for article in articles:
formatted_articles.append({
"title": article.get('title', 'No title'),
"source": article.get('source', {}).get('name', 'Unknown source'),
"url": article.get('url', '#'),
"publishedAt": article.get('publishedAt', ''),
"description": article.get('description', 'No description available'),
"urlToImage": article.get('urlToImage') # Add image URL
})
return formatted_articles
else:
st.error(f"Error fetching news: {response.status_code}")
return []
except Exception as e:
st.error(f"Error fetching latest news: {e}")
return []
def render_news_column(articles):
"""Render news articles in a Streamlit container"""
if not articles:
st.info("No news articles available")
return
# Add custom CSS to ensure text visibility
st.markdown("""
<style>
.news-title { color: #1E1E1E; font-weight: bold; font-size: 1.2rem; margin-bottom: 0.5rem; }
.news-source { color: #5A5A5A; font-size: 0.8rem; }
.news-description { color: #333333; margin: 0.7rem 0; }
.news-link { color: #1E88E5; text-decoration: underline; }
</style>
""", unsafe_allow_html=True)
# Create a grid layout for news articles
cols = st.columns(min(3, len(articles)))
for idx, article in enumerate(articles):
col_idx = idx % len(cols)
with cols[col_idx]:
with st.container():
# Use HTML/CSS to control text colors
st.markdown(f"""
<div class="news-title">{article['title']}</div>
<div class="news-source">{article['source']} • {format_date(article['publishedAt'])}</div>
<div class="news-description">{article['description']}</div>
<a href="{article['url']}" target="_blank" class="news-link">Read more</a>
""", unsafe_allow_html=True)
st.divider()
# Display any remaining articles in a list format if more than fits in the grid
if len(articles) > 3:
st.markdown("### More Related Articles")
for idx, article in enumerate(articles[3:], 3):
with st.expander(f"{article['title']} ({article['source']})"):
# Use HTML/CSS to control text colors in expanders too
st.markdown(f"""
<div class="news-source">{format_date(article['publishedAt'])}</div>
<div class="news-description">{article['description']}</div>
<a href="{article['url']}" target="_blank" class="news-link">Read more</a>
""", unsafe_allow_html=True)
def format_date(date_str):
"""Format date string to a more readable format"""
try:
dt = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ")
return dt.strftime("%B %d, %Y")
except:
return date_str