File size: 3,419 Bytes
335bd01
 
 
 
 
 
 
0941786
335bd01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bf02d2
335bd01
5bf02d2
 
 
 
 
 
 
 
d297191
335bd01
 
 
d297191
 
 
335bd01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d297191
 
 
 
 
 
335bd01
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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()