saherPervaiz commited on
Commit
cb1db45
Β·
verified Β·
1 Parent(s): f8a9c37

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+
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):
9
+ url = f"https://newsapi.org/v2/everything?q={topic}&apiKey={API_KEY}"
10
+ response = requests.get(url)
11
+ if response.status_code == 200:
12
+ return response.json().get("articles", [])
13
+ else:
14
+ st.error("Failed to fetch news. Please check your API key or internet connection.")
15
+ return []
16
+
17
+ # Streamlit app
18
+ def main():
19
+ st.title("πŸ“° Topic-Based News Aggregator")
20
+ st.write("Subscribe to topics and get the latest updates in real-time!")
21
+
22
+ # Sidebar: Topic subscriptions
23
+ st.sidebar.header("πŸ”” Subscribe to Topics")
24
+ topics = ["Sports", "Politics", "Technology", "Entertainment", "Health", "Science"]
25
+ selected_topics = st.sidebar.multiselect("Select topics you're interested in:", topics)
26
+
27
+ # Sidebar: Keyword-based filtering
28
+ keyword_filter = st.sidebar.text_input("πŸ” Enter a keyword to filter news:", "")
29
+
30
+ # Display news articles
31
+ st.header("Latest News Articles")
32
+ if st.button("Fetch News"):
33
+ if not selected_topics:
34
+ st.warning("Please select at least one topic.")
35
+ else:
36
+ articles = []
37
+ for topic in selected_topics:
38
+ articles.extend(fetch_news(topic))
39
+
40
+ # Apply keyword filter if provided
41
+ if keyword_filter:
42
+ articles = [a for a in articles if keyword_filter.lower() in a.get("title", "").lower()]
43
+
44
+ if articles:
45
+ for article in articles:
46
+ st.subheader(article.get("title", "No Title"))
47
+ st.write(article.get("description", "No Description"))
48
+ st.write(f"Source: {article.get('source', {}).get('name', 'Unknown')}")
49
+ st.write(f"[Read more...]({article.get('url')})")
50
+ st.write("---")
51
+ else:
52
+ st.info("No articles found for the selected topics or keyword.")
53
+
54
+ # Footer
55
+ st.sidebar.write("Developed with ❀️ by your group!")
56
+
57
+ if __name__ == "__main__":
58
+ main()