saherPervaiz commited on
Commit
1143b62
Β·
verified Β·
1 Parent(s): 40485d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -19
app.py CHANGED
@@ -4,10 +4,8 @@ import requests
4
  # Replace this with your actual News API key
5
  API_KEY = "fe1e6bcbbf384b3e9220a7a1138805e0" # πŸ”‘ Add your NewsAPI.org API key here
6
 
7
- # Store articles submitted by publishers
8
  publisher_articles = []
9
 
10
- # Function to fetch news from News API
11
  def fetch_news(topic, keyword):
12
  query = f"{topic} {keyword}" if keyword else topic
13
  url = f"https://newsapi.org/v2/everything?q={query}&apiKey={API_KEY}"
@@ -15,15 +13,13 @@ def fetch_news(topic, keyword):
15
  if response.status_code == 200:
16
  return response.json().get("articles", [])
17
  else:
18
- st.error("Failed to fetch news. Please check your API key or internet connection.")
19
  return []
20
 
21
- # Streamlit app
22
  def main():
23
- st.title("πŸ“° News Aggregator")
24
  st.write("Publish and subscribe to topics to get the latest updates!")
25
 
26
- # Sidebar: Navigation
27
  menu = ["Home", "Publisher Panel", "Subscriber Panel"]
28
  choice = st.sidebar.selectbox("Choose a panel:", menu)
29
 
@@ -34,14 +30,12 @@ def main():
34
  elif choice == "Publisher Panel":
35
  st.header("πŸ“ Publisher Panel")
36
  st.write("Publish articles to relevant topics.")
37
-
38
- # Publisher inputs
39
  title = st.text_input("Article Title:")
40
  description = st.text_area("Article Description:")
41
  link = st.text_input("Article Link:")
42
  topic = st.selectbox("Select Topic:", ["Sports", "Politics", "Technology", "Entertainment", "Health", "Science"])
43
 
44
- # Submit article
45
  if st.button("Publish Article"):
46
  if title and description and link and topic:
47
  publisher_articles.append({"title": title, "description": description, "link": link, "topic": topic})
@@ -52,28 +46,27 @@ def main():
52
  elif choice == "Subscriber Panel":
53
  st.header("πŸ”” Subscriber Panel")
54
  st.write("Subscribe to topics and view news articles.")
55
-
56
- # Subscriber inputs
57
  topics = ["Sports", "Politics", "Technology", "Entertainment", "Health", "Science"]
58
  selected_topics = st.multiselect("Select topics to subscribe to:", topics)
59
  keyword_filter = st.text_input("πŸ” Enter a keyword to filter news:")
60
 
61
- # Fetch news articles
62
  if st.button("Fetch News"):
63
  if not selected_topics:
64
- st.warning("Please select at least one topic.")
65
  else:
 
 
66
  st.info("Fetching news articles...")
67
 
68
- # Combine publisher and fetched news
69
  all_articles = []
70
-
71
- # Articles from publishers
72
  for article in publisher_articles:
73
  if article["topic"] in selected_topics:
74
  all_articles.append(article)
75
 
76
- # Articles from News API
77
  for topic in selected_topics:
78
  fetched_articles = fetch_news(topic, keyword_filter)
79
  for article in fetched_articles:
@@ -84,11 +77,10 @@ def main():
84
  "topic": topic
85
  })
86
 
87
- # Apply keyword filtering if provided
88
  if keyword_filter:
89
  all_articles = [a for a in all_articles if keyword_filter.lower() in a["title"].lower()]
90
 
91
- # Display articles
92
  if all_articles:
93
  st.success(f"Found {len(all_articles)} articles!")
94
  for article in all_articles:
 
4
  # Replace this with your actual News API key
5
  API_KEY = "fe1e6bcbbf384b3e9220a7a1138805e0" # πŸ”‘ Add your NewsAPI.org API key here
6
 
 
7
  publisher_articles = []
8
 
 
9
  def fetch_news(topic, keyword):
10
  query = f"{topic} {keyword}" if keyword else topic
11
  url = f"https://newsapi.org/v2/everything?q={query}&apiKey={API_KEY}"
 
13
  if response.status_code == 200:
14
  return response.json().get("articles", [])
15
  else:
16
+ st.error("Failed to fetch news. Check your API key or internet connection.")
17
  return []
18
 
 
19
  def main():
20
+ st.title("πŸ“° Topic-Based News Aggregator")
21
  st.write("Publish and subscribe to topics to get the latest updates!")
22
 
 
23
  menu = ["Home", "Publisher Panel", "Subscriber Panel"]
24
  choice = st.sidebar.selectbox("Choose a panel:", menu)
25
 
 
30
  elif choice == "Publisher Panel":
31
  st.header("πŸ“ Publisher Panel")
32
  st.write("Publish articles to relevant topics.")
33
+
 
34
  title = st.text_input("Article Title:")
35
  description = st.text_area("Article Description:")
36
  link = st.text_input("Article Link:")
37
  topic = st.selectbox("Select Topic:", ["Sports", "Politics", "Technology", "Entertainment", "Health", "Science"])
38
 
 
39
  if st.button("Publish Article"):
40
  if title and description and link and topic:
41
  publisher_articles.append({"title": title, "description": description, "link": link, "topic": topic})
 
46
  elif choice == "Subscriber Panel":
47
  st.header("πŸ”” Subscriber Panel")
48
  st.write("Subscribe to topics and view news articles.")
49
+
 
50
  topics = ["Sports", "Politics", "Technology", "Entertainment", "Health", "Science"]
51
  selected_topics = st.multiselect("Select topics to subscribe to:", topics)
52
  keyword_filter = st.text_input("πŸ” Enter a keyword to filter news:")
53
 
 
54
  if st.button("Fetch News"):
55
  if not selected_topics:
56
+ st.warning("Please select at least one topic to subscribe to.")
57
  else:
58
+ # Notify user about subscription
59
+ st.success(f"πŸ”” You have subscribed to: {', '.join(selected_topics)}")
60
  st.info("Fetching news articles...")
61
 
 
62
  all_articles = []
63
+
64
+ # Fetch publisher articles for selected topics
65
  for article in publisher_articles:
66
  if article["topic"] in selected_topics:
67
  all_articles.append(article)
68
 
69
+ # Fetch external news articles from API
70
  for topic in selected_topics:
71
  fetched_articles = fetch_news(topic, keyword_filter)
72
  for article in fetched_articles:
 
77
  "topic": topic
78
  })
79
 
80
+ # Filter articles by keyword if provided
81
  if keyword_filter:
82
  all_articles = [a for a in all_articles if keyword_filter.lower() in a["title"].lower()]
83
 
 
84
  if all_articles:
85
  st.success(f"Found {len(all_articles)} articles!")
86
  for article in all_articles: