2022-07-10 12:07:26 +00:00
|
|
|
from logging import Logger
|
2022-07-10 14:40:19 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from time import mktime
|
2022-07-08 19:23:12 +00:00
|
|
|
from feedparser import FeedParserDict, parse
|
2022-05-29 20:17:03 +00:00
|
|
|
|
|
|
|
|
2022-07-08 19:23:12 +00:00
|
|
|
class FeedItem:
|
|
|
|
def __init__(self, item: FeedParserDict) -> None:
|
|
|
|
self.url = item.get('link', '')
|
|
|
|
self.title = item.get('title', '')
|
|
|
|
self.description = item.get('summary', '')
|
|
|
|
if 'published' in item:
|
2022-07-10 14:40:19 +00:00
|
|
|
self.date = datetime.fromtimestamp(mktime(item.published_parsed))
|
2022-07-08 19:23:12 +00:00
|
|
|
else:
|
|
|
|
self.date = None
|
2022-05-29 20:17:03 +00:00
|
|
|
|
|
|
|
|
2022-07-08 19:23:12 +00:00
|
|
|
class Feed:
|
|
|
|
def __init__(self, url: str, feed: FeedParserDict) -> None:
|
|
|
|
self.url = url
|
|
|
|
self.items = []
|
|
|
|
self.title = feed.feed.get('title', '')
|
|
|
|
for item in feed.entries:
|
|
|
|
self.items.append(FeedItem(item))
|
2022-05-29 20:17:03 +00:00
|
|
|
|
|
|
|
|
2022-07-08 19:23:12 +00:00
|
|
|
class RssReader:
|
2022-07-10 12:07:26 +00:00
|
|
|
|
|
|
|
def __init__(self, logger: Logger):
|
|
|
|
self.log: Logger = logger
|
|
|
|
self.log.debug('RssReader.__init__(logger=%s)', logger)
|
|
|
|
|
2022-07-08 19:23:12 +00:00
|
|
|
def get_feed(self, url: str) -> Feed:
|
2022-07-10 12:07:26 +00:00
|
|
|
self.log.debug('get_feed(url=\'%s\')', url)
|
2022-07-08 19:23:12 +00:00
|
|
|
return Feed(url, parse(url))
|