saherPervaiz commited on
Commit
c1ce264
Β·
verified Β·
1 Parent(s): fd95745

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -40
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("Subscribe to topics and get the latest updates in real-time!")
23
 
24
- # Sidebar: Topic subscriptions
25
- st.sidebar.header("πŸ”” Subscribe to Topics")
26
- topics = ["Sports", "Politics", "Technology", "Entertainment", "Health", "Science"]
27
- selected_topics = st.sidebar.multiselect("Select topics you're interested in:", topics)
28
 
29
- # Sidebar: Keyword-based filtering
30
- keyword_filter = st.sidebar.text_input("πŸ” Enter a keyword to filter news:", "")
 
 
 
 
 
31
 
32
- # Display prompt messages
33
- if not selected_topics:
34
- st.info("πŸ”” Please select at least one topic from the sidebar.")
35
- elif keyword_filter:
36
- st.info(f"πŸ” Searching for news on '{', '.join(selected_topics)}' with keyword: '{keyword_filter}'.")
37
- else:
38
- st.info(f"πŸ” Searching for news on '{', '.join(selected_topics)}' without any specific keyword.")
39
-
40
- # Display news articles
41
- st.header("Latest News Articles")
42
- if st.button("Fetch News"):
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.info("❌ No articles found for the selected topics or keyword. Try different options.")
62
 
63
- # Footer
64
- st.sidebar.write("Developed with ❀️ by your group!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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