Spaces:
Paused
Paused
Delete news.py
Browse files
news.py
DELETED
|
@@ -1,50 +0,0 @@
|
|
| 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 |
-
def run():
|
| 8 |
-
st.title("News Fetcher")
|
| 9 |
-
|
| 10 |
-
# Define country options
|
| 11 |
-
countries = {
|
| 12 |
-
'us': 'United States', 'in': 'India', 'gb': 'United Kingdom',
|
| 13 |
-
'ca': 'Canada', 'au': 'Australia', 'de': 'Germany'
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
-
# Sidebar selections
|
| 17 |
-
st.sidebar.title("Filters")
|
| 18 |
-
country = st.sidebar.selectbox("Select country", list(countries.keys()), format_func=lambda x: countries[x])
|
| 19 |
-
query = st.sidebar.text_input("Search for news")
|
| 20 |
-
|
| 21 |
-
# Date range selection
|
| 22 |
-
from_date = st.sidebar.date_input("From", datetime.now() - timedelta(days=7))
|
| 23 |
-
to_date = st.sidebar.date_input("To", datetime.now())
|
| 24 |
-
|
| 25 |
-
# Fetch news function
|
| 26 |
-
def get_news(api_key, query, country, from_date, to_date):
|
| 27 |
-
url = 'https://newsapi.org/v2/everything'
|
| 28 |
-
params = {'apiKey': api_key, 'q': query, 'from': from_date, 'to': to_date, 'language': 'en', 'pageSize': 10}
|
| 29 |
-
try:
|
| 30 |
-
response = requests.get(url, params=params)
|
| 31 |
-
response.raise_for_status()
|
| 32 |
-
return response.json().get("articles", [])
|
| 33 |
-
except requests.exceptions.RequestException as e:
|
| 34 |
-
st.error(f"Error fetching news: {e}")
|
| 35 |
-
return []
|
| 36 |
-
|
| 37 |
-
# API Key (replace with your own)
|
| 38 |
-
API_KEY = "your_news_api_key"
|
| 39 |
-
|
| 40 |
-
if st.sidebar.button("Fetch News"):
|
| 41 |
-
articles = get_news(API_KEY, query, country, from_date, to_date)
|
| 42 |
-
if articles:
|
| 43 |
-
for article in articles:
|
| 44 |
-
st.subheader(article["title"])
|
| 45 |
-
st.write(article["description"])
|
| 46 |
-
st.markdown(f"[Read more]({article['url']})")
|
| 47 |
-
st.write("---")
|
| 48 |
-
else:
|
| 49 |
-
st.write("No articles found.")
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|