tg_rss_bot/rss.py

22 lines
646 B
Python
Raw Normal View History

2022-06-12 17:13:58 +00:00
from feedparser import FeedParserDict, parse
2022-05-29 20:17:03 +00:00
2022-06-11 22:51:39 +00:00
class FeedItem:
2022-06-12 17:13:58 +00:00
def __init__(self, item: FeedParserDict) -> None:
2022-06-12 13:18:55 +00:00
self.url = item.get('link', '')
self.title = item.get('title', '')
self.description = item.get('summary', '')
self.date = item.get('published', '')
2022-05-29 20:17:03 +00:00
2022-06-11 22:51:39 +00:00
class Feed:
2022-06-12 17:13:58 +00:00
def __init__(self, url: str, feed: FeedParserDict) -> None:
2022-05-29 20:18:12 +00:00
self.url = url
2022-06-12 17:13:58 +00:00
self.items = []
2022-06-12 20:48:05 +00:00
self.title = feed.feed.get('title', '')
for item in feed.entries:
2022-06-12 17:13:58 +00:00
self.items.append(FeedItem(item))
2022-05-29 20:17:03 +00:00
2022-06-12 13:18:55 +00:00
class RssReader:
def get_feed(self, url: str) -> Feed:
2022-06-12 17:13:58 +00:00
return Feed(url, parse(url))