From 71c317da23b02b4228376ebeab35e0a405a687e9 Mon Sep 17 00:00:00 2001 From: Callum Leslie Date: Tue, 9 Aug 2022 19:45:31 +0100 Subject: [PATCH 1/3] feat: meaningful error message for bad credentials --- reddit/subreddit.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 716a7fa..06538b2 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -1,5 +1,7 @@ import re +from prawcore.exceptions import ResponseException + from utils import settings import praw from praw.models import MoreComments @@ -29,14 +31,21 @@ def get_subreddit_threads(POST_ID: str): username = settings.config["reddit"]["creds"]["username"] if str(username).casefold().startswith("u/"): username = username[2:] - reddit = praw.Reddit( - client_id=settings.config["reddit"]["creds"]["client_id"], - client_secret=settings.config["reddit"]["creds"]["client_secret"], - user_agent="Accessing Reddit threads", - username=username, - passkey=passkey, - check_for_async=False, - ) + try: + reddit = praw.Reddit( + client_id=settings.config["reddit"]["creds"]["client_id"], + client_secret=settings.config["reddit"]["creds"]["client_secret"], + user_agent="Accessing Reddit threads", + username=username, + passkey=passkey, + check_for_async=False, + ) + except ResponseException as e: + match e.response.status_code: + case 401: + print("Invalid credentials - please check them in config.toml") + except: + print("Something went wrong...") # Ask user for subreddit input print_step("Getting subreddit threads...") From 968028c5415dc73e3c2c5c337d0974ebc82c5635 Mon Sep 17 00:00:00 2001 From: Callum Leslie Date: Tue, 9 Aug 2022 20:05:06 +0100 Subject: [PATCH 2/3] chore: remove comment reference to .env --- reddit/subreddit.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 06538b2..11b93af 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -66,9 +66,7 @@ def get_subreddit_threads(POST_ID: str): subreddit_choice = sub if str(subreddit_choice).casefold().startswith("r/"): # removes the r/ from the input subreddit_choice = subreddit_choice[2:] - subreddit = reddit.subreddit( - subreddit_choice - ) # Allows you to specify in .env. Done for automation purposes. + subreddit = reddit.subreddit(subreddit_choice) if POST_ID: # would only be called if there are multiple queued posts submission = reddit.submission(id=POST_ID) From 74a3707f5543b88cd1ad4bfabe1d613eff4b2b4b Mon Sep 17 00:00:00 2001 From: Simon <65854503+StopmotionSimonYT@users.noreply.github.com> Date: Tue, 9 Aug 2022 21:23:03 +0200 Subject: [PATCH 3/3] Config.toml multiple subreddits --- utils/.config.template.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/.config.template.toml b/utils/.config.template.toml index 31ae9aa..adbaed0 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -8,7 +8,7 @@ password = { optional = false, nmin = 8, explanation = "The password of your red [reddit.thread] random = { optional = true, options = [true, false,], default = false, type = "bool", explanation = "If set to no, it will ask you a thread link to extract the thread, if yes it will randomize it. Default: 'False'", example = "True" } -subreddit = { optional = false, regex = "[_0-9a-zA-Z]+$", nmin = 3, explanation = "What subreddit to pull posts from, the name of the sub, not the URL", example = "AskReddit", oob_error = "A subreddit name HAS to be between 3 and 20 characters" } +subreddit = { optional = false, regex = "[_0-9a-zA-Z]+$", nmin = 3, explanation = "What subreddit to pull posts from, the name of the sub, not the URL. You can have multiple subreddits, add an + with no spaces.", example = "AskReddit+Redditdev", oob_error = "A subreddit name HAS to be between 3 and 20 characters" } post_id = { optional = true, default = "", regex = "^((?!://|://)[+a-zA-Z0-9])*$", explanation = "Used if you want to use a specific post.", example = "urdtfx" } max_comment_length = { default = 500, optional = false, nmin = 10, nmax = 10000, type = "int", explanation = "max number of characters a comment can have. default is 500", example = 500, oob_error = "the max comment length should be between 10 and 10000" } post_lang = { default = "", optional = true, explanation = "The language you would like to translate to.", example = "es-cr" }