feeds: enable updating feeds at different intervals

This commit is contained in:
ducklet 2020-11-12 23:44:06 +01:00
parent a96704e1fa
commit 76e7969209
5 changed files with 76 additions and 40 deletions

View file

@ -12,7 +12,6 @@ class Feeder:
def __init__(self, store: Store, feeds: Iterable[Feed] = None):
self.feeds: Dict[str, Feed] = {}
self.store: Store = store
self.news: Mapping[FeedId, Set[PostId]] = {}
if feeds:
self.add_feeds(feeds)
@ -22,17 +21,26 @@ class Feeder:
self.store.sync_feeds(self.feeds)
log.debug("Active feeds: %s", ", ".join(self.feeds.keys()))
async def update_all(self) -> Mapping[FeedId, Set[PostId]]:
new_post_ids = self.news = dict(
async def update_all(self, feed_ids=None) -> Mapping[FeedId, Set[PostId]]:
"""Update all feeds.
Automatically persists any new posts in storage.
"""
feeds = {i: self.feeds[i] for i in feed_ids} if feed_ids else self.feeds
new_post_ids = dict(
zip(
self.feeds,
await asyncio.gather(*(self.update(id) for id in self.feeds)),
feeds,
await asyncio.gather(*(self._update(id) for id in feeds)),
)
)
self.store.sync_feeds(self.feeds)
self.store.sync_feeds(feeds)
return new_post_ids
async def update(self, feed_id) -> Set[PostId]:
async def _update(self, feed_id) -> Set[PostId]:
"""Update a single feed.
Does not persist any changes.
"""
feed = self.feeds[feed_id]
post_ids = feed.post_ids
feed.load()