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', '')
|
2022-06-12 22:52:16 +00:00
|
|
|
if 'published' in item:
|
|
|
|
self.date = item.published_parsed()
|
|
|
|
else:
|
|
|
|
self.date = ''
|
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))
|