refactor a bit in rss.py
This commit is contained in:
parent
b2c49a583a
commit
94c1447093
|
@ -120,7 +120,7 @@ class Database:
|
|||
self.cur.execute('SELECT * FROM feeds_last_items WHERE feed_id = ?', [feed_id])
|
||||
return self.cur.fetchall()
|
||||
|
||||
def find_feed_items_url(self, feed_id: int) -> list[str]:
|
||||
def find_feed_items_urls(self, feed_id: int) -> list[str]:
|
||||
"""Return urls last feed items"""
|
||||
items = self.find_feed_items(feed_id)
|
||||
if not items:
|
||||
|
|
18
rss.py
18
rss.py
|
@ -1,24 +1,20 @@
|
|||
import feedparser
|
||||
from feedparser import FeedParserDict, parse
|
||||
|
||||
|
||||
class FeedItem:
|
||||
def __init__(self, item: feedparser.util.FeedParserDict) -> None:
|
||||
def __init__(self, item: FeedParserDict) -> None:
|
||||
self.url = item.get('link', '')
|
||||
self.title = item.get('title', '')
|
||||
self.description = item.get('summary', '')
|
||||
self.date = item.get('published', '')
|
||||
|
||||
class Feed:
|
||||
def __init__(self, url: str, feed: feedparser.util.FeedParserDict) -> None:
|
||||
def __init__(self, url: str, feed: FeedParserDict) -> None:
|
||||
self.url = url
|
||||
self.items = self.__get_items(feed.entries)
|
||||
|
||||
def __get_items(self, items: list[feedparser.util.FeedParserDict]) -> list[FeedItem]:
|
||||
list_items = []
|
||||
for item in items:
|
||||
list_items.append(FeedItem(item))
|
||||
return list_items
|
||||
self.items = []
|
||||
for item in feed:
|
||||
self.items.append(FeedItem(item))
|
||||
|
||||
class RssReader:
|
||||
def get_feed(self, url: str) -> Feed:
|
||||
return Feed(url, feedparser.parse(url))
|
||||
return Feed(url, parse(url))
|
||||
|
|
|
@ -14,9 +14,9 @@ class UpdateManager:
|
|||
|
||||
for feed_id, feed_url in feeds:
|
||||
new_items = self.rss_reader.get_feed(feed_url).items
|
||||
old_items_url = self.database.find_feed_items_url(feed_id)
|
||||
old_items_urls = self.database.find_feed_items_urls(feed_id)
|
||||
|
||||
diff = self.__calculate_difference(new_items, old_items_url)
|
||||
diff = self.__calculate_difference(new_items, old_items_urls)
|
||||
|
||||
if not diff:
|
||||
continue
|
||||
|
@ -25,14 +25,14 @@ class UpdateManager:
|
|||
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_url: list[str]) -> list[FeedItem]:
|
||||
if not old_items_url:
|
||||
def __calculate_difference(self, new_items: list[FeedItem], old_items_urls: list[str]) -> list[FeedItem]:
|
||||
if not old_items_urls:
|
||||
return new_items
|
||||
|
||||
diff = []
|
||||
|
||||
for item in new_items:
|
||||
if item.url not in old_items_url:
|
||||
if item.url not in old_items_urls:
|
||||
diff.append(item)
|
||||
|
||||
return diff
|
||||
|
|
Loading…
Reference in a new issue