gskdsrikrishna commited on
Commit
46c3f1b
·
verified ·
1 Parent(s): 26fcd8f

Upload news.py

Browse files
Files changed (1) hide show
  1. news.py +224 -0
news.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+ from datetime import datetime, timedelta
6
+
7
+ # Set page configuration
8
+ st.set_page_config(page_title="News Fetcher", layout="wide")
9
+
10
+ # CSS styling for Canva theme
11
+ st.markdown(
12
+ """
13
+ <style>
14
+ /* General styling */
15
+ body {
16
+ font-family: 'Helvetica Neue', Arial, sans-serif;
17
+ background-color: #f8f9fa; /* Light gray background */
18
+ color: #333; /* Dark gray font color */
19
+ }
20
+
21
+ /* Title styling */
22
+ h1 {
23
+ color: #00bcd4; /* Canva teal */
24
+ font-size: 2.5em;
25
+ margin-bottom: 0.5em;
26
+ }
27
+
28
+ /* Sidebar styling */
29
+ .css-18e3th9 {
30
+ background-color: #ffffff; /* White background for sidebar */
31
+ color: #333; /* Dark text color */
32
+ border-right: 1px solid #e0e0e0; /* Light gray border */
33
+ padding: 20px;
34
+ }
35
+
36
+ /* Sidebar header */
37
+ .css-hxt7ib {
38
+ color: #00bcd4; /* Canva teal */
39
+ font-weight: bold;
40
+ }
41
+
42
+ /* Button styling */
43
+ button {
44
+ background-color: #00bcd4 !important; /* Canva teal button */
45
+ color: white !important;
46
+ border: none !important;
47
+ font-weight: bold !important;
48
+ padding: 10px 20px;
49
+ border-radius: 5px;
50
+ }
51
+
52
+ /* Dropdown and input styling */
53
+ .stTextInput, .stDateInput, .stSelectbox {
54
+ border: 1px solid #e0e0e0 !important; /* Light gray border */
55
+ border-radius: 5px !important;
56
+ padding: 10px !important;
57
+ font-size: 1em !important;
58
+ }
59
+
60
+ /* News articles styling */
61
+ .stMarkdown, .stSubheader {
62
+ font-family: 'Helvetica Neue', Arial, sans-serif;
63
+ background-color: #ffffff; /* White background for articles */
64
+ color: #212529; /* Darker gray for articles text */
65
+ padding: 15px;
66
+ border-radius: 8px;
67
+ box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
68
+ }
69
+
70
+ /* Footer styling */
71
+ footer {
72
+ font-size: 0.85em;
73
+ color: #6c757d; /* Gray footer text */
74
+ text-align: center;
75
+ margin-top: 50px;
76
+ padding: 10px;
77
+ border-top: 1px solid #e0e0e0;
78
+ }
79
+
80
+ /* Link styling */
81
+ a {
82
+ color: #00bcd4; /* Canva teal */
83
+ }
84
+
85
+ a:hover {
86
+ color: #0097a7; /* Darker teal on hover */
87
+ }
88
+ </style>
89
+ """,
90
+ unsafe_allow_html=True
91
+ )
92
+
93
+ # Define countries dictionary
94
+ countries = {
95
+ 'us': 'United States',
96
+ 'in': 'India',
97
+ 'gb': 'United Kingdom',
98
+ 'ca': 'Canada',
99
+ 'au': 'Australia',
100
+ 'de': 'Germany',
101
+ 'fr': 'France',
102
+ 'it': 'Italy',
103
+ 'jp': 'Japan',
104
+ 'cn': 'China',
105
+ 'br': 'Brazil',
106
+ 'za': 'South Africa',
107
+ 'ru': 'Russia',
108
+ }
109
+
110
+ def get_news(api_key, query=None, country='us', language='en', from_date=None, to_date=None):
111
+ url = 'https://newsapi.org/v2/everything'
112
+ params = {
113
+ 'apiKey': api_key,
114
+ 'q': query,
115
+ 'from': from_date,
116
+ 'to': to_date,
117
+ 'language': language,
118
+ 'pageSize': 20
119
+ }
120
+
121
+ try:
122
+ response = requests.get(url, params=params)
123
+ response.raise_for_status()
124
+ news_data = response.json()
125
+ if news_data['status'] == 'ok':
126
+ return news_data['articles']
127
+ else:
128
+ st.error("Error fetching news: {}".format(news_data['message']))
129
+ return []
130
+
131
+ except requests.exceptions.RequestException as e:
132
+ st.error("HTTP Request failed: {}".format(e))
133
+ return []
134
+
135
+ # Important queries
136
+ important_queries = [
137
+ "COVID-19", "Technology", "Politics", "Economy", "Health",
138
+ "Environment", "Sports", "Entertainment", "Science",
139
+ "Education", "Travel"
140
+ ]
141
+
142
+ # Streamlit UI setup
143
+ st.title("News Fetcher")
144
+
145
+ # Store and display the last seen timestamp
146
+ if 'last_seen' not in st.session_state:
147
+ st.session_state['last_seen'] = datetime.now()
148
+ else:
149
+ st.session_state['last_seen'] = datetime.now()
150
+ last_seen = st.session_state['last_seen']
151
+
152
+ # Sidebar layout
153
+ st.sidebar.title("Settings")
154
+ st.sidebar.write(f"Last accessed on: {last_seen.strftime('%Y-%m-%d %H:%M:%S')}")
155
+
156
+ # Language selection
157
+ language = st.selectbox("Select your preferred language:",
158
+ options=[
159
+ ('en', 'English'), ('es', 'Spanish'), ('fr', 'French'),
160
+ ('de', 'German'), ('it', 'Italian'), ('pt', 'Portuguese'),
161
+ ('ar', 'Arabic'), ('zh', 'Chinese'), ('hi', 'Hindi'),
162
+ ('te', 'Telugu')
163
+ ])
164
+
165
+ # Country selection in the sidebar
166
+ country = st.sidebar.selectbox("Select your country:", options=list(countries.keys()), format_func=lambda x: countries[x])
167
+
168
+ # Sidebar for important queries
169
+ st.sidebar.header("Important Queries")
170
+ for query in important_queries:
171
+ if st.sidebar.button(query):
172
+ st.session_state.query = query
173
+
174
+ # Input field for user queries
175
+ if 'query' in st.session_state:
176
+ query = st.session_state.query
177
+ else:
178
+ query = st.text_input("Enter a search query:", placeholder="Type something...")
179
+
180
+ # Date pickers for filtering news articles
181
+ st.write("Select the date range for previous news articles:")
182
+ from_date = st.date_input("From", value=datetime.now() - timedelta(days=30))
183
+ to_date = st.date_input("To", value=datetime.now())
184
+
185
+ # Button to fetch news
186
+ if st.button("Fetch News"):
187
+ API_KEY = '43283de608cc43b7a49ad17ceda39636' # Replace with your actual News API key
188
+ news_articles = get_news(API_KEY, query=query, country=country, language=language, from_date=from_date, to_date=to_date)
189
+
190
+ if news_articles:
191
+ for i, article in enumerate(news_articles):
192
+ st.subheader(article['title'])
193
+ st.markdown(f"**Source**: {article['source']['name']} | **Published At**: {article['publishedAt']}")
194
+ st.write(article['description'] or "No description available")
195
+ st.markdown(f"[Read more]({article['url']})")
196
+ st.markdown("---")
197
+
198
+ # Visualization of trends in news topics
199
+ dates = [article['publishedAt'][:10] for article in news_articles]
200
+ date_counts = pd.Series(dates).value_counts().sort_index()
201
+
202
+ st.subheader("Trends in News Topics Over Time")
203
+ plt.figure(figsize=(10, 5))
204
+ plt.plot(date_counts.index, date_counts.values, marker='o')
205
+ plt.xticks(rotation=45)
206
+ plt.xlabel('Date')
207
+ plt.ylabel('Number of Articles')
208
+ plt.title('Frequency of Articles Over Time')
209
+ st.pyplot(plt)
210
+
211
+ else:
212
+ st.write("No articles found.")
213
+
214
+ # Footer
215
+ st.markdown("---")
216
+ st.markdown(
217
+ """
218
+ <footer>
219
+ Developed by SriKrishna | © 2024 | All rights reserved.<br>
220
+ Last accessed on: {}
221
+ </footer>
222
+ """.format(last_seen.strftime('%Y-%m-%d %H:%M:%S')),
223
+ unsafe_allow_html=True
224
+ )