Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
from xml.etree import ElementTree as ET
|
| 5 |
+
|
| 6 |
+
# Define RSS feed URLs for ABP News
|
| 7 |
+
rss_feeds = {
|
| 8 |
+
"Home Page": "https://www.abplive.com/home/feed",
|
| 9 |
+
"India News": "https://www.abplive.com/news/india/feed",
|
| 10 |
+
"World News": "https://www.abplive.com/news/world/feed",
|
| 11 |
+
"States": "https://www.abplive.com/states/feed",
|
| 12 |
+
"Sports": "https://www.abplive.com/sports/feed",
|
| 13 |
+
"Bollywood": "https://www.abplive.com/entertainment/bollywood/feed",
|
| 14 |
+
"Television": "https://www.abplive.com/entertainment/television/feed",
|
| 15 |
+
"Tamil Cinema": "https://www.abplive.com/entertainment/tamil-cinema/feed",
|
| 16 |
+
"Bhojpuri Cinema": "https://www.abplive.com/entertainment/bhojpuri-cinema/feed",
|
| 17 |
+
"Astro": "https://www.abplive.com/astro/feed",
|
| 18 |
+
"Religion": "https://www.abplive.com/lifestyle/religion/feed",
|
| 19 |
+
"Business": "https://www.abplive.com/business/feed",
|
| 20 |
+
"Gadgets": "https://www.abplive.com/technology/gadgets/feed",
|
| 21 |
+
"Life Style": "https://www.abplive.com/lifestyle/feed",
|
| 22 |
+
"Health": "https://www.abplive.com/lifestyle/health/feed",
|
| 23 |
+
"Technology": "https://www.abplive.com/technology/feed",
|
| 24 |
+
"Education": "https://www.abplive.com/education/feed",
|
| 25 |
+
"Jobs": "https://www.abplive.com/education/jobs/feed",
|
| 26 |
+
"Coronavirus": "https://www.abplive.com/latest-news/coronavirus/feed",
|
| 27 |
+
"Agricultures": "https://www.abplive.com/agriculture/feed",
|
| 28 |
+
"GK": "https://www.abplive.com/gk/feed",
|
| 29 |
+
"Utility News": "https://www.abplive.com/utility-news/feed",
|
| 30 |
+
"Web Stories": "https://www.abplive.com/web-stories/feed",
|
| 31 |
+
"Blog": "https://www.abplive.com/blog/feed",
|
| 32 |
+
"Short Video": "https://www.abplive.com/short-video/feed",
|
| 33 |
+
"Photo Gallery": "https://www.abplive.com/photo-gallery/feed",
|
| 34 |
+
"Fact Check": "https://www.abplive.com/fact-check/feed",
|
| 35 |
+
"Trending": "https://www.abplive.com/trending/feed",
|
| 36 |
+
"Election": "https://www.abplive.com/elections/feed",
|
| 37 |
+
"Sports IPL": "https://www.abplive.com/sports/ipl/feed",
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
def fetch_rss_feed(url):
|
| 41 |
+
response = requests.get(url)
|
| 42 |
+
response.raise_for_status()
|
| 43 |
+
root = ET.fromstring(response.content)
|
| 44 |
+
|
| 45 |
+
# Extract channel-wide image URL
|
| 46 |
+
channel_image = root.find('.//image/url')
|
| 47 |
+
channel_image_url = channel_image.text if channel_image is not None else None
|
| 48 |
+
|
| 49 |
+
items = root.findall('.//item')
|
| 50 |
+
feed_data = []
|
| 51 |
+
|
| 52 |
+
for item in items:
|
| 53 |
+
title = item.find('title').text
|
| 54 |
+
link = item.find('link').text
|
| 55 |
+
description = item.find('description')
|
| 56 |
+
pub_date = item.find('pubDate').text
|
| 57 |
+
media_thumbnail = item.find('{http://search.yahoo.com/mrss/}thumbnail')
|
| 58 |
+
|
| 59 |
+
# Handle cases where description might be None
|
| 60 |
+
description_text = BeautifulSoup(description.text, 'html.parser').get_text() if description is not None and description.text else "No description available"
|
| 61 |
+
|
| 62 |
+
# Extract image URL from media thumbnail or fallback to channel image
|
| 63 |
+
image_url = media_thumbnail.attrib['url'] if media_thumbnail is not None and 'url' in media_thumbnail.attrib else channel_image_url
|
| 64 |
+
|
| 65 |
+
feed_data.append({
|
| 66 |
+
'title': title,
|
| 67 |
+
'link': link,
|
| 68 |
+
'description': description_text,
|
| 69 |
+
'pub_date': pub_date,
|
| 70 |
+
'image': image_url
|
| 71 |
+
})
|
| 72 |
+
return feed_data
|
| 73 |
+
|
| 74 |
+
def main():
|
| 75 |
+
st.title("ABP News RSS Feeds")
|
| 76 |
+
|
| 77 |
+
# Sidebar navigation
|
| 78 |
+
section = st.sidebar.selectbox("Select a Section", list(rss_feeds.keys()))
|
| 79 |
+
|
| 80 |
+
# Fetch RSS feed data
|
| 81 |
+
feed_url = rss_feeds[section]
|
| 82 |
+
feed_data = fetch_rss_feed(feed_url)
|
| 83 |
+
|
| 84 |
+
# Display feed items
|
| 85 |
+
st.header(f"{section}")
|
| 86 |
+
for item in feed_data:
|
| 87 |
+
st.subheader(item['title'])
|
| 88 |
+
st.write(f"[Read more]({item['link']})")
|
| 89 |
+
|
| 90 |
+
# Display image if present
|
| 91 |
+
if item['image']:
|
| 92 |
+
st.image(item['image'])
|
| 93 |
+
|
| 94 |
+
st.write(item['description'])
|
| 95 |
+
st.write(f"*Published on {item['pub_date']}*")
|
| 96 |
+
st.write("---")
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
main()
|