From 18eb749bee546b7f9b46aafd3038419d53b7712d Mon Sep 17 00:00:00 2001 From: Kristian Date: Wed, 8 May 2024 14:11:27 +0200 Subject: [PATCH] Bunch of fixes --- .gitignore | 1 + video_creation/final_video.py | 6 +-- video_creation/memes.py | 1 + video_creation/screenshot_downloader.py | 50 +++++++++++++++++++++---- 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 0b8935d..da12cfe 100644 --- a/.gitignore +++ b/.gitignore @@ -240,6 +240,7 @@ out .DS_Store .setup-done-before results/* +clipped/* reddit-bot-351418-5560ebc49cac.json /.idea *.pyc diff --git a/video_creation/final_video.py b/video_creation/final_video.py index a0fa76a..8ed9de9 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -203,7 +203,7 @@ def make_final_video( "No audio clips to gather. Please use a different TTS or post." ) # This is to fix the TypeError: unsupported operand type(s) for +: 'int' and 'NoneType' exit() - if settings.config["settings"]["storymode"]: + if settings.config["settings"]["storymode"] and not settings.config["settings"]["mememode"]: if settings.config["settings"]["storymodemethod"] == 0: audio_clips = [ffmpeg.input(f"assets/temp/{reddit_id}/mp3/title.mp3")] audio_clips.insert(1, ffmpeg.input(f"assets/temp/{reddit_id}/mp3/postaudio.mp3")) @@ -231,7 +231,7 @@ def make_final_video( audio_concat = ffmpeg.concat(*audio_clips, a=1, v=0) ffmpeg.output( audio_concat, f"assets/temp/{reddit_id}/audio.mp3", **{"b:a": "192k"} - ).overwrite_output().run(quiet=True) + ).overwrite_output().run(quiet=False) console.log(f"[bold green] Video Will Be: {length} Seconds Long") @@ -241,7 +241,7 @@ def make_final_video( image_clips = list() - if settings.config["settings"]["storymode"]: + if not settings.config["settings"]["mememode"] and settings.config["settings"]["storymodemethod"] == 1: Path(f"assets/temp/{reddit_id}/png").mkdir(parents=True, exist_ok=True) # Copyright 2024 beingbored (aka. Tim), MIT License, permission granted to use, copy, modify, and distribute. diff --git a/video_creation/memes.py b/video_creation/memes.py index 0dd621d..18748e5 100644 --- a/video_creation/memes.py +++ b/video_creation/memes.py @@ -1,3 +1,4 @@ +import os from utils import settings from moviepy.editor import VideoFileClip, concatenate_videoclips diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index d2ead6d..3e9ef40 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -14,7 +14,6 @@ from utils.console import print_step, print_substep from utils.imagenarator import imagemaker from utils.playwright import clear_cookie_by_name from utils.videos import save_data -from video_creation.final_video import name_normalize __all__ = ["download_screenshots_of_reddit_posts"] @@ -25,9 +24,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): reddit_object (Dict): Reddit object received from reddit/subreddit.py screenshot_num (int): Number of screenshots to download """ - - if settings.config["settings"]["storymodemethod"] == 0: - return + # settings values W: Final[int] = int(settings.config["settings"]["resolution_w"]) H: Final[int] = int(settings.config["settings"]["resolution_h"]) @@ -73,6 +70,9 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): txtclr=txtcolor, transparent=transparent, ) + + if settings.config["settings"]["storymodemethod"] == 1: + return screenshot_num: int with sync_playwright() as p: @@ -134,7 +134,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): # Reload the page for the redesign to take effect page.reload() # Get the thread screenshot - page.goto(reddit_object["thread_url"], timeout=0) + page.goto(reddit_object["thread_url"].replace("//r", "/r"), timeout=0) page.set_viewport_size(ViewportSize(width=W, height=H)) page.wait_for_load_state() page.wait_for_timeout(5000) @@ -173,8 +173,42 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): else: print_substep("Skipping translation...") + if mememode or settings.config["settings"]["storymodemethod"] == 0: + postcontentpath = f"assets/temp/{reddit_id}/png/title.png" + try: + if settings.config["settings"]["zoom"] != 1: + # store zoom settings + zoom = settings.config["settings"]["zoom"] + # zoom the body of the page + page.evaluate("document.body.style.zoom=" + str(zoom)) + # as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom + location = page.locator('[view-context="CommentsPage"]').bounding_box() # view-context="CommentsPage" + for i in location: + location[i] = float("{:.2f}".format(location[i] * zoom)) + page.screenshot(clip=location, path=postcontentpath) + else: + page.locator('[view-context="CommentsPage"]').screenshot(path=postcontentpath) + except Exception as e: + print_substep("Something went wrong!", style="red") + resp = input( + "Something went wrong with making the screenshots! Do you want to skip the post? (y/n) " + ) + + if resp.casefold().startswith("y"): + save_data("", "", "skipped", reddit_id, "") + print_substep( + "The post is successfully skipped! You can now restart the program and this post will skipped.", + "green", + ) + + resp = input("Do you want the error traceback for debugging purposes? (y/n)") + if not resp.casefold().startswith("y"): + exit() + + raise e + if storymode and not mememode: - page.locator('[data-click-id="text"]').first.screenshot( + page.locator('[data-post-click-location="text-body"]').first.screenshot( path=f"assets/temp/{reddit_id}/png/story_content.png" ) elif not mememode: @@ -184,6 +218,8 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): "Downloading screenshots...", ) ): + + print(f"https://www.reddit.com{comment['comment_url']}") # Stop if we have reached the screenshot_num if idx >= screenshot_num: break @@ -191,7 +227,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): if page.locator('[data-testid="content-gate"]').is_visible(): page.locator('[data-testid="content-gate"] button').click() - page.goto(f"https://new.reddit.com/{comment['comment_url']}") + page.goto(f"https://www.reddit.com{comment['comment_url']}") # translate code