From 566bda734f5e32f15e5f744b8f13451fa7c83413 Mon Sep 17 00:00:00 2001 From: Simon <65854503+StopmotionSimonYT@users.noreply.github.com> Date: Fri, 29 Jul 2022 21:26:48 +0200 Subject: [PATCH] Added cleanup in shutdown and fixed bug --- id.py | 10 ++++++++++ main.py | 19 ++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 id.py diff --git a/id.py b/id.py new file mode 100644 index 0000000..c66d77a --- /dev/null +++ b/id.py @@ -0,0 +1,10 @@ +import re +from utils.console import print_substep + +def id(reddit_obj: dict): + """ + This function takes a reddit object and returns the post id + """ + id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"]) + print_substep(f"Thread ID is {id}", style="bold blue") + return id \ No newline at end of file diff --git a/main.py b/main.py index 7bb971f..2ea08e3 100755 --- a/main.py +++ b/main.py @@ -9,7 +9,8 @@ from prawcore import ResponseException from reddit.subreddit import get_subreddit_threads from utils.cleanup import cleanup from utils.console import print_markdown, print_step, print_substep -from utils import settings, id +from utils import settings +from utils.id import id from video_creation.background import ( download_background, @@ -42,7 +43,8 @@ print_step(f"You are using v{__VERSION__} of the bot in the {__BRANCH__} branch. def main(POST_ID=None): reddit_object = get_subreddit_threads(POST_ID) - id(reddit_object) + global redditid + redditid = id(reddit_object) length, number_of_comments = save_text_to_mp3(reddit_object) length = math.ceil(length) download_screenshots_of_reddit_posts(reddit_object, number_of_comments) @@ -62,9 +64,16 @@ def run_many(times): def shutdown(): - print("Exiting...") - exit() - + print_markdown("## Clearing temp files") + try: + redditid + except NameError: + print("Exiting...") + exit() + else: + cleanup(redditid) + print("Exiting...") + exit() if __name__ == "__main__": config = settings.check_toml("utils/.config.template.toml", "config.toml")