From 21ef6a17d1567f89c0fde5a0cce74096171cca3c Mon Sep 17 00:00:00 2001 From: Callum Leslie Date: Tue, 9 Aug 2022 01:27:38 +0100 Subject: [PATCH 01/10] fix: add pyttsx3 to requirements.txt --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 7bccd0d..a954c20 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,5 +9,5 @@ requests==2.28.1 rich==12.5.1 toml==0.10.2 translators==5.3.1 - +pyttsx3==2.90 Pillow~=9.1.1 From 005065549944381112bda677ee321bc4faf35afe Mon Sep 17 00:00:00 2001 From: Simon <65854503+StopmotionSimonYT@users.noreply.github.com> Date: Tue, 9 Aug 2022 20:03:37 +0200 Subject: [PATCH 02/10] Add version check in utils folder --- utils/version.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 utils/version.py diff --git a/utils/version.py b/utils/version.py new file mode 100644 index 0000000..3042c0c --- /dev/null +++ b/utils/version.py @@ -0,0 +1,11 @@ +import requests +from utils.console import print_step + +def checkversion(__VERSION__): + response = requests.get("https://api.github.com/repos/elebumm/RedditVideoMakerBot/releases/latest") + latestversion = (response.json()["tag_name"]) + if __VERSION__ == latestversion: + print_step(f"You are using the newest version ({__VERSION__}) of the bot") + return True + else: + print_step(f"You are using an older version ({__VERSION__}) of the bot. Download the newest version ({latestversion}) from https://github.com/elebumm/RedditVideoMakerBot/releases/latest") \ No newline at end of file From 7b7abf62898c297a30c1ad19d8abf7353f78671f Mon Sep 17 00:00:00 2001 From: Simon <65854503+StopmotionSimonYT@users.noreply.github.com> Date: Tue, 9 Aug 2022 20:05:55 +0200 Subject: [PATCH 03/10] Using the check version in main.py --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index bef8ff8..21e257f 100755 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ from reddit.subreddit import get_subreddit_threads from utils.cleanup import cleanup from utils.console import print_markdown, print_step from utils import settings +from utils.version import checkversion from video_creation.background import ( download_background, @@ -36,8 +37,7 @@ print( print_markdown( "### Thanks for using this tool! [Feel free to contribute to this project on GitHub!](https://lewismenelaws.com) If you have any questions, feel free to reach out to me on Twitter or submit a GitHub issue. You can find solutions to many common problems in the [Documentation](https://luka-hietala.gitbook.io/documentation-for-the-reddit-bot/)" ) -print_step(f"You are using v{__VERSION__} of the bot") - +checkversion(__VERSION__) def main(POST_ID=None): cleanup() From a4fa968acf6bb5c97bca48b9bdfcafe362aee353 Mon Sep 17 00:00:00 2001 From: Callum Leslie Date: Tue, 9 Aug 2022 18:18:58 +0000 Subject: [PATCH 04/10] chore: bump version and remove unused code --- main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/main.py b/main.py index 21e257f..b078c10 100755 --- a/main.py +++ b/main.py @@ -20,8 +20,7 @@ from video_creation.final_video import make_final_video from video_creation.screenshot_downloader import download_screenshots_of_reddit_posts from video_creation.voices import save_text_to_mp3 -__VERSION__ = "2.4" -__BRANCH__ = "master" +__VERSION__ = "2.4.1" print( """ From 66ec9b34d24a7fe540be2dfc64440c44e7292f1d Mon Sep 17 00:00:00 2001 From: Callum Leslie Date: Tue, 9 Aug 2022 19:21:12 +0100 Subject: [PATCH 05/10] style: formatting --- GUI.py | 10 ++++++---- TTS/pyttsx.py | 4 +--- main.py | 1 + utils/console.py | 1 - utils/version.py | 11 ++++++++--- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/GUI.py b/GUI.py index 90e37ae..bc41838 100644 --- a/GUI.py +++ b/GUI.py @@ -1,6 +1,7 @@ # Import the server module import http.server import webbrowser + # Set the hostname HOST = "localhost" # Set the port number @@ -9,15 +10,16 @@ PORT = 4000 # Define class to display the index page of the web server class PythonServer(http.server.SimpleHTTPRequestHandler): def do_GET(self): - if self.path == '/GUI': - self.path = 'index.html' + if self.path == "/GUI": + self.path = "index.html" return http.server.SimpleHTTPRequestHandler.do_GET(self) + # Declare object of the class webServer = http.server.HTTPServer((HOST, PORT), PythonServer) # Print the URL of the webserver, new =2 opens in a new tab print(f"Server started at http://{HOST}:{PORT}/GUI/") -webbrowser.open(f'http://{HOST}:{PORT}/GUI/', new = 2) +webbrowser.open(f"http://{HOST}:{PORT}/GUI/", new=2) print("Website opened in new tab") print("Press Ctrl+C to quit") try: @@ -27,4 +29,4 @@ except KeyboardInterrupt: # Stop the web server webServer.server_close() print("The server is stopped.") - exit() \ No newline at end of file + exit() diff --git a/TTS/pyttsx.py b/TTS/pyttsx.py index 06d27c5..b5ffb29 100644 --- a/TTS/pyttsx.py +++ b/TTS/pyttsx.py @@ -19,9 +19,7 @@ class pyttsx: if voice_id == "" or voice_num == "": voice_id = 2 voice_num = 3 - raise ValueError( - "set pyttsx values to a valid value, switching to defaults" - ) + raise ValueError("set pyttsx values to a valid value, switching to defaults") else: voice_id = int(voice_id) voice_num = int(voice_num) diff --git a/main.py b/main.py index b078c10..a4abd81 100755 --- a/main.py +++ b/main.py @@ -38,6 +38,7 @@ print_markdown( ) checkversion(__VERSION__) + def main(POST_ID=None): cleanup() reddit_object = get_subreddit_threads(POST_ID) diff --git a/utils/console.py b/utils/console.py index 1518c27..6f99a41 100644 --- a/utils/console.py +++ b/utils/console.py @@ -118,4 +118,3 @@ def handle_input( console.print( "[red bold]" + err_message + "\nValid options are: " + ", ".join(map(str, options)) + "." ) - diff --git a/utils/version.py b/utils/version.py index 3042c0c..a8177cc 100644 --- a/utils/version.py +++ b/utils/version.py @@ -1,11 +1,16 @@ import requests from utils.console import print_step + def checkversion(__VERSION__): - response = requests.get("https://api.github.com/repos/elebumm/RedditVideoMakerBot/releases/latest") - latestversion = (response.json()["tag_name"]) + response = requests.get( + "https://api.github.com/repos/elebumm/RedditVideoMakerBot/releases/latest" + ) + latestversion = response.json()["tag_name"] if __VERSION__ == latestversion: print_step(f"You are using the newest version ({__VERSION__}) of the bot") return True else: - print_step(f"You are using an older version ({__VERSION__}) of the bot. Download the newest version ({latestversion}) from https://github.com/elebumm/RedditVideoMakerBot/releases/latest") \ No newline at end of file + print_step( + f"You are using an older version ({__VERSION__}) of the bot. Download the newest version ({latestversion}) from https://github.com/elebumm/RedditVideoMakerBot/releases/latest" + ) From 71c317da23b02b4228376ebeab35e0a405a687e9 Mon Sep 17 00:00:00 2001 From: Callum Leslie Date: Tue, 9 Aug 2022 19:45:31 +0100 Subject: [PATCH 06/10] feat: meaningful error message for bad credentials --- reddit/subreddit.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 716a7fa..06538b2 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -1,5 +1,7 @@ import re +from prawcore.exceptions import ResponseException + from utils import settings import praw from praw.models import MoreComments @@ -29,14 +31,21 @@ def get_subreddit_threads(POST_ID: str): username = settings.config["reddit"]["creds"]["username"] if str(username).casefold().startswith("u/"): username = username[2:] - reddit = praw.Reddit( - client_id=settings.config["reddit"]["creds"]["client_id"], - client_secret=settings.config["reddit"]["creds"]["client_secret"], - user_agent="Accessing Reddit threads", - username=username, - passkey=passkey, - check_for_async=False, - ) + try: + reddit = praw.Reddit( + client_id=settings.config["reddit"]["creds"]["client_id"], + client_secret=settings.config["reddit"]["creds"]["client_secret"], + user_agent="Accessing Reddit threads", + username=username, + passkey=passkey, + check_for_async=False, + ) + except ResponseException as e: + match e.response.status_code: + case 401: + print("Invalid credentials - please check them in config.toml") + except: + print("Something went wrong...") # Ask user for subreddit input print_step("Getting subreddit threads...") From 968028c5415dc73e3c2c5c337d0974ebc82c5635 Mon Sep 17 00:00:00 2001 From: Callum Leslie Date: Tue, 9 Aug 2022 20:05:06 +0100 Subject: [PATCH 07/10] chore: remove comment reference to .env --- reddit/subreddit.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 06538b2..11b93af 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -66,9 +66,7 @@ def get_subreddit_threads(POST_ID: str): subreddit_choice = sub if str(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. + subreddit = reddit.subreddit(subreddit_choice) if POST_ID: # would only be called if there are multiple queued posts submission = reddit.submission(id=POST_ID) From 74a3707f5543b88cd1ad4bfabe1d613eff4b2b4b Mon Sep 17 00:00:00 2001 From: Simon <65854503+StopmotionSimonYT@users.noreply.github.com> Date: Tue, 9 Aug 2022 21:23:03 +0200 Subject: [PATCH 08/10] Config.toml multiple subreddits --- utils/.config.template.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/.config.template.toml b/utils/.config.template.toml index 31ae9aa..adbaed0 100644 --- a/utils/.config.template.toml +++ b/utils/.config.template.toml @@ -8,7 +8,7 @@ password = { optional = false, nmin = 8, explanation = "The password of your red [reddit.thread] random = { optional = true, options = [true, false,], default = false, type = "bool", explanation = "If set to no, it will ask you a thread link to extract the thread, if yes it will randomize it. Default: 'False'", example = "True" } -subreddit = { optional = false, regex = "[_0-9a-zA-Z]+$", nmin = 3, explanation = "What subreddit to pull posts from, the name of the sub, not the URL", example = "AskReddit", oob_error = "A subreddit name HAS to be between 3 and 20 characters" } +subreddit = { optional = false, regex = "[_0-9a-zA-Z]+$", nmin = 3, explanation = "What subreddit to pull posts from, the name of the sub, not the URL. You can have multiple subreddits, add an + with no spaces.", example = "AskReddit+Redditdev", oob_error = "A subreddit name HAS to be between 3 and 20 characters" } post_id = { optional = true, default = "", regex = "^((?!://|://)[+a-zA-Z0-9])*$", explanation = "Used if you want to use a specific post.", example = "urdtfx" } max_comment_length = { default = 500, optional = false, nmin = 10, nmax = 10000, type = "int", explanation = "max number of characters a comment can have. default is 500", example = 500, oob_error = "the max comment length should be between 10 and 10000" } post_lang = { default = "", optional = true, explanation = "The language you would like to translate to.", example = "es-cr" } From 939d9ad387a7c8ac3a6901d95fa102972d9d4a97 Mon Sep 17 00:00:00 2001 From: Callum Leslie Date: Tue, 9 Aug 2022 20:05:06 +0100 Subject: [PATCH 09/10] chore: remove comment reference to .env --- reddit/subreddit.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 716a7fa..1042f7c 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -57,9 +57,7 @@ def get_subreddit_threads(POST_ID: str): subreddit_choice = sub if str(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. + subreddit = reddit.subreddit(subreddit_choice) if POST_ID: # would only be called if there are multiple queued posts submission = reddit.submission(id=POST_ID) From 5a8db461789a8f6df13d47b7ee17e25afc416036 Mon Sep 17 00:00:00 2001 From: Simon <65854503+StopmotionSimonYT@users.noreply.github.com> Date: Tue, 9 Aug 2022 22:48:15 +0200 Subject: [PATCH 10/10] Bot can run multiple instances at the same time (#1091) * Bot can run multiple instances at the same time * Delete TTS/__pycache__ directory * Delete video_creation/__pycache__ directory * Delete videos.json * Delete utils/__pycache__ directory * Create videos.json * Moved id to utils * Added cleanup in shutdown and fixed bug * Removed watermark todo and fixed it * Delete final_video.py * Update final_video.py * Delete video.py * Update video.py * Delete id.py * feat: meaningful error message for bad credentials * chore: remove comment reference to .env Co-authored-by: Callum Leslie --- TTS/engine_wrapper.py | 5 +++-- main.py | 22 +++++++++++++++------- utils/cleanup.py | 4 ++-- utils/id.py | 10 ++++++++++ utils/video.py | 11 +++++++---- video_creation/background.py | 9 +++++---- video_creation/final_video.py | 21 +++++++++++---------- video_creation/screenshot_downloader.py | 12 +++++++----- 8 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 utils/id.py diff --git a/TTS/engine_wrapper.py b/TTS/engine_wrapper.py index 9a64f0e..12668df 100644 --- a/TTS/engine_wrapper.py +++ b/TTS/engine_wrapper.py @@ -34,13 +34,14 @@ class TTSEngine: self, tts_module, reddit_object: dict, - path: str = "assets/temp/mp3", + path: str = "assets/temp/", max_length: int = DEFAULT_MAX_LENGTH, last_clip_length: int = 0, ): self.tts_module = tts_module() self.reddit_object = reddit_object - self.path = path + self.redditid = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"]) + self.path = path + self.redditid + "/mp3" self.max_length = max_length self.length = 0 self.last_clip_length = last_clip_length diff --git a/main.py b/main.py index a4abd81..8a0755d 100755 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ #!/usr/bin/env python import math +import re from subprocess import Popen from os import name @@ -7,8 +8,9 @@ from prawcore import ResponseException from reddit.subreddit import get_subreddit_threads from utils.cleanup import cleanup -from utils.console import print_markdown, print_step +from utils.console import print_markdown, print_step, print_substep from utils import settings +from utils.id import id from utils.version import checkversion from video_creation.background import ( @@ -40,14 +42,15 @@ checkversion(__VERSION__) def main(POST_ID=None): - cleanup() reddit_object = get_subreddit_threads(POST_ID) + 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) bg_config = get_background_config() download_background(bg_config) - chop_background_video(bg_config, length) + chop_background_video(bg_config, length, reddit_object) make_final_video(number_of_comments, length, reddit_object, bg_config) @@ -62,10 +65,15 @@ def run_many(times): def shutdown(): print_markdown("## Clearing temp files") - cleanup() - print("Exiting...") - exit() - + 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") diff --git a/utils/cleanup.py b/utils/cleanup.py index 75074b7..f7bde35 100644 --- a/utils/cleanup.py +++ b/utils/cleanup.py @@ -6,7 +6,7 @@ def _listdir(d): # listdir with full path return [os.path.join(d, f) for f in os.listdir(d)] -def cleanup() -> int: +def cleanup(id) -> int: """Deletes all temporary assets in assets/temp Returns: @@ -18,7 +18,7 @@ def cleanup() -> int: count += len(files) for f in files: os.remove(f) - REMOVE_DIRS = ["./assets/temp/mp3/", "./assets/temp/png/"] + REMOVE_DIRS = [f"./assets/temp/{id}/mp3/", f"./assets/temp/{id}/png/"] files_to_remove = list(map(_listdir, REMOVE_DIRS)) for directory in files_to_remove: for file in directory: diff --git a/utils/id.py b/utils/id.py new file mode 100644 index 0000000..cdf71a6 --- /dev/null +++ b/utils/id.py @@ -0,0 +1,10 @@ +import re +from utils 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/utils/video.py b/utils/video.py index 63dc170..5566cd1 100644 --- a/utils/video.py +++ b/utils/video.py @@ -1,4 +1,6 @@ from __future__ import annotations +from ast import Str +import re from typing import Tuple @@ -14,8 +16,9 @@ class Video: self.duration = self.video.duration @staticmethod - def _create_watermark(text, fontsize, opacity=0.5): - path = "./assets/temp/png/watermark.png" + def _create_watermark(text, redditid, fontsize, opacity=0.5): + id = re.sub(r"[^\w\s-]", "", redditid["thread_id"]) + path = f"./assets/temp/{id}/png/watermark.png" width = int(fontsize * len(text)) height = int(fontsize * len(text) / 2) white = (255, 255, 255) @@ -35,7 +38,7 @@ class Video: return ImageClip(path) def add_watermark( - self, text, opacity=0.5, duration: int | float = 5, position: Tuple = (0.7, 0.9), fontsize=15 + self, text, redditid, opacity=0.5, duration: int | float = 5, position: Tuple = (0.7, 0.9), fontsize=15 ): compensation = round( (position[0] / ((len(text) * (fontsize / 5) / 1.5) / 100 + position[0] * position[0])), @@ -44,7 +47,7 @@ class Video: position = (compensation, position[1]) # print(f'{compensation=}') # print(f'{position=}') - img_clip = self._create_watermark(text, opacity=opacity, fontsize=fontsize) + img_clip = self._create_watermark(text, redditid, fontsize=fontsize, opacity=opacity) img_clip = img_clip.set_opacity(opacity).set_duration(duration) img_clip = img_clip.set_position( position, relative=True diff --git a/video_creation/background.py b/video_creation/background.py index 6e656fa..1676f97 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -1,6 +1,7 @@ from pathlib import Path import random from random import randrange +import re from typing import Any, Tuple @@ -62,7 +63,7 @@ def download_background(background_config: Tuple[str, str, str, Any]): print_substep("Background video downloaded successfully! 🎉", style="bold green") -def chop_background_video(background_config: Tuple[str, str, str, Any], video_length: int): +def chop_background_video(background_config: Tuple[str, str, str, Any], video_length: int, reddit_object: dict): """Generates the background footage to be used in the video and writes it to assets/temp/background.mp4 Args: @@ -72,7 +73,7 @@ def chop_background_video(background_config: Tuple[str, str, str, Any], video_le print_step("Finding a spot in the backgrounds video to chop...✂️") choice = f"{background_config[2]}-{background_config[1]}" - + id = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"]) background = VideoFileClip(f"assets/backgrounds/{choice}") start_time, end_time = get_start_and_end_times(video_length, background.duration) @@ -81,12 +82,12 @@ def chop_background_video(background_config: Tuple[str, str, str, Any], video_le f"assets/backgrounds/{choice}", start_time, end_time, - targetname="assets/temp/background.mp4", + targetname=f"assets/temp/{id}/background.mp4", ) except (OSError, IOError): # ffmpeg issue see #348 print_substep("FFMPEG issue. Trying again...") with VideoFileClip(f"assets/backgrounds/{choice}") as video: new = video.subclip(start_time, end_time) - new.write_videofile("assets/temp/background.mp4") + new.write_videofile(f"assets/temp/{id}/background.mp4") print_substep("Background video chopped successfully!", style="bold green") return background_config[2] diff --git a/video_creation/final_video.py b/video_creation/final_video.py index d78863a..743e561 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -62,21 +62,22 @@ def make_final_video( # except (TypeError, KeyError): # print('No background audio volume found in config.toml. Using default value of 1.') # VOLUME_MULTIPLIER = 1 + id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"]) print_step("Creating the final video 🎥") VideoFileClip.reW = lambda clip: clip.resize(width=W) VideoFileClip.reH = lambda clip: clip.resize(width=H) opacity = settings.config["settings"]["opacity"] transition = settings.config["settings"]["transition"] background_clip = ( - VideoFileClip("assets/temp/background.mp4") + VideoFileClip(f"assets/temp/{id}/background.mp4") .without_audio() .resize(height=H) .crop(x1=1166.6, y1=0, x2=2246.6, y2=1920) ) # Gather all audio clips - audio_clips = [AudioFileClip(f"assets/temp/mp3/{i}.mp3") for i in range(number_of_clips)] - audio_clips.insert(0, AudioFileClip("assets/temp/mp3/title.mp3")) + audio_clips = [AudioFileClip(f"assets/temp/{id}/mp3/{i}.mp3") for i in range(number_of_clips)] + audio_clips.insert(0, AudioFileClip(f"assets/temp/{id}/mp3/title.mp3")) audio_concat = concatenate_audioclips(audio_clips) audio_composite = CompositeAudioClip([audio_concat]) @@ -88,7 +89,7 @@ def make_final_video( new_transition = 0 if transition is None or float(transition) > 2 else float(transition) image_clips.insert( 0, - ImageClip("assets/temp/png/title.png") + ImageClip(f"assets/temp/{id}/png/title.png") .set_duration(audio_clips[0].duration) .resize(width=W - 100) .set_opacity(new_opacity) @@ -98,7 +99,7 @@ def make_final_video( for i in range(0, number_of_clips): image_clips.append( - ImageClip(f"assets/temp/png/comment_{i}.png") + ImageClip(f"assets/temp/{id}/png/comment_{i}.png") .set_duration(audio_clips[i + 1].duration) .resize(width=W - 100) .set_opacity(new_opacity) @@ -140,10 +141,10 @@ def make_final_video( # # VOLUME_MULTIPLIER) # lower volume by background_audio_volume, use with fx # final.set_audio(final_audio) final = Video(final).add_watermark( - text=f"Background credit: {background_config[2]}", opacity=0.4 - ) + text=f"Background credit: {background_config[2]}", opacity=0.4, redditid=reddit_obj + ) final.write_videofile( - "assets/temp/temp.mp4", + f"assets/temp/{id}/temp.mp4", fps=30, audio_codec="aac", audio_bitrate="192k", @@ -151,14 +152,14 @@ def make_final_video( threads=multiprocessing.cpu_count(), ) ffmpeg_extract_subclip( - "assets/temp/temp.mp4", + f"assets/temp/{id}/temp.mp4", 0, length, targetname=f"results/{subreddit}/{filename}", ) save_data(subreddit, filename, title, idx, background_config[2]) print_step("Removing temporary files 🗑") - cleanups = cleanup() + cleanups = cleanup(id) print_substep(f"Removed {cleanups} temporary files 🗑") print_substep("See result in the results folder!") diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 51645e4..2344fce 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -1,6 +1,7 @@ import json from pathlib import Path +import re from typing import Dict from utils import settings from playwright.async_api import async_playwright # pylint: disable=unused-import @@ -24,9 +25,9 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in screenshot_num (int): Number of screenshots to download """ print_step("Downloading screenshots of reddit posts...") - + id = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"]) # ! Make sure the reddit screenshots folder exists - Path("assets/temp/png").mkdir(parents=True, exist_ok=True) + Path(f"assets/temp/{id}/png").mkdir(parents=True, exist_ok=True) with sync_playwright() as p: print_substep("Launching Headless Browser...") @@ -72,11 +73,12 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in else: print_substep("Skipping translation...") - page.locator('[data-test-id="post-content"]').screenshot(path="assets/temp/png/title.png") + postcontentpath = f"assets/temp/{id}/png/title.png" + page.locator('[data-test-id="post-content"]').screenshot(path= postcontentpath) if storymode: page.locator('[data-click-id="text"]').screenshot( - path="assets/temp/png/story_content.png" + path=f"assets/temp/{id}/png/story_content.png" ) else: for idx, comment in enumerate( @@ -104,7 +106,7 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in ) try: page.locator(f"#t1_{comment['comment_id']}").screenshot( - path=f"assets/temp/png/comment_{idx}.png" + path=f"assets/temp/{id}/png/comment_{idx}.png" ) except TimeoutError: del reddit_object["comments"]