slimshadow's picture
Create app.py
e8b42c4 verified
import streamlit as st
import requests
from bs4 import BeautifulSoup
from xml.etree import ElementTree as ET
# Define RSS feed URLs for Dainik Bhaskar
rss_feeds = {
"Opinion": "https://www.bhaskar.com/rss-v1--category-1944.xml",
"Budget 2024": "https://www.bhaskar.com/rss-v1--category-14994.xml",
"Bollywood": "https://www.bhaskar.com/rss-v1--category-11215.xml",
"Auto": "https://www.bhaskar.com/rss-v1--category-10711.xml",
"Jobs & Education": "https://www.bhaskar.com/rss-v1--category-11945.xml",
"Women": "https://www.bhaskar.com/rss-v1--category-1532.xml",
"Tech - Auto": "https://www.bhaskar.com/rss-v1--category-5707.xml",
"Happy Life": "https://www.bhaskar.com/rss-v1--category-7911.xml",
"Utility": "https://www.bhaskar.com/rss-v1--category-11616.xml",
"International": "https://www.bhaskar.com/rss-v1--category-1125.xml",
"National": "https://www.bhaskar.com/rss-v1--category-1061.xml",
"Business": "https://www.bhaskar.com/rss-v1--category-1051.xml",
"Sports": "https://www.bhaskar.com/rss-v1--category-1053.xml",
"Fake News Expose": "https://www.bhaskar.com/rss-v1--category-10891.xml",
"Magazine": "https://www.bhaskar.com/rss-v1--category-1057.xml",
"Jeevan Mantra": "https://www.bhaskar.com/rss-v1--category-3379.xml",
"Entertainment": "https://www.bhaskar.com/rss-v1--category-3998.xml",
"DB Original": "https://www.bhaskar.com/rss-v1--category-4587.xml",
"Rajasthan": "https://www.bhaskar.com/rss-v1--category-1740.xml",
"Madhya Pradesh": "https://www.bhaskar.com/rss-v1--category-1739.xml",
"Uttar Pradesh": "https://www.bhaskar.com/rss-v1--category-2052.xml",
"Bihar": "https://www.bhaskar.com/rss-v1--category-3679.xml",
"Chhattisgarh": "https://www.bhaskar.com/rss-v1--category-1741.xml",
"Haryana": "https://www.bhaskar.com/rss-v1--category-1742.xml",
"New Delhi": "https://www.bhaskar.com/rss-v1--category-7140.xml",
"Maharashtra": "https://www.bhaskar.com/rss-v1--category-2318.xml",
"Jharkhand": "https://www.bhaskar.com/rss-v1--category-3682.xml",
"Punjab": "https://www.bhaskar.com/rss-v1--category-1743.xml",
"Gujarat": "https://www.bhaskar.com/rss-v1--category-2314.xml",
"Chandigarh": "https://www.bhaskar.com/rss-v1--category-11649.xml",
"Himachal": "https://www.bhaskar.com/rss-v1--category-12084.xml",
}
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("Dainik Bhaskar 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(f"{section}")
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()