From 79391d1cc509ca23605dddf84a9afece1e57dc15 Mon Sep 17 00:00:00 2001 From: Kumi Date: Sun, 16 Jun 2024 08:28:29 +0200 Subject: [PATCH] feat(bot): support backfilling RSS feed history Added the ability to backfill messages from an RSS feed upon adding it to a room. Users can now add the `--backfill` flag when using the `addfeed` command to retrieve and post historical entries from the feed. This enhancement improves user experience by ensuring no important feed information is missed. --- pyproject.toml | 2 +- src/matrix_rssbot/classes/bot.py | 13 ++++++++ src/matrix_rssbot/classes/commands/addfeed.py | 31 +++++++++++++++++-- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 23f80e8..7dd76c3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ allow-direct-references = true [project] name = "matrix-rssbot" -version = "0.1.4" +version = "0.1.5" authors = [{ name = "Private.coffee Team", email = "support@private.coffee" }] diff --git a/src/matrix_rssbot/classes/bot.py b/src/matrix_rssbot/classes/bot.py index 2e9cce2..8028518 100644 --- a/src/matrix_rssbot/classes/bot.py +++ b/src/matrix_rssbot/classes/bot.py @@ -582,6 +582,19 @@ class RSSBot: ) < self.loop_duration: await asyncio.sleep(self.loop_duration - time_taken) + async def backfill_feed(self, room: MatrixRoom, url: str): + self.logger.log(f"Backfilling feed {url} in room {room.room_id}", "debug") + + feed_content = await self.fetch_feed(url) + + for entry in feed_content.entries: + entry_message = f"__{feed_content.feed.title}: {entry.title}__\n\n{entry.description}\n\n{entry.link}" + await self.send_message( + room, + entry_message, + (await self.get_event_type_for_room(room)) == "notice", + ) + async def run(self): """Start the bot.""" diff --git a/src/matrix_rssbot/classes/commands/addfeed.py b/src/matrix_rssbot/classes/commands/addfeed.py index d4732f0..7fa081d 100644 --- a/src/matrix_rssbot/classes/commands/addfeed.py +++ b/src/matrix_rssbot/classes/commands/addfeed.py @@ -8,7 +8,24 @@ import feedparser async def command_addfeed(room: MatrixRoom, event: RoomMessageText, bot): - url = event.body.split()[2] + args = event.body.split()[2:] + + if not args: + await bot.send_message(room, "Please provide a feed URL to add.", True) + return + + if "--backfill" in args: + backfill = True + args.remove("--backfill") + + else: + backfill = False + + if len(args) != 1: + await bot.send_message(room, "Please provide only one feed URL.", True) + return + + url = args[0] bot.logger.log(f"Adding new feed to room {room.room_id}") @@ -67,8 +84,18 @@ async def command_addfeed(room: MatrixRoom, event: RoomMessageText, bot): return await bot.send_message(room, f"Added {url} to this room's feeds.", True) + + if backfill: + await bot.send_message( + room, + "Backfilling messages from the feed. This may take a while, depending on the feed size.", + True, + ) + + await bot.backfill_feed(room, url) + except Exception as e: await bot.send_message( room, "Sorry, something went wrong. Please try again.", True ) - bot.logger.log(f"Error adding feed to room {room.room_id}: {e}", "error") \ No newline at end of file + bot.logger.log(f"Error adding feed to room {room.room_id}: {e}", "error")