From 30abf5144666675ea8b68efe6fad411d61bb52ef Mon Sep 17 00:00:00 2001 From: Daniel Berger <39526272+Bengonator@users.noreply.github.com> Date: Tue, 7 Jun 2022 15:12:46 +0200 Subject: [PATCH] Added the possibility to request a new thread, without exiting the program and starting it again. --- main.py | 3 ++ reddit/subreddit.py | 76 ++++++++++++++++++++++++++------------------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/main.py b/main.py index ea944f0..6e8484f 100644 --- a/main.py +++ b/main.py @@ -88,6 +88,9 @@ console.log("[bold green]Enviroment Variables are set! Continuing...") if configured: reddit_object = get_subreddit_threads() + if (reddit_object == -1): + exit() + length, number_of_comments = save_text_to_mp3(reddit_object) download_screenshots_of_reddit_posts( reddit_object, number_of_comments, os.getenv("THEME", "light") diff --git a/reddit/subreddit.py b/reddit/subreddit.py index ff01684..a44c2f9 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -29,43 +29,55 @@ def get_subreddit_threads(): else: passkey = os.getenv("REDDIT_PASSWORD") - content = {} - reddit = praw.Reddit( - client_id=os.getenv("REDDIT_CLIENT_ID"), - client_secret=os.getenv("REDDIT_CLIENT_SECRET"), - user_agent="Accessing AskReddit threads", - username=os.getenv("REDDIT_USERNAME"), - password=passkey, - ) + print_step("Getting AskReddit threads...") + accept_thread = False + while (accept_thread == False): + content = {} + reddit = praw.Reddit( + client_id=os.getenv("REDDIT_CLIENT_ID"), + client_secret=os.getenv("REDDIT_CLIENT_SECRET"), + user_agent="Accessing AskReddit threads", + username=os.getenv("REDDIT_USERNAME"), + password=passkey, + ) - # If the user specifies that he doesnt want a random thread, or if he doesn't insert the "RANDOM_THREAD" variable at all, ask the thread link - if not os.getenv("RANDOM_THREAD") or os.getenv("RANDOM_THREAD") == "no": - print_substep("Insert the full thread link:", style="bold green") - thread_link = input() - print_step("Getting the inserted thread...") - submission = reddit.submission(url=thread_link) - else: - # Otherwise, picks a random thread from the inserted subreddit - if os.getenv("SUBREDDIT"): - subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT"))) + # If the user specifies that he doesnt want a random thread, or if he doesn't insert the "RANDOM_THREAD" variable at all, ask the thread link + if not os.getenv("RANDOM_THREAD") or os.getenv("RANDOM_THREAD") == "no": + print_substep("Insert the full thread link:", style="bold green") + thread_link = input() + print_step("Getting the inserted thread...") + submission = reddit.submission(url=thread_link) else: - # ! Prompt the user to enter a subreddit - try: - subreddit = reddit.subreddit( - re.sub( - r"r\/", - "", - input("What subreddit would you like to pull from? "), + # Otherwise, picks a random thread from the inserted subreddit + if os.getenv("SUBREDDIT"): + subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT"))) + else: + # ! Prompt the user to enter a subreddit + try: + subreddit = reddit.subreddit( + re.sub( + r"r\/", + "", + input("What subreddit would you like to pull from? "), + ) ) - ) - except ValueError: - subreddit = reddit.subreddit("askreddit") - print_substep("Subreddit not defined. Using AskReddit.") + except ValueError: + subreddit = reddit.subreddit("askreddit") + print_substep("Subreddit not defined. Using AskReddit.") - threads = subreddit.hot(limit=25) - submission = list(threads)[random.randrange(0, 25)] + threads = subreddit.hot(limit=25) + submission = list(threads)[random.randrange(0, 25)] + + print_substep(f"Video will be: {submission.title} :thumbsup:") + + response = input("Is this thread acceptable? [yes/no/exit]: ").strip().lower() + if response == "yes" or response == "y": + accept_thread = TRUE + elif response == "exit": + return -1 + else: + print_step("Getting new AskReddit threads...") - print_substep(f"Video will be: {submission.title} :thumbsup:") console.log("Getting video comments...") try: content["thread_url"] = submission.url