-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add post body saving feature, bump version.
- Loading branch information
Showing
3 changed files
with
54 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "rflying_tower_bot" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
description = "" | ||
authors = ["Kris Knigga <[email protected]>"] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""A module to react to new posts.""" | ||
|
||
import logging | ||
|
||
from asyncpraw.models import Comment, Subreddit | ||
|
||
from rflying_tower_bot.config import BotConfig | ||
from rflying_tower_bot.utilities import Utilities | ||
|
||
|
||
class PostStream: | ||
"""A class to react to new posts.""" | ||
|
||
def __init__(self, config: BotConfig) -> None: | ||
""" | ||
Create an instance of PostStream. | ||
Args: | ||
---- | ||
config (BotConfig): See config.BotConfig | ||
""" | ||
self.log: logging.Logger = logging.getLogger( | ||
f"{__name__}.{self.__class__.__name__}" | ||
) | ||
self.config = config | ||
self.utilities = Utilities(config) | ||
|
||
async def watch_poststream(self) -> None: | ||
"""Watch the post stream and react to new posts.""" | ||
subreddit: Subreddit = await self.config.reddit.subreddit( | ||
self.config.subreddit_name | ||
) | ||
self.log.info("Watching the post stream for new posts in %s", subreddit) | ||
async for post in subreddit.stream.submissions(): | ||
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) | ||
self.log.info( | ||
"Comment created with post body for posterity: %s", c.permalink | ||
) |