1
0
Fork 0

Immediately generate and post instead of waiting for PM.

This commit is contained in:
Casper V. Kristensen 2018-08-16 12:26:51 +02:00
parent 3b993ab811
commit df4cafadbd
Signed by: caspervk
GPG key ID: B1156723DB3BDDA8

View file

@ -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,16 +372,6 @@ 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)
logger.info("Execution took %s seconds", int(time.time() - start_time))