Rss_the_hindu / app.py
slimshadow's picture
Update app.py
6288638 verified
import streamlit as st
import requests
from bs4 import BeautifulSoup
from xml.etree import ElementTree as ET
# Define RSS feed URLs for The Hindu
rss_feeds = {
"News": {
"Default": "https://www.thehindu.com/news/feeder/default.rss",
"India": "https://www.thehindu.com/news/national/feeder/default.rss",
"World": "https://www.thehindu.com/news/international/feeder/default.rss",
"States": "https://www.thehindu.com/news/states/feeder/default.rss",
"Cities": "https://www.thehindu.com/news/cities/feeder/default.rss",
},
"Opinion": {
"Cartoon": "https://www.thehindu.com/opinion/cartoon/feeder/default.rss",
"Columns": "https://www.thehindu.com/opinion/columns/feeder/default.rss",
"Comment": "https://www.thehindu.com/opinion/comment/feeder/default.rss",
"Editorial": "https://www.thehindu.com/opinion/editorial/feeder/default.rss",
"Interview": "https://www.thehindu.com/opinion/interview/feeder/default.rss",
"Lead": "https://www.thehindu.com/opinion/lead/feeder/default.rss",
"Letters": "https://www.thehindu.com/opinion/letters/feeder/default.rss",
"Open Page": "https://www.thehindu.com/opinion/open-page/feeder/default.rss",
"Readers' Editor": "https://www.thehindu.com/opinion/readers-editor/feeder/default.rss",
"Corrections & Clarifications": "https://www.thehindu.com/opinion/corrections-clarifications/feeder/default.rss",
},
"Business": {
"Agri-Business": "https://www.thehindu.com/business/agri-business/feeder/default.rss",
"Industry": "https://www.thehindu.com/business/industry/feeder/default.rss",
"Economy": "https://www.thehindu.com/business/economy/feeder/default.rss",
"Markets": "https://www.thehindu.com/business/markets/feeder/default.rss",
"Budget": "https://www.thehindu.com/business/budget/feeder/default.rss",
},
"Sport": {
"Olympics": "https://www.thehindu.com/sport/olympics/feeder/default.rss",
"Cricket": "https://www.thehindu.com/sport/cricket/feeder/default.rss",
"Football": "https://www.thehindu.com/sport/football/feeder/default.rss",
"Hockey": "https://www.thehindu.com/sport/hockey/feeder/default.rss",
"Tennis": "https://www.thehindu.com/sport/tennis/feeder/default.rss",
"Athletics": "https://www.thehindu.com/sport/athletics/feeder/default.rss",
"Motorsport": "https://www.thehindu.com/sport/motorsport/feeder/default.rss",
"Races": "https://www.thehindu.com/sport/races/feeder/default.rss",
"Other Sports": "https://www.thehindu.com/sport/other-sports/feeder/default.rss",
},
"Entertainment": {
"Art": "https://www.thehindu.com/entertainment/art/feeder/default.rss",
"Dance": "https://www.thehindu.com/entertainment/dance/feeder/default.rss",
"Movies": "https://www.thehindu.com/entertainment/movies/feeder/default.rss",
"Music": "https://www.thehindu.com/entertainment/music/feeder/default.rss",
"Reviews": "https://www.thehindu.com/entertainment/reviews/feeder/default.rss",
"Theatre": "https://www.thehindu.com/entertainment/theatre/feeder/default.rss",
},
"Life & Style": {
"Fashion": "https://www.thehindu.com/life-and-style/fashion/feeder/default.rss",
"Fitness": "https://www.thehindu.com/life-and-style/fitness/feeder/default.rss",
"Food": "https://www.thehindu.com/life-and-style/food/feeder/default.rss",
"Motoring": "https://www.thehindu.com/life-and-style/motoring/feeder/default.rss",
"Travel": "https://www.thehindu.com/life-and-style/travel/feeder/default.rss",
"Homes and gardens": "https://www.thehindu.com/life-and-style/homes-and-gardens/feeder/default.rss",
"Luxury": "https://www.thehindu.com/life-and-style/luxury/feeder/default.rss",
},
"Home": {
"Default": "https://www.thehindu.com/home/feeder/default.rss",
},
"Science": {
"Default": "https://www.thehindu.com/sci-tech/science/feeder/default.rss",
}
}
def fetch_rss_feed(url):
response = requests.get(url)
response.raise_for_status()
root = ET.fromstring(response.content)
# Extract channel-wide image URL
channel_image = root.find('.//image/url')
channel_image_url = channel_image.text if channel_image is not None else None
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
media_content = item.find('{http://search.yahoo.com/mrss/}content')
# Handle cases where description might be None
description_text = BeautifulSoup(description.text, 'html.parser').get_text() if description is not None and description.text else "No description available"
# Extract image URL from media content or fallback to channel image
image_url = media_content.attrib['url'] if media_content is not None and 'url' in media_content.attrib else channel_image_url
feed_data.append({
'title': title,
'link': link,
'description': description_text,
'pub_date': pub_date,
'image': image_url
})
return feed_data
def main():
st.title("The Hindu RSS Feeds")
# Sidebar navigation
section = st.sidebar.selectbox("Select a Section", list(rss_feeds.keys()))
subsection = st.sidebar.selectbox("Select a Sub-Section", list(rss_feeds[section].keys()))
# Fetch RSS feed data
feed_url = rss_feeds[section][subsection]
feed_data = fetch_rss_feed(feed_url)
# Display feed items
st.header(f"{section} - {subsection}")
for item in feed_data:
st.subheader(item['title'])
st.write(f"[Read more]({item['link']})")
# Display image if present
if item['image']:
st.image(item['image'])
st.write(item['description'])
st.write(f"*Published on {item['pub_date']}*")
st.write("---")
if __name__ == "__main__":
main()