diff --git a/main.py b/main.py index 742fedf..ad5127a 100755 --- a/main.py +++ b/main.py @@ -46,9 +46,33 @@ reddit_id: str reddit_object: Dict[str, str | list] -def main(POST_ID=None) -> None: +def prompt_excluded_terms(): + """Ask the user if any words/phrases should be excluded from posts or comments.""" + + print_substep( + "Would you like to exclude any comments or posts that contain a word or phrase? (y/n)" + ) + choice = input("> ").strip().lower() + if not choice.startswith("y"): + return [] + + print_substep( + "Enter words or phrases to exclude (comma-separated). Leave blank to skip.", + ) + raw_terms = input("> ").strip() + if not raw_terms: + return [] + + terms = [term.strip().lower() for term in raw_terms.split(",") if term.strip()] + return terms + + +def main(POST_ID=None, exclude_terms=None) -> None: global reddit_id, reddit_object - reddit_object = get_subreddit_threads(POST_ID) + reddit_object = get_subreddit_threads(POST_ID, exclude_terms) + if reddit_object is None: + print_substep("No suitable submission found after applying exclusions.", style="bold red") + return reddit_id = extract_id(reddit_object) print_substep(f"Thread ID is {reddit_id}", style="bold blue") length, number_of_comments = save_text_to_mp3(reddit_object) @@ -64,12 +88,12 @@ def main(POST_ID=None) -> None: make_final_video(number_of_comments, length, reddit_object, bg_config) -def run_many(times) -> None: +def run_many(times, exclude_terms=None) -> None: for x in range(1, times + 1): print_step( f'on the {x}{("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th")[x % 10]} iteration of {times}' ) - main() + main(exclude_terms=exclude_terms) Popen("cls" if name == "nt" else "clear", shell=True).wait() @@ -95,6 +119,8 @@ if __name__ == "__main__": ) config is False and sys.exit() + exclude_terms = prompt_excluded_terms() + if ( not settings.config["settings"]["tts"]["tiktok_sessionid"] or settings.config["settings"]["tts"]["tiktok_sessionid"] == "" @@ -111,12 +137,12 @@ if __name__ == "__main__": print_step( f'on the {index}{("st" if index % 10 == 1 else ("nd" if index % 10 == 2 else ("rd" if index % 10 == 3 else "th")))} post of {len(config["reddit"]["thread"]["post_id"].split("+"))}' ) - main(post_id) + main(post_id, exclude_terms) Popen("cls" if name == "nt" else "clear", shell=True).wait() elif config["settings"]["times_to_run"]: - run_many(config["settings"]["times_to_run"]) + run_many(config["settings"]["times_to_run"], exclude_terms) else: - main() + main(None, exclude_terms) except KeyboardInterrupt: shutdown() except ResponseException: diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 5f2ac5f..39d3f25 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -13,7 +13,7 @@ from utils.videos import check_done from utils.voice import sanitize_text -def get_subreddit_threads(POST_ID: str): +def get_subreddit_threads(POST_ID: str, exclude_terms=None): """ Returns a list of threads from the AskReddit subreddit. """ @@ -86,14 +86,24 @@ def get_subreddit_threads(POST_ID: str): print(f"Sorting threads by similarity to the given keywords: {keywords_print}") threads, similarity_scores = sort_by_similarity(threads, keywords) submission, similarity_score = get_subreddit_undone( - threads, subreddit, similarity_scores=similarity_scores + threads, subreddit, similarity_scores=similarity_scores, exclude_terms=exclude_terms ) else: threads = subreddit.hot(limit=25) - submission = get_subreddit_undone(threads, subreddit) + submission = get_subreddit_undone(threads, subreddit, exclude_terms=exclude_terms) if submission is None: - return get_subreddit_threads(POST_ID) # submission already done. rerun + return get_subreddit_threads(POST_ID, exclude_terms) # submission already done. rerun + + if exclude_terms: + terms = [term.lower() for term in exclude_terms if term] + post_text = f"{submission.title or ''} {getattr(submission, 'selftext', '') or ''}".lower() + if any(term in post_text for term in terms): + if POST_ID or settings.config["reddit"]["thread"]["post_id"]: + print_substep("Selected post contains an excluded phrase. Exiting this run.", style="bold red") + return None + print_substep("Post contains an excluded phrase. Trying another post...", style="bold yellow") + return get_subreddit_threads(POST_ID, exclude_terms) elif not submission.num_comments and settings.config["settings"]["storymode"] == "false": print_substep("No comments found. Skipping.") @@ -135,6 +145,12 @@ def get_subreddit_threads(POST_ID: str): if top_level_comment.body in ["[removed]", "[deleted]"]: continue # # see https://github.com/JasonLovesDoggo/RedditVideoMakerBot/issues/78 if not top_level_comment.stickied: + if exclude_terms: + lower_body = top_level_comment.body.lower() + terms = [term.lower() for term in exclude_terms if term] + if any(term in lower_body for term in terms): + print_substep("Skipping comment containing an excluded phrase.") + continue sanitised = sanitize_text(top_level_comment.body) if not sanitised or sanitised == " ": continue diff --git a/utils/subreddit.py b/utils/subreddit.py index 403b6d3..4a33ce3 100644 --- a/utils/subreddit.py +++ b/utils/subreddit.py @@ -6,7 +6,7 @@ from utils.ai_methods import sort_by_similarity from utils.console import print_substep -def get_subreddit_undone(submissions: list, subreddit, times_checked=0, similarity_scores=None): +def get_subreddit_undone(submissions: list, subreddit, times_checked=0, similarity_scores=None, exclude_terms=None): """_summary_ Args: @@ -29,9 +29,16 @@ def get_subreddit_undone(submissions: list, subreddit, times_checked=0, similari json.dump([], f) with open("./video_creation/data/videos.json", "r", encoding="utf-8") as done_vids_raw: done_videos = json.load(done_vids_raw) + terms = [term.lower() for term in (exclude_terms or []) if term] + for i, submission in enumerate(submissions): if already_done(done_videos, submission): continue + if terms: + post_text = f"{submission.title or ''} {getattr(submission, 'selftext', '') or ''}".lower() + if any(term in post_text for term in terms): + print_substep("Post contains an excluded phrase. Skipping...") + continue if submission.over_18: try: if not settings.config["settings"]["allow_nsfw"]: @@ -90,6 +97,7 @@ def get_subreddit_undone(submissions: list, subreddit, times_checked=0, similari ), subreddit, times_checked=index, + exclude_terms=exclude_terms, ) # all the videos in hot have already been done