2022-06-11 22:51:39 +00:00
|
|
|
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
|
2022-06-12 13:02:18 +00:00
|
|
|
old_items_url = self.database.find_feed_items_url(feed_id)
|
2022-06-11 22:51:39 +00:00
|
|
|
|
2022-06-12 13:02:18 +00:00
|
|
|
diff = self.__calculate_difference(new_items, old_items_url)
|
2022-06-11 22:51:39 +00:00
|
|
|
|
|
|
|
if not diff:
|
|
|
|
continue
|
|
|
|
|
|
|
|
chat_ids = self.database.find_feed_subscribers(feed_id)
|
|
|
|
self.notifier.send_updates(chat_ids, diff)
|
|
|
|
self.database.update_feed_items(feed_id, new_items)
|
|
|
|
|
2022-06-12 13:02:18 +00:00
|
|
|
def __calculate_difference(self, new_items: list[FeedItem], old_items_url: list[str]) -> list[FeedItem]:
|
|
|
|
if not old_items_url:
|
2022-06-11 22:51:39 +00:00
|
|
|
return new_items
|
|
|
|
|
|
|
|
diff = []
|
|
|
|
|
|
|
|
for item in new_items:
|
2022-06-12 13:02:18 +00:00
|
|
|
if item.url not in old_items_url:
|
2022-06-11 22:51:39 +00:00
|
|
|
diff.append(item)
|
|
|
|
|
|
|
|
return diff
|