tg_rss_bot/rss.py

35 lines
996 B
Python
Raw Normal View History

from logging import Logger
2022-07-10 16:10:52 +00:00
from time import strftime
from feedparser import FeedParserDict, parse
2022-05-29 20:17:03 +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 16:10:52 +00:00
self.date = strftime("%d.%m.%Y %H:%M", item.published_parsed)
else:
self.date = None
2022-05-29 20:17:03 +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
class RssReader:
def __init__(self, logger: Logger):
self.log: Logger = logger
self.log.debug('RssReader.__init__(logger=%s)', logger)
def get_feed(self, url: str) -> Feed:
self.log.debug('get_feed(url=\'%s\')', url)
return Feed(url, parse(url))