From f57cad9798c8d50a5a3914c853f8cf3c8acd26de Mon Sep 17 00:00:00 2001 From: Ahmad Mujahid Date: Sun, 5 Jun 2022 12:33:49 +0400 Subject: [PATCH] The user can select thread --- .env.template | 3 +++ reddit/subreddit.py | 45 ++++++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/.env.template b/.env.template index cab265e..21e9116 100644 --- a/.env.template +++ b/.env.template @@ -8,8 +8,11 @@ REDDIT_2FA="" # Valid options are "light" and "dark" THEME="" +# Enter either a SUBREDDIT to choose randomly from or SUBMISSION_ID but not both # Enter a subreddit, e.g. "AskReddit" SUBREDDIT="" +# Enter submission id, e.g. "v4nt9m" +SUBMISSION_ID="" # Range is 0 -> 1 OPACITY="0.9" diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 61c909d..dc4f9f5 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -1,7 +1,13 @@ +from asyncio.windows_events import NULL from utils.console import print_markdown, print_step, print_substep from dotenv import load_dotenv import os, random, praw, re +def get_random_submission(reddit , subreddit_name): + subreddit = reddit.subreddit(re.sub(r"r\/", "", subreddit_name)) + threads = subreddit.hot(limit=25) + return list(threads)[random.randrange(0, 25)] + def get_subreddit_threads(): global submission """ @@ -10,8 +16,6 @@ def get_subreddit_threads(): load_dotenv() - print_step("Getting AskReddit threads...") - if os.getenv("REDDIT_2FA", default="no").casefold() == "yes": print( "\nEnter your two-factor authentication code from your authenticator app.\n" @@ -33,20 +37,31 @@ def get_subreddit_threads(): password=passkey, ) - 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.") + submission = NULL; - threads = subreddit.hot(limit=25) - submission = list(threads)[random.randrange(0, 25)] + try: + if os.getenv("SUBMISSION_ID"): + print_step("Getting submissions from " + os.getenv("SUBMISSION_ID") + " based on .env file value") + submission = reddit.submission(os.getenv("SUBMISSION_ID")) + elif os.getenv("SUBREDDIT"): + print_step("Getting a radon submission from " + os.getenv("SUBREDDIT") + " based on .env file value") + submission = get_random_submission(reddit , os.getenv("SUBREDDIT")) + else: + print_step("Choose your option : ") + print_substep("1: Enter subreddit to pull randomly from") + print_substep("2: Enter a submission to ID") + print_substep("3: Pull randomly from AskReddit") + option = int(input("Enter the option number : ")) + if option == 1: + submission = get_random_submission(reddit , input("What subreddit would you like to pull from? ")) + elif option == 2: + submission = reddit.submission(input("Enter submission ID : ")); + else: + submission = get_random_submission(reddit , "askreddit") + except ValueError: + print_substep("Subreddit not defined. Using AskReddit.") + submission = get_random_submission(reddit , "askreddit") + print_substep(f"Video will be: {submission.title} :thumbsup:") try: