Rss-TOI / app.py
slimshadow's picture
Update app.py
0941786 verified
import streamlit as st
import requests
from bs4 import BeautifulSoup
from xml.etree import ElementTree as ET
# Define RSS feed URLs
rss_feeds = {
# "Top Stories": "http://timesofindia.indiatimes.com/rssfeedstopstories.cms",
"Most Recent Stories": "http://timesofindia.indiatimes.com/rssfeedmostrecent.cms",
"India": "http://timesofindia.indiatimes.com/rssfeeds/-2128936835.cms",
"World": "http://timesofindia.indiatimes.com/rssfeeds/296589292.cms",
"NRI": "http://timesofindia.indiatimes.com/rssfeeds/7098551.cms",
"Business": "http://timesofindia.indiatimes.com/rssfeeds/1898055.cms",
"US": "https://timesofindia.indiatimes.com/rssfeeds_us/72258322.cms",
"Cricket": "http://timesofindia.indiatimes.com/rssfeeds/54829575.cms",
"Sports": "http://timesofindia.indiatimes.com/rssfeeds/4719148.cms",
"Science": "http://timesofindia.indiatimes.com/rssfeeds/-2128672765.cms",
"Environment": "http://timesofindia.indiatimes.com/rssfeeds/2647163.cms",
"Tech": "http://timesofindia.indiatimes.com/rssfeeds/66949542.cms",
"Education": "http://timesofindia.indiatimes.com/rssfeeds/913168846.cms",
"Entertainment": "http://timesofindia.indiatimes.com/rssfeeds/1081479906.cms",
"Life & Style": "http://timesofindia.indiatimes.com/rssfeeds/2886704.cms",
"Most Read": "http://timesofindia.indiatimes.com/rssfeedmostread.cms",
"Most Shared": "http://timesofindia.indiatimes.com/rssfeedmostshared.cms",
"Most Commented": "http://timesofindia.indiatimes.com/rssfeedmostcommented.cms",
"Astrology": "https://timesofindia.indiatimes.com/rssfeeds/65857041.cms",
"Auto": "https://timesofindia.indiatimes.com/rssfeeds/74317216.cms"
}
def fetch_rss_feed(url):
response = requests.get(url)
response.raise_for_status()
root = ET.fromstring(response.content)
items = root.findall('.//item')
feed_data = []
for item in items:
title = item.find('title').text
link = item.find('link').text
description = item.find('description')
pub_date = item.find('pubDate').text
# Handle cases where description might be None
if description is not None:
description_text = BeautifulSoup(description.text, 'html.parser').get_text()
images = [img['src'] for img in BeautifulSoup(description.text, 'html.parser').find_all('img', src=True)]
else:
description_text = "No description available"
images = []
feed_data.append({
'title': title,
'link': link,
'description': description_text,
'pub_date': pub_date,
'images': images
})
return feed_data
def main():
st.title("Times of India RSS Feeds")
# Sidebar navigation
section = st.sidebar.selectbox("Select a Section", list(rss_feeds.keys()))
# Fetch RSS feed data
feed_url = rss_feeds[section]
feed_data = fetch_rss_feed(feed_url)
# Display feed items
st.header(section)
for item in feed_data:
st.subheader(item['title'])
st.write(f"[Read more]({item['link']})")
# Display images if present
if item['images']:
for img in item['images']:
st.image(img)
st.write(item['description'])
st.write(f"*Published on {item['pub_date']}*")
st.write("---")
if __name__ == "__main__":
main()