RSS / app.py
slimshadow's picture
Update app.py
9f280a3 verified
import streamlit as st
import feedparser
from bs4 import BeautifulSoup
# Function to fetch and parse RSS feed
def fetch_rss_feed(url):
feed = feedparser.parse(url)
return feed.entries
# Function to extract images and text from HTML content
def extract_images_and_text(html_content):
soup = BeautifulSoup(html_content, 'html.parser')
images = [img['src'] for img in soup.find_all('img')]
text = soup.get_text()
return images, text
# Function to display RSS feed content
def display_feed(feed_url):
entries = fetch_rss_feed(feed_url)
for entry in entries:
st.subheader(entry.title)
st.write(f"Published on: {entry.published}")
# Check for media content
media_content = entry.get('media_content', [])
if media_content:
for media in media_content:
if media.get('url'):
st.image(media['url'], width=300) # Display image
# Use BeautifulSoup to parse HTML content
content_encoded = entry.get('content_encoded', entry.description)
images, text = extract_images_and_text(content_encoded)
st.write(text) # Display plain text
for img_url in images:
st.image(img_url, width=600) # Display images if any
st.write(f"[Read more]({entry.link})")
# Main function to handle Streamlit app
def main():
st.sidebar.title("Navigation")
selection = st.sidebar.radio("Choose a section", [
"Latest Stories",
"Top Stories",
"World",
"Movies",
"Tech",
"Trending News",
"India News",
"Sports",
"Cricket",
"Car & Bike",
"Cities News",
"South",
"Indians Abroad",
"Cooks",
"Offbeat News",
"People",
"NDTV Khabar",
"Latest Videos"
])
# Define RSS feed URLs for different sections
feed_urls = {
"Latest Stories": "https://feeds.feedburner.com/ndtvnews-latest",
"Top Stories": "https://feeds.feedburner.com/ndtvnews-top-stories",
"World": "https://feeds.feedburner.com/ndtvnews-world-news",
"Movies": "https://feeds.feedburner.com/ndtvmovies-latest",
"Tech": "https://feeds.feedburner.com/gadgets360-latest",
"Trending News": "https://feeds.feedburner.com/ndtvnews-trending-news",
"India News": "https://feeds.feedburner.com/ndtvnews-india-news",
"Sports": "https://feeds.feedburner.com/ndtvsports-latest",
"Cricket": "https://feeds.feedburner.com/ndtvsports-cricket",
"Car & Bike": "https://feeds.feedburner.com/carandbike-latest",
"Cities News": "https://feeds.feedburner.com/ndtvnews-cities-news",
"South": "https://feeds.feedburner.com/ndtvnews-south",
"Indians Abroad": "https://feeds.feedburner.com/ndtvnews-indians-abroad",
"Cooks": "https://feeds.feedburner.com/ndtvcooks-latest",
"Offbeat News": "https://feeds.feedburner.com/ndtvnews-offbeat-news",
"People": "https://feeds.feedburner.com/ndtvnews-people",
"NDTV Khabar": "https://feeds.feedburner.com/ndtvkhabar-latest",
"Latest Videos": "https://feeds.feedburner.com/ndtv/latest-videos"
}
feed_url = feed_urls.get(selection)
st.title(f"NDTV {selection}")
display_feed(feed_url)
# Run the app
if __name__ == "__main__":
main()