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 
pull/790/head
Jason 3 years ago
parent 211a3e1da8
commit a5fe2eface

@ -31,10 +31,9 @@ print_markdown(
) )
def main(): def main(POST_ID=None):
cleanup() cleanup()
reddit_object = get_subreddit_threads(POST_ID)
reddit_object = get_subreddit_threads()
length, number_of_comments = save_text_to_mp3(reddit_object) length, number_of_comments = save_text_to_mp3(reddit_object)
download_screenshots_of_reddit_posts(reddit_object, number_of_comments) download_screenshots_of_reddit_posts(reddit_object, number_of_comments)
download_background() download_background()
@ -58,6 +57,13 @@ if __name__ == "__main__":
try: try:
if getenv("TIMES_TO_RUN") and isinstance(int(getenv("TIMES_TO_RUN")), int): if getenv("TIMES_TO_RUN") and isinstance(int(getenv("TIMES_TO_RUN")), int):
run_many(int(getenv("TIMES_TO_RUN"))) 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: else:
main() main()
except KeyboardInterrupt: except KeyboardInterrupt:

@ -9,13 +9,11 @@ from utils.subreddit import get_subreddit_undone
from utils.videos import check_done 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. Returns a list of threads from the AskReddit subreddit.
""" """
submission = None
print_substep("Logging into Reddit.") print_substep("Logging into Reddit.")
content = {} content = {}
@ -29,11 +27,14 @@ def get_subreddit_threads():
passkey = f"{pw}:{code}" passkey = f"{pw}:{code}"
else: else:
passkey = getenv("REDDIT_PASSWORD") passkey = getenv("REDDIT_PASSWORD")
username = getenv("REDDIT_USERNAME")
if username.casefold().startswith('u/'):
username = username[2:]
reddit = praw.Reddit( reddit = praw.Reddit(
client_id=getenv("REDDIT_CLIENT_ID"), client_id=getenv("REDDIT_CLIENT_ID"),
client_secret=getenv("REDDIT_CLIENT_SECRET"), client_secret=getenv("REDDIT_CLIENT_SECRET"),
user_agent="Accessing Reddit threads", user_agent="Accessing Reddit threads",
username=getenv("REDDIT_USERNAME"), username=username,
passkey=passkey, passkey=passkey,
check_for_async=False, check_for_async=False,
) )
@ -41,7 +42,7 @@ def get_subreddit_threads():
# Ask user for subreddit input # Ask user for subreddit input
print_step("Getting subreddit threads...") print_step("Getting subreddit threads...")
if not getenv( if not getenv(
"SUBREDDIT" "SUBREDDIT"
): # note to user. you can have multiple subreddits via reddit.subreddit("redditdev+learnpython") ): # note to user. you can have multiple subreddits via reddit.subreddit("redditdev+learnpython")
try: try:
subreddit = reddit.subreddit( subreddit = reddit.subreddit(
@ -58,20 +59,23 @@ def get_subreddit_threads():
f"Using subreddit: r/{getenv('SUBREDDIT')} from environment variable config" f"Using subreddit: r/{getenv('SUBREDDIT')} from environment variable config"
) )
subreddit_choice = getenv("SUBREDDIT") 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_choice = subreddit_choice[2:]
subreddit = reddit.subreddit( subreddit = reddit.subreddit(
subreddit_choice subreddit_choice
) # Allows you to specify in .env. Done for automation purposes. ) # 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")) submission = reddit.submission(id=getenv("POST_ID"))
else: else:
threads = subreddit.hot(limit=25) threads = subreddit.hot(limit=25)
submission = get_subreddit_undone(threads, subreddit) submission = get_subreddit_undone(threads, subreddit)
submission = check_done(submission) # double checking submission = check_done(submission) # double checking
if submission is None: 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 upvotes = submission.score
ratio = submission.upvote_ratio * 100 ratio = submission.upvote_ratio * 100
num_comments = submission.num_comments num_comments = submission.num_comments
@ -94,12 +98,12 @@ def get_subreddit_threads():
continue # # see https://github.com/JasonLovesDoggo/RedditVideoMakerBot/issues/78 continue # # see https://github.com/JasonLovesDoggo/RedditVideoMakerBot/issues/78
if not top_level_comment.stickied: if not top_level_comment.stickied:
if len(top_level_comment.body) <= int(getenv("MAX_COMMENT_LENGTH", "500")): 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( content["comments"].append(
{ {
"comment_body": top_level_comment.body, "comment_body": top_level_comment.body,
"comment_url": top_level_comment.permalink, "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") print_substep("Received subreddit threads Successfully.", style="bold green")

@ -57,8 +57,8 @@ def make_final_video(number_of_clips: int, length: int, reddit_obj: dict[str]):
# round total_length to an integer # round total_length to an integer
int_total_length = round(total_length) int_total_length = round(total_length)
# Output 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 # add title to video
image_clips = [] image_clips = []
# Gather all images # Gather all images

Loading…
Cancel
Save