Created
February 7, 2020 04:04
-
-
Save dev4Fun/91d650acbd08653bcda3b54597251f3a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # bot.py | |
| import logging | |
| import random | |
| import praw | |
| from store import dump_pickled, read_pickled_set | |
| submission_reply_list = ["Nice", "up up", "nice job, friend", | |
| "I like your creativity", "Very nice"] | |
| class RedditBot: | |
| def __init__(self, credentials: dict): | |
| self.credentials = credentials | |
| username = credentials['username'] | |
| self.credentials['user_agent'] = f"testscript by /u/{username}" | |
| self.reddit_client = praw.Reddit(**self.credentials) | |
| self.passed_submissions = read_pickled_set(f"submissions-{username}.pickle") | |
| @property | |
| def username(self): | |
| return self.credentials['username'] | |
| def work_on_subreddit(self, subreddit: str, **generator_kwargs): | |
| if 'limit' not in generator_kwargs: | |
| generator_kwargs['limit'] = 60 | |
| try: | |
| submissions = list(self.reddit_client.subreddit(subreddit).new(**generator_kwargs)) | |
| for submission in submissions: | |
| self._process_submission(submission) | |
| finally: | |
| dump_pickled(self.passed_submissions, f"submissions-{self.username}.pickle") | |
| def _process_submission(self, submission): | |
| submission_id = submission.id | |
| if submission_id not in self.passed_submissions: | |
| comments = submission.comments.list() | |
| if len(comments) < 12: | |
| submission_reply = random.choice(submission_reply_list) | |
| submission.upvote() | |
| submission.reply(submission_reply) | |
| logging.info(f"Processed '{submission.title}'") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment