Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,9 +4,17 @@ import requests
|
|
4 |
# Replace this with your actual News API key
|
5 |
API_KEY = "fe1e6bcbbf384b3e9220a7a1138805e0" # π Add your NewsAPI.org API key here
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# Function to fetch news from News API
|
8 |
def fetch_news(topic, keyword):
|
9 |
-
# Combine topic and keyword for a refined search
|
10 |
query = f"{topic} {keyword}" if keyword else topic
|
11 |
url = f"https://newsapi.org/v2/everything?q={query}&apiKey={API_KEY}"
|
12 |
response = requests.get(url)
|
@@ -19,51 +27,85 @@ def fetch_news(topic, keyword):
|
|
19 |
# Streamlit app
|
20 |
def main():
|
21 |
st.title("π° Topic-Based News Aggregator")
|
22 |
-
st.write("
|
23 |
|
24 |
-
# Sidebar:
|
25 |
-
|
26 |
-
|
27 |
-
selected_topics = st.sidebar.multiselect("Select topics you're interested in:", topics)
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
st.
|
35 |
-
|
36 |
-
st.
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
if not selected_topics:
|
44 |
-
st.warning("β οΈ You must select at least one topic to fetch news.")
|
45 |
-
else:
|
46 |
-
st.info("β³ Fetching news articles. Please wait...")
|
47 |
-
articles = []
|
48 |
-
for topic in selected_topics:
|
49 |
-
# Fetch news for each topic combined with the keyword
|
50 |
-
articles.extend(fetch_news(topic, keyword_filter))
|
51 |
-
|
52 |
-
if articles:
|
53 |
-
st.success(f"β
Found {len(articles)} articles!")
|
54 |
-
for article in articles:
|
55 |
-
st.subheader(article.get("title", "No Title"))
|
56 |
-
st.write(article.get("description", "No Description"))
|
57 |
-
st.write(f"Source: {article.get('source', {}).get('name', 'Unknown')}")
|
58 |
-
st.write(f"[Read more...]({article.get('url')})")
|
59 |
-
st.write("---")
|
60 |
else:
|
61 |
-
st.
|
62 |
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
if __name__ == "__main__":
|
67 |
main()
|
68 |
|
69 |
-
|
|
|
4 |
# Replace this with your actual News API key
|
5 |
API_KEY = "fe1e6bcbbf384b3e9220a7a1138805e0" # π Add your NewsAPI.org API key here
|
6 |
|
7 |
+
import streamlit as st
|
8 |
+
import requests
|
9 |
+
|
10 |
+
# Replace this with your actual News API key
|
11 |
+
API_KEY = "YOUR_NEWS_API_KEY"
|
12 |
+
|
13 |
+
# Store articles submitted by publishers
|
14 |
+
publisher_articles = []
|
15 |
+
|
16 |
# Function to fetch news from News API
|
17 |
def fetch_news(topic, keyword):
|
|
|
18 |
query = f"{topic} {keyword}" if keyword else topic
|
19 |
url = f"https://newsapi.org/v2/everything?q={query}&apiKey={API_KEY}"
|
20 |
response = requests.get(url)
|
|
|
27 |
# Streamlit app
|
28 |
def main():
|
29 |
st.title("π° Topic-Based News Aggregator")
|
30 |
+
st.write("Publish and subscribe to topics to get the latest updates!")
|
31 |
|
32 |
+
# Sidebar: Navigation
|
33 |
+
menu = ["Home", "Publisher Panel", "Subscriber Panel"]
|
34 |
+
choice = st.sidebar.selectbox("Choose a panel:", menu)
|
|
|
35 |
|
36 |
+
if choice == "Home":
|
37 |
+
st.header("Welcome to the News Aggregator!")
|
38 |
+
st.write("Navigate to the **Publisher Panel** to add news articles or to the **Subscriber Panel** to view articles.")
|
39 |
+
|
40 |
+
elif choice == "Publisher Panel":
|
41 |
+
st.header("π Publisher Panel")
|
42 |
+
st.write("Publish articles to relevant topics.")
|
43 |
|
44 |
+
# Publisher inputs
|
45 |
+
title = st.text_input("Article Title:")
|
46 |
+
description = st.text_area("Article Description:")
|
47 |
+
link = st.text_input("Article Link:")
|
48 |
+
topic = st.selectbox("Select Topic:", ["Sports", "Politics", "Technology", "Entertainment", "Health", "Science"])
|
49 |
+
|
50 |
+
# Submit article
|
51 |
+
if st.button("Publish Article"):
|
52 |
+
if title and description and link and topic:
|
53 |
+
publisher_articles.append({"title": title, "description": description, "link": link, "topic": topic})
|
54 |
+
st.success(f"Article on '{topic}' published successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
else:
|
56 |
+
st.warning("Please fill in all fields before publishing.")
|
57 |
|
58 |
+
elif choice == "Subscriber Panel":
|
59 |
+
st.header("π Subscriber Panel")
|
60 |
+
st.write("Subscribe to topics and view news articles.")
|
61 |
+
|
62 |
+
# Subscriber inputs
|
63 |
+
topics = ["Sports", "Politics", "Technology", "Entertainment", "Health", "Science"]
|
64 |
+
selected_topics = st.multiselect("Select topics to subscribe to:", topics)
|
65 |
+
keyword_filter = st.text_input("π Enter a keyword to filter news:")
|
66 |
+
|
67 |
+
# Fetch news articles
|
68 |
+
if st.button("Fetch News"):
|
69 |
+
if not selected_topics:
|
70 |
+
st.warning("Please select at least one topic.")
|
71 |
+
else:
|
72 |
+
st.info("Fetching news articles...")
|
73 |
+
|
74 |
+
# Combine publisher and fetched news
|
75 |
+
all_articles = []
|
76 |
+
|
77 |
+
# Articles from publishers
|
78 |
+
for article in publisher_articles:
|
79 |
+
if article["topic"] in selected_topics:
|
80 |
+
all_articles.append(article)
|
81 |
+
|
82 |
+
# Articles from News API
|
83 |
+
for topic in selected_topics:
|
84 |
+
fetched_articles = fetch_news(topic, keyword_filter)
|
85 |
+
for article in fetched_articles:
|
86 |
+
all_articles.append({
|
87 |
+
"title": article.get("title", "No Title"),
|
88 |
+
"description": article.get("description", "No Description"),
|
89 |
+
"link": article.get("url", "#"),
|
90 |
+
"topic": topic
|
91 |
+
})
|
92 |
+
|
93 |
+
# Apply keyword filtering if provided
|
94 |
+
if keyword_filter:
|
95 |
+
all_articles = [a for a in all_articles if keyword_filter.lower() in a["title"].lower()]
|
96 |
+
|
97 |
+
# Display articles
|
98 |
+
if all_articles:
|
99 |
+
st.success(f"Found {len(all_articles)} articles!")
|
100 |
+
for article in all_articles:
|
101 |
+
st.subheader(article["title"])
|
102 |
+
st.write(article["description"])
|
103 |
+
st.write(f"Topic: {article['topic']}")
|
104 |
+
st.write(f"[Read more]({article['link']})")
|
105 |
+
st.write("---")
|
106 |
+
else:
|
107 |
+
st.info("No articles found for the selected topics or keyword.")
|
108 |
|
109 |
if __name__ == "__main__":
|
110 |
main()
|
111 |
|
|