1
0
Fork 0

Add midnight mode

This commit is contained in:
Casper V. Kristensen 2022-05-21 20:39:12 +02:00
parent 88d2dffac4
commit e00b09c5d8
2 changed files with 22 additions and 2 deletions

View file

@ -3,6 +3,7 @@
# immediately : Immediately generate and submit post to own subreddit. PM reddit notify_users with link to post. This # immediately : Immediately generate and submit post to own subreddit. PM reddit notify_users with link to post. This
# mode is useful for cron jobs, e.g. generating at midnight: '0 0 * * * /usr/local/bin/python3.7 -m dailyreleases'. # mode is useful for cron jobs, e.g. generating at midnight: '0 0 * * * /usr/local/bin/python3.7 -m dailyreleases'.
# reply : Listen for reddit PMs; on receipt, generate and submit post to own subreddit. Reply with link to post. # reply : Listen for reddit PMs; on receipt, generate and submit post to own subreddit. Reply with link to post.
# midnight : Like 'immediately', but run continuously, generating and submitting post at midnight every day.
# test : Generate and print to log and console. Nothing is posted to reddit. # test : Generate and print to log and console. Nothing is posted to reddit.
mode = test mode = test
@ -24,8 +25,8 @@ password = xxxxxxx
# List of users who are authorized to start the generation by PM'ing the bot. Only applies to 'reply' mode. # List of users who are authorized to start the generation by PM'ing the bot. Only applies to 'reply' mode.
authorized_users = spez,Deimorz,kn0thing authorized_users = spez,Deimorz,kn0thing
# List of users who should receive a PM on generation. Only applies to 'immediately' mode, since the sender will receive # List of users who should receive a PM on generation. Only applies to 'immediately' and 'midnight' mode, since the
# the PM in 'reply' mode. # sender will receive the PM in 'reply' mode.
notify_users = chooter,alienth notify_users = chooter,alienth
# Reddit perceives PMs with many links as spam, so the bot posts the code for the generated post in its own subreddit # Reddit perceives PMs with many links as spam, so the bot posts the code for the generated post in its own subreddit

View file

@ -1,4 +1,6 @@
import logging import logging
from datetime import time, datetime, timedelta
from time import sleep
import prawcore import prawcore
@ -28,6 +30,21 @@ def listen_inbox() -> None:
print("Exiting (KeyboardInterrupt)") print("Exiting (KeyboardInterrupt)")
break break
def at_midnight() -> None:
while True:
try:
now = datetime.now()
midnight = datetime.combine(now + timedelta(days=1), time.min)
until_midnight = midnight - now
logger.info(f"Waiting {until_midnight} until midnight..")
sleep(until_midnight.total_seconds())
generate(post=True, pm_recipients=CONFIG["reddit"]["notify_users"].split(","))
except Exception as e:
logger.exception(e)
except KeyboardInterrupt:
print("Exiting (KeyboardInterrupt)")
break
def main() -> None: def main() -> None:
try: try:
@ -39,6 +56,8 @@ def main() -> None:
generate(post=False) generate(post=False)
if mode == "immediately": if mode == "immediately":
generate(post=True, pm_recipients=CONFIG["reddit"]["notify_users"].split(",")) generate(post=True, pm_recipients=CONFIG["reddit"]["notify_users"].split(","))
if mode == "midnight":
at_midnight()
if mode == "reply": if mode == "reply":
listen_inbox() listen_inbox()
except Exception as e: except Exception as e: