Skip to content

Commit

Permalink
Merge pull request #51 from dkozinn:dkozinn/issue50
Browse files Browse the repository at this point in the history
Modify modq and post bots to take sub name as input
  • Loading branch information
dkozinn authored Jan 1, 2024
2 parents e775b85 + b9882ae commit e4f86e3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/nasamodqbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import praw
import prawcore
from discord_webhook import DiscordWebhook
from nasautils.utilities import get_sub

try:
from q_signals import Q_WHITE, send_signal # type: ignore
Expand All @@ -17,7 +18,7 @@
except ModuleNotFoundError:
GOT_Q = False

SUB = "nasa"
SUB = get_sub()


def main():
Expand All @@ -42,7 +43,7 @@ def main():
logger.addHandler(handler)

subreddit = reddit.subreddit(SUB)
logging.info("Entering main loop")
logging.info("Entering main loop for r/%s", SUB)

for submission in subreddit.mod.stream.modqueue():
try:
Expand All @@ -51,11 +52,11 @@ def main():
title = f"Comment on post '{submission.link_title}'"
link = f"https://reddit.com{submission.permalink}"
logging.info(
"New modqueue entry from %s: %s (%s)", submission.author, title, link
"New modqueue entry in r/%s from %s: %s (%s)", SUB, submission.author, title, link
)
webhook = DiscordWebhook(
discord_webhook,
username="NASA Modqueue Bot",
username=f"{SUB} Modqueue Bot",
content=f"Modqueue: [{title} by {submission.author}]({link})",
)
webhook.execute()
Expand Down
9 changes: 5 additions & 4 deletions src/nasapostbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import praw
import prawcore
from discord_webhook import DiscordWebhook
from nasautils.utilities import get_sub


SUB = "nasa"
SUB = get_sub()


def main():
Expand All @@ -34,15 +35,15 @@ def main():
logger.addHandler(handler)

subreddit = reddit.subreddit(SUB)
logging.info("Entering main loop")
logging.info("Entering main loop for r/%s", SUB)
for submission in subreddit.stream.submissions(skip_existing=True):
reddit_url = "https://reddit.com" + submission.permalink
logging.info(
"New post by %s: %s (%s)", submission.author, submission.title, reddit_url
"New post in r/%s by %s: %s (%s)", SUB, submission.author, submission.title, reddit_url
)
webhook = DiscordWebhook(
discord_webhook,
username="nasapostbot",
username=f"{SUB} Post Bot",
content=f"[{submission.title}]({reddit_url})",
)
webhook.execute()
Expand Down
15 changes: 15 additions & 0 deletions src/nasautils/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/python3

"""Misc utilities for r/nasa bot"""

import sys


def get_sub():
"""Get and return subreddit name"""
if len(sys.argv) != 2:
sub = 'nasa'
else:
sub = sys.argv[1:][0].lower()

return sub

0 comments on commit e4f86e3

Please sign in to comment.