implement update manager
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing

This commit is contained in:
mitsuha_s 2022-06-11 22:51:39 +00:00
parent 629db4c25d
commit 94d77a42f4
4 changed files with 56 additions and 5 deletions

1
.gitignore vendored
View file

@ -3,6 +3,7 @@
# Python
/.venv
/__pycache__
# Database
/*.db

View file

@ -1,6 +1,7 @@
import sqlite3
from exceptions import DisplayableException
from rss import FeedItem
class Database:
@ -97,6 +98,11 @@ class Database:
return 0
return int(row[0])
def find_feed_subscribers(self, feed_id: int) -> list:
"""Return feed subscribers"""
self.cur.execute('SELECT telegram_id FROM users WHERE id IN (SELECT user_id FROM subscriptions WHERE feed_id = ?)', [feed_id])
return self.cur.fetchall()
def find_feeds(self) -> list:
"""Get a list of feeds."""
self.cur.execute('SELECT * FROM feeds')
@ -113,10 +119,10 @@ class Database:
self.cur.execute('SELECT * FROM feeds_last_items WHERE feed_id = ?', [feed_id])
return self.cur.fetchall()
def update_feed_items(self, feed_id: int, new_items: list) -> None:
def update_feed_items(self, feed_id: int, new_items: list[FeedItem]) -> None:
"""Replace last feed items with a list items that receive."""
for i in range(len(new_items)):
new_items[i] = (feed_id,) + new_items[i]
new_items[i] = [feed_id] + list(new_items[i].__dict__.values())
self.cur.execute('DELETE FROM feeds_last_items WHERE feed_id = ?', [feed_id])
self.cur.executemany(

6
rss.py
View file

@ -1,19 +1,19 @@
import feedparser
class FeedItem():
class FeedItem:
def __init__(self, url: str, title: str, description: str, date: str) -> None:
self.url = url
self.title = title
self.description = description
self.date = date
class Feed():
class Feed:
def __init__(self, url: str, items: list[FeedItem]) -> None:
self.url = url
self.items = items
class RssReader():
class RssReader:
def get_feed(self, url: str) -> Feed:
f = feedparser.parse(url)
items = self.__get_items(f.entries)

44
update_manager.py Normal file
View file

@ -0,0 +1,44 @@
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