From df4cafadbd50ba35934bd2a9134b67f14cf31dfa Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Thu, 16 Aug 2018 12:26:51 +0200 Subject: [PATCH] Immediately generate and post instead of waiting for PM. --- dailyreleases/main.py | 41 ++++------------------------------------- 1 file changed, 4 insertions(+), 37 deletions(-) diff --git a/dailyreleases/main.py b/dailyreleases/main.py index 3718c37..be69b44 100644 --- a/dailyreleases/main.py +++ b/dailyreleases/main.py @@ -1,4 +1,3 @@ -import inspect import json import logging import re @@ -8,7 +7,6 @@ import time from collections import defaultdict from datetime import datetime, timedelta -import prawcore import requests_cache from dailyreleases import config, __version__, util @@ -48,28 +46,7 @@ class DailyReleasesBot(object): self.generate() return - # Retry forever because PRAW (or reddit) sucks - while True: - try: - logger.info("Listening on reddit inbox stream") - for message in self.reddit.praw.inbox.stream(): - if message.author not in self.config["reddit"]["authorized_users"].split(","): - logger.info("Discarding PM from %s: not in authorized users", message.author) - continue - - self.generate(pm_recipient=message.author.name) - message.mark_read() - - # Clean requests cache after each successful generation so it doesn't grow indefinitely - self.cache.remove_expired_responses() - - except prawcore.PrawcoreException as e: - logger.warning("PrawcoreException: %s", e) - logger.info("Restarting inbox listener..") - - except KeyboardInterrupt: - print("Exiting (KeyboardInterrupt)") - break + self.generate(do_post=True) def find_store_links(self, game_name) -> dict: links = {} @@ -371,7 +348,7 @@ class DailyReleasesBot(object): logger.debug("Generated post:\n%s", post_str) return post_str - def generate(self, pm_recipient=None): + def generate(self, do_post=False): logger.info("-------------------------------------------------------------------------------------------------") start_time = time.time() @@ -386,7 +363,7 @@ class DailyReleasesBot(object): # The date of the post changes at midday instead of midnight to allow calling script after 00:00 title = "Daily Releases ({})".format((datetime.today() - timedelta(hours=12)).strftime("%B %-d, %Y")) - if pm_recipient: + if do_post: # Post to bot's subreddit source_post = self.reddit.submit_post(f"{title} - Source", source, self.config["reddit"]["bot_subreddit"]) preview_post = self.reddit.submit_post(title, post, self.config["reddit"]["bot_subreddit"]) @@ -395,17 +372,7 @@ class DailyReleasesBot(object): source_post.mod.approve() preview_post.mod.approve() - # Write and send PM to whoever initiated the generation - msg = inspect.cleandoc( - f""" - [Preview]({preview_post.url}) - [Source]({source_post.url}) - Failed: {", ".join(failed_dirnames)} - """ - ) - self.reddit.praw.redditor(pm_recipient).message(title, msg) - - self.save_already_posted(already_posted) + self.save_already_posted(already_posted) logger.info("Execution took %s seconds", int(time.time() - start_time)) logger.info("-------------------------------------------------------------------------------------------------")