Immediately generate and post instead of waiting for PM.
This commit is contained in:
parent
3b993ab811
commit
df4cafadbd
|
@ -1,4 +1,3 @@
|
||||||
import inspect
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
@ -8,7 +7,6 @@ import time
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
import prawcore
|
|
||||||
import requests_cache
|
import requests_cache
|
||||||
|
|
||||||
from dailyreleases import config, __version__, util
|
from dailyreleases import config, __version__, util
|
||||||
|
@ -48,28 +46,7 @@ class DailyReleasesBot(object):
|
||||||
self.generate()
|
self.generate()
|
||||||
return
|
return
|
||||||
|
|
||||||
# Retry forever because PRAW (or reddit) sucks
|
self.generate(do_post=True)
|
||||||
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
|
|
||||||
|
|
||||||
def find_store_links(self, game_name) -> dict:
|
def find_store_links(self, game_name) -> dict:
|
||||||
links = {}
|
links = {}
|
||||||
|
@ -371,7 +348,7 @@ class DailyReleasesBot(object):
|
||||||
logger.debug("Generated post:\n%s", post_str)
|
logger.debug("Generated post:\n%s", post_str)
|
||||||
return post_str
|
return post_str
|
||||||
|
|
||||||
def generate(self, pm_recipient=None):
|
def generate(self, do_post=False):
|
||||||
logger.info("-------------------------------------------------------------------------------------------------")
|
logger.info("-------------------------------------------------------------------------------------------------")
|
||||||
start_time = time.time()
|
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
|
# 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"))
|
title = "Daily Releases ({})".format((datetime.today() - timedelta(hours=12)).strftime("%B %-d, %Y"))
|
||||||
|
|
||||||
if pm_recipient:
|
if do_post:
|
||||||
# Post to bot's subreddit
|
# Post to bot's subreddit
|
||||||
source_post = self.reddit.submit_post(f"{title} - Source", source, self.config["reddit"]["bot_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"])
|
preview_post = self.reddit.submit_post(title, post, self.config["reddit"]["bot_subreddit"])
|
||||||
|
@ -395,16 +372,6 @@ class DailyReleasesBot(object):
|
||||||
source_post.mod.approve()
|
source_post.mod.approve()
|
||||||
preview_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("Execution took %s seconds", int(time.time() - start_time))
|
||||||
|
|
Reference in a new issue