From ddd922c71719694d99b795a1049e12b825883976 Mon Sep 17 00:00:00 2001 From: Kris Knigga Date: Mon, 22 Jul 2024 13:42:09 -0500 Subject: [PATCH] Add better error handling --- pyproject.toml | 2 +- rflying_tower_bot/__init__.py | 2 +- rflying_tower_bot/modlog.py | 54 ++++++++++++++++++------------ rflying_tower_bot/post_stream.py | 57 ++++++++++++++++++++------------ tests/test_rflying_tower_bot.py | 2 +- 5 files changed, 71 insertions(+), 46 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 84d12d7..257aa09 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "rflying_tower_bot" -version = "0.3.0" +version = "0.3.1" description = "" authors = ["Kris Knigga "] diff --git a/rflying_tower_bot/__init__.py b/rflying_tower_bot/__init__.py index 1a45a6d..af285d6 100644 --- a/rflying_tower_bot/__init__.py +++ b/rflying_tower_bot/__init__.py @@ -1,3 +1,3 @@ """rflying_tower_bot package.""" -__version__ = "0.3.0" +__version__ = "0.3.1" diff --git a/rflying_tower_bot/modlog.py b/rflying_tower_bot/modlog.py index 91af9a2..f01bcf7 100644 --- a/rflying_tower_bot/modlog.py +++ b/rflying_tower_bot/modlog.py @@ -130,25 +130,35 @@ async def watch_modlog(self) -> None: self.config.subreddit_name ) self.log.info("Starting watch of %s's mod log", subreddit) - async for modlog_entry in subreddit.mod.stream.log(skip_existing=True): - self.log.debug( - "Found new modlog entry: %s did %s (%s) to target %s", - modlog_entry.mod, - modlog_entry.action, - modlog_entry.details, - modlog_entry.target_permalink, - ) - if ( - modlog_entry.action == "editflair" - and modlog_entry.target_fullname - and modlog_entry.target_fullname[:2] == "t3" - ): - target_name: str = modlog_entry.target_fullname[3:] - post: Submission = await self.config.reddit.submission(id=target_name) - await self.check_post_flair(post) - - if ( - modlog_entry.action == "wikirevise" - and modlog_entry.details == f"Page {self.config.rules_wiki_page} edited" - ): - await self.config.update_rules() + while True: + try: + async for modlog_entry in subreddit.mod.stream.log(skip_existing=True): + self.log.debug( + "Found new modlog entry: %s did %s (%s) to target %s", + modlog_entry.mod, + modlog_entry.action, + modlog_entry.details, + modlog_entry.target_permalink, + ) + if ( + modlog_entry.action == "editflair" + and modlog_entry.target_fullname + and modlog_entry.target_fullname[:2] == "t3" + ): + target_name: str = modlog_entry.target_fullname[3:] + post: Submission = await self.config.reddit.submission( + id=target_name + ) + await self.check_post_flair(post) + + if ( + modlog_entry.action == "wikirevise" + and modlog_entry.details + == f"Page {self.config.rules_wiki_page} edited" + ): + await self.config.update_rules() + except KeyboardInterrupt: + self.log.info("Caught keyboard interrupt, exiting modlog watcher") + break + except Exception as e: + self.log.error("Error in modlog watcher: %s", e) diff --git a/rflying_tower_bot/post_stream.py b/rflying_tower_bot/post_stream.py index 4293db4..99f8ba8 100644 --- a/rflying_tower_bot/post_stream.py +++ b/rflying_tower_bot/post_stream.py @@ -32,24 +32,39 @@ async def watch_poststream(self) -> None: self.config.subreddit_name ) self.log.info("Watching the post stream for new posts in %s", subreddit) - async for post in subreddit.stream.submissions(): - if await self.config.history.check(post.permalink, "save_post_body") > 0: - self.log.info("Skipping post %s, already processed", post.permalink) - continue - - self.log.info("New post from %s: %s", post.author, post.permalink) - if post.selftext != "": - comment_text = f"This is a copy of the original post body for posterity:\n\n --- \n{post.selftext}" - c: Comment | None = await post.reply( - self.utilities.format_comment(comment_text) - ) - if not c: - self.log.error( - "Making comment on %s seems to have failed", str(post) - ) - return - await c.mod.distinguish(sticky=False) - await self.config.history.add(post.permalink, "save_post_body") - self.log.info( - "Comment created with post body for posterity: %s", c.permalink - ) + while True: + try: + async for post in subreddit.stream.submissions(): + if ( + await self.config.history.check( + post.permalink, "save_post_body" + ) + > 0 + ): + self.log.info( + "Skipping post %s, already processed", post.permalink + ) + continue + + self.log.info("New post from %s: %s", post.author, post.permalink) + if post.selftext != "": + comment_text = f"This is a copy of the original post body for posterity:\n\n --- \n{post.selftext}" + c: Comment | None = await post.reply( + self.utilities.format_comment(comment_text) + ) + if not c: + self.log.error( + "Making comment on %s seems to have failed", str(post) + ) + return + await c.mod.distinguish(sticky=False) + await self.config.history.add(post.permalink, "save_post_body") + self.log.info( + "Comment created with post body for posterity: %s", + c.permalink, + ) + except KeyboardInterrupt: + self.log.info("Caught keyboard interrupt, exiting post stream watcher") + break + except Exception as e: + self.log.error("Error in post stream watcher: %s", e) diff --git a/tests/test_rflying_tower_bot.py b/tests/test_rflying_tower_bot.py index 01817d5..830c644 100644 --- a/tests/test_rflying_tower_bot.py +++ b/tests/test_rflying_tower_bot.py @@ -5,4 +5,4 @@ def test_version(): """Test the version of the rflying_tower_bot package.""" - assert __version__ == "0.3.0" + assert __version__ == "0.3.1"