From b05143e9551657e58a1fe14aa629f57a56807db7 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 21:08:04 +0800 Subject: [PATCH] moved the exceptions directly to the modules --- cli.py | 19 ------------------- reddit/subreddit.py | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/cli.py b/cli.py index 2e873c7..9c0fba4 100644 --- a/cli.py +++ b/cli.py @@ -1,10 +1,5 @@ import argparse -from prawcore.exceptions import ( - OAuthException, - ResponseException, - RequestException -) from rich.console import Console from main import main @@ -81,20 +76,6 @@ def program_options(): KeyboardInterrupt, ): print_substep("Error occured!", style="bold red") - except OAuthException: - console.print( - "[bold red]There is something wrong with the .env file, kindly check:[/bold red]\n" - + "1. ClientID\n" - + "2. ClientSecret\n" - + "3. If these variables are fine, kindly check other variables." - ) - except ( - RequestException, - ResponseException, - ): - console.print( - "[bold red]Kindly check the kind of application created, it must be script.[/bold red]" - ) if __name__ == "__main__": diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 1c1dcd5..329b22f 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -3,7 +3,14 @@ import os import re import praw +from prawcore.exceptions import ( + OAuthException, + ResponseException, + RequestException, + BadRequest +) from dotenv import load_dotenv +from rich.console import Console from utils.console import print_step, print_substep @@ -17,12 +24,14 @@ def get_subreddit_threads(subreddit_, thread_link_): Returns a list of threads from the AskReddit subreddit. """ + console = Console() + global submission load_dotenv() if os.getenv("REDDIT_2FA", default="no").casefold() == "yes": print( - "\nEnter your two-factor authentication code from your authenticator app.\n" + "\nEnter your two-factor authentication code from your authenticator app.\n", end=" " ) code = input("> ") pw = os.getenv("REDDIT_PASSWORD") @@ -31,14 +40,27 @@ def get_subreddit_threads(subreddit_, thread_link_): passkey = os.getenv("REDDIT_PASSWORD") content = {} - reddit = praw.Reddit( - client_id=os.getenv("REDDIT_CLIENT_ID").strip(), - client_secret=os.getenv("REDDIT_CLIENT_SECRET").strip(), - user_agent="Accessing AskReddit threads", - username=os.getenv("REDDIT_USERNAME").strip(), - password=passkey.strip(), - ) - + try: + reddit = praw.Reddit( + client_id=os.getenv("REDDIT_CLIENT_ID").strip(), + client_secret=os.getenv("REDDIT_CLIENT_SECRET").strip(), + user_agent="Accessing AskReddit threads", + username=os.getenv("REDDIT_USERNAME").strip(), + password=passkey.strip(), + ) + except ( + OAuthException, + ResponseException, + RequestException, + BadRequest + ): + console.print( + "[bold red]There is something wrong with the .env file, kindly check:[/bold red]\n" + + "1. ClientID\n" + + "2. ClientSecret\n" + + "3. If these variables are fine, kindly check other variables." + + "4. Check if the type of Reddit app created is script (personal use script)." + ) # If the user specifies that he doesnt want a random thread, or if