tg_rss_bot/update_manager.py
mitsuha_s 94d77a42f4
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing
implement update manager
2022-06-11 22:51:39 +00:00

45 lines
1.3 KiB
Python

from rss import RssReader, FeedItem
from database import Database
from telegram import Notifier
class UpdateManager:
def __init__(self, database: Database, notifier: Notifier, rss_reader: RssReader) -> None:
self.database = database
self.notifier = notifier
self.rss_reader = rss_reader
def update(self):
feeds = self.database.find_feeds()
for feed_id, feed_url in feeds:
new_items = self.rss_reader.get_feed(feed_url).items
old_items = self.database.find_feed_items(feed_id)
diff = self.__calculate_difference(new_items, old_items)
if not diff:
continue
chat_ids = self.database.find_feed_subscribers(feed_id)
chat_ids = list(map(lambda x: x[0], chat_ids))
self.notifier.send_updates(chat_ids, diff)
self.database.update_feed_items(feed_id, new_items)
def __calculate_difference(self, new_items: list[FeedItem], old_items: list[tuple]) -> list[FeedItem]:
if not old_items:
return new_items
diff = []
urls = list(map(lambda x: x[1], old_items))
for item in new_items:
if item.url not in urls:
diff.append(item)
return diff