implement update manager
This commit is contained in:
parent
629db4c25d
commit
94d77a42f4
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
# Python
|
# Python
|
||||||
/.venv
|
/.venv
|
||||||
|
/__pycache__
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
/*.db
|
/*.db
|
10
database.py
10
database.py
|
@ -1,6 +1,7 @@
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
from exceptions import DisplayableException
|
from exceptions import DisplayableException
|
||||||
|
from rss import FeedItem
|
||||||
|
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
|
@ -97,6 +98,11 @@ class Database:
|
||||||
return 0
|
return 0
|
||||||
return int(row[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:
|
def find_feeds(self) -> list:
|
||||||
"""Get a list of feeds."""
|
"""Get a list of feeds."""
|
||||||
self.cur.execute('SELECT * FROM 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])
|
self.cur.execute('SELECT * FROM feeds_last_items WHERE feed_id = ?', [feed_id])
|
||||||
return self.cur.fetchall()
|
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."""
|
"""Replace last feed items with a list items that receive."""
|
||||||
for i in range(len(new_items)):
|
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.execute('DELETE FROM feeds_last_items WHERE feed_id = ?', [feed_id])
|
||||||
self.cur.executemany(
|
self.cur.executemany(
|
||||||
|
|
6
rss.py
6
rss.py
|
@ -1,19 +1,19 @@
|
||||||
import feedparser
|
import feedparser
|
||||||
|
|
||||||
|
|
||||||
class FeedItem():
|
class FeedItem:
|
||||||
def __init__(self, url: str, title: str, description: str, date: str) -> None:
|
def __init__(self, url: str, title: str, description: str, date: str) -> None:
|
||||||
self.url = url
|
self.url = url
|
||||||
self.title = title
|
self.title = title
|
||||||
self.description = description
|
self.description = description
|
||||||
self.date = date
|
self.date = date
|
||||||
|
|
||||||
class Feed():
|
class Feed:
|
||||||
def __init__(self, url: str, items: list[FeedItem]) -> None:
|
def __init__(self, url: str, items: list[FeedItem]) -> None:
|
||||||
self.url = url
|
self.url = url
|
||||||
self.items = items
|
self.items = items
|
||||||
|
|
||||||
class RssReader():
|
class RssReader:
|
||||||
def get_feed(self, url: str) -> Feed:
|
def get_feed(self, url: str) -> Feed:
|
||||||
f = feedparser.parse(url)
|
f = feedparser.parse(url)
|
||||||
items = self.__get_items(f.entries)
|
items = self.__get_items(f.entries)
|
||||||
|
|
44
update_manager.py
Normal file
44
update_manager.py
Normal 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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue