import streamlit as st import requests # Replace this with your actual News API key API_KEY = "fe1e6bcbbf384b3e9220a7a1138805e0" # 🔑 Add your NewsAPI.org API key here publisher_articles = [] def fetch_news(topic, keyword): query = f"{topic} {keyword}" if keyword else topic url = f"https://newsapi.org/v2/everything?q={query}&apiKey={API_KEY}" response = requests.get(url) if response.status_code == 200: return response.json().get("articles", []) else: st.error("Failed to fetch news. Check your API key or internet connection.") return [] def main(): st.title("📰 News Aggregator") st.write("Publish and subscribe to topics to get the latest updates!") menu = ["Home", "Publisher Panel", "Subscriber Panel"] choice = st.sidebar.selectbox("Choose a panel:", menu) if choice == "Home": st.header("Welcome to the News Aggregator!") st.write("Navigate to the **Publisher Panel** to add news articles or to the **Subscriber Panel** to view articles.") elif choice == "Publisher Panel": st.header("📝 Publisher Panel") st.write("Publish articles to relevant topics.") title = st.text_input("Article Title:") description = st.text_area("Article Description:") link = st.text_input("Article Link:") topic = st.selectbox("Select Topic:", ["Sports", "Politics", "Technology", "Entertainment", "Health", "Science"]) if st.button("Publish Article"): if title and description and link and topic: publisher_articles.append({"title": title, "description": description, "link": link, "topic": topic}) st.success(f"Article on '{topic}' published successfully!") else: st.warning("Please fill in all fields before publishing.") elif choice == "Subscriber Panel": st.header("🔔 Subscriber Panel") st.write("Subscribe to topics and view news articles.") topics = ["Sports", "Politics", "Technology", "Entertainment", "Health", "Science"] selected_topics = st.multiselect("Select topics to subscribe to:", topics) keyword_filter = st.text_input("🔍 Enter a keyword to filter news:") if st.button("Fetch News"): if not selected_topics: st.warning("Please select at least one topic to subscribe to.") else: # Notify user about subscription st.success(f"🔔 You have subscribed to: {', '.join(selected_topics)}") st.info("Fetching news articles...") all_articles = [] # Fetch publisher articles for selected topics for article in publisher_articles: if article["topic"] in selected_topics: all_articles.append(article) # Fetch external news articles from API for topic in selected_topics: fetched_articles = fetch_news(topic, keyword_filter) for article in fetched_articles: all_articles.append({ "title": article.get("title", "No Title"), "description": article.get("description", "No Description"), "link": article.get("url", "#"), "topic": topic }) # Filter articles by keyword if provided if keyword_filter: all_articles = [a for a in all_articles if keyword_filter.lower() in a["title"].lower()] if all_articles: st.success(f"Found {len(all_articles)} articles!") for article in all_articles: st.subheader(article["title"]) st.write(article["description"]) st.write(f"Topic: {article['topic']}") st.write(f"[Read more]({article['link']})") st.write("---") else: st.info("No articles found for the selected topics or keyword.") if __name__ == "__main__": main()