From a5fe2eface79cf2552b5dd61f07306cbca75f496 Mon Sep 17 00:00:00 2001 From: Jason <thebosssg19@gmail.com> Date: Fri, 1 Jul 2022 16:50:15 -0400 Subject: [PATCH] feat: allows users to queue up multiple posts by splitting the post id's with a + e.g. POST_ID="urdtfx+qrftps" would do urdtfx than qrftps note: it is incompatible with times_to_run feat: allows the username to begin with u/ closes #745 --- main.py | 12 +++++++++--- reddit/subreddit.py | 24 ++++++++++++++---------- video_creation/final_video.py | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 0aca634..99d0995 100755 --- a/main.py +++ b/main.py @@ -31,10 +31,9 @@ print_markdown( ) -def main(): +def main(POST_ID=None): cleanup() - - reddit_object = get_subreddit_threads() + reddit_object = get_subreddit_threads(POST_ID) length, number_of_comments = save_text_to_mp3(reddit_object) download_screenshots_of_reddit_posts(reddit_object, number_of_comments) download_background() @@ -58,6 +57,13 @@ if __name__ == "__main__": try: if getenv("TIMES_TO_RUN") and isinstance(int(getenv("TIMES_TO_RUN")), int): run_many(int(getenv("TIMES_TO_RUN"))) + + elif len(getenv("POST_ID", '').split('+')) > 1: + for index, post_id in enumerate(getenv("POST_ID", '').split('+')): + index += 1 + print_step(f'on the {index}{("st" if index == 1 else ("nd" if index == 2 else ("rd" if index == 3 else "th")))} post of {len(getenv("POST_ID", "").split("+"))}') + main(post_id) + Popen("cls" if name == "nt" else "clear", shell=True).wait() else: main() except KeyboardInterrupt: diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 3419fbf..ddbdfe1 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -9,13 +9,11 @@ from utils.subreddit import get_subreddit_undone from utils.videos import check_done -def get_subreddit_threads(): +def get_subreddit_threads(POST_ID: str): """ Returns a list of threads from the AskReddit subreddit. """ - submission = None - print_substep("Logging into Reddit.") content = {} @@ -29,11 +27,14 @@ def get_subreddit_threads(): passkey = f"{pw}:{code}" else: passkey = getenv("REDDIT_PASSWORD") + username = getenv("REDDIT_USERNAME") + if username.casefold().startswith('u/'): + username = username[2:] reddit = praw.Reddit( client_id=getenv("REDDIT_CLIENT_ID"), client_secret=getenv("REDDIT_CLIENT_SECRET"), user_agent="Accessing Reddit threads", - username=getenv("REDDIT_USERNAME"), + username=username, passkey=passkey, check_for_async=False, ) @@ -41,7 +42,7 @@ def get_subreddit_threads(): # Ask user for subreddit input print_step("Getting subreddit threads...") if not getenv( - "SUBREDDIT" + "SUBREDDIT" ): # note to user. you can have multiple subreddits via reddit.subreddit("redditdev+learnpython") try: subreddit = reddit.subreddit( @@ -58,20 +59,23 @@ def get_subreddit_threads(): f"Using subreddit: r/{getenv('SUBREDDIT')} from environment variable config" ) subreddit_choice = getenv("SUBREDDIT") - if subreddit_choice.casefold().startswith('r/'): # removes the r/ from the input + if 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. - if getenv("POST_ID"): + if POST_ID: # would only be called if there are multiple queued posts + submission = reddit.submission(id=POST_ID) + elif getenv("POST_ID") and len(getenv("POST_ID").split('+')) == 1: submission = reddit.submission(id=getenv("POST_ID")) else: + threads = subreddit.hot(limit=25) submission = get_subreddit_undone(threads, subreddit) submission = check_done(submission) # double checking if submission is None: - return get_subreddit_threads() # submission already done. rerun + return get_subreddit_threads(POST_ID) # submission already done. rerun upvotes = submission.score ratio = submission.upvote_ratio * 100 num_comments = submission.num_comments @@ -94,12 +98,12 @@ def get_subreddit_threads(): continue # # see https://github.com/JasonLovesDoggo/RedditVideoMakerBot/issues/78 if not top_level_comment.stickied: if len(top_level_comment.body) <= int(getenv("MAX_COMMENT_LENGTH", "500")): - if not top_level_comment.author is None: + if top_level_comment.author is not None: # if errors occur with this change to if not. content["comments"].append( { "comment_body": top_level_comment.body, "comment_url": top_level_comment.permalink, - "comment_id": top_level_comment.id, + "comment_id": top_level_comment.id } ) print_substep("Received subreddit threads Successfully.", style="bold green") diff --git a/video_creation/final_video.py b/video_creation/final_video.py index baa5a00..62a55e1 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -57,8 +57,8 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict[str]): # round total_length to an integer int_total_length = round(total_length) # Output Length - console.log(f"[bold green] Video Will Be: {int_total_length} Seconds Long") + console.log(f"[bold green] Video Will Be: {int_total_length} Seconds Long") # add title to video image_clips = [] # Gather all images