Path: blob/master/video_creation/screenshot_downloader.py
327 views
import json1import re2from pathlib import Path3from typing import Dict, Final45import translators6from playwright.sync_api import ViewportSize, sync_playwright7from rich.progress import track89from utils import settings10from utils.console import print_step, print_substep11from utils.imagenarator import imagemaker12from utils.playwright import clear_cookie_by_name13from utils.videos import save_data1415__all__ = ["get_screenshots_of_reddit_posts"]161718def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):19"""Downloads screenshots of reddit posts as seen on the web. Downloads to assets/temp/png2021Args:22reddit_object (Dict): Reddit object received from reddit/subreddit.py23screenshot_num (int): Number of screenshots to download24"""25# settings values26W: Final[int] = int(settings.config["settings"]["resolution_w"])27H: Final[int] = int(settings.config["settings"]["resolution_h"])28lang: Final[str] = settings.config["reddit"]["thread"]["post_lang"]29storymode: Final[bool] = settings.config["settings"]["storymode"]3031print_step("Downloading screenshots of reddit posts...")32reddit_id = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"])33# ! Make sure the reddit screenshots folder exists34Path(f"assets/temp/{reddit_id}/png").mkdir(parents=True, exist_ok=True)3536# set the theme and disable non-essential cookies37if settings.config["settings"]["theme"] == "dark":38cookie_file = open("./video_creation/data/cookie-dark-mode.json", encoding="utf-8")39bgcolor = (33, 33, 36, 255)40txtcolor = (240, 240, 240)41transparent = False42elif settings.config["settings"]["theme"] == "transparent":43if storymode:44# Transparent theme45bgcolor = (0, 0, 0, 0)46txtcolor = (255, 255, 255)47transparent = True48cookie_file = open("./video_creation/data/cookie-dark-mode.json", encoding="utf-8")49else:50# Switch to dark theme51cookie_file = open("./video_creation/data/cookie-dark-mode.json", encoding="utf-8")52bgcolor = (33, 33, 36, 255)53txtcolor = (240, 240, 240)54transparent = False55else:56cookie_file = open("./video_creation/data/cookie-light-mode.json", encoding="utf-8")57bgcolor = (255, 255, 255, 255)58txtcolor = (0, 0, 0)59transparent = False6061if storymode and settings.config["settings"]["storymodemethod"] == 1:62# for idx,item in enumerate(reddit_object["thread_post"]):63print_substep("Generating images...")64return imagemaker(65theme=bgcolor,66reddit_obj=reddit_object,67txtclr=txtcolor,68transparent=transparent,69)7071screenshot_num: int72with sync_playwright() as p:73print_substep("Launching Headless Browser...")7475browser = p.chromium.launch(76headless=True77) # headless=False will show the browser for debugging purposes78# Device scale factor (or dsf for short) allows us to increase the resolution of the screenshots79# When the dsf is 1, the width of the screenshot is 600 pixels80# so we need a dsf such that the width of the screenshot is greater than the final resolution of the video81dsf = (W // 600) + 18283context = browser.new_context(84locale=lang or "en-us",85color_scheme="dark",86viewport=ViewportSize(width=W, height=H),87device_scale_factor=dsf,88user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",89)90cookies = json.load(cookie_file)91cookie_file.close()9293context.add_cookies(cookies) # load preference cookies9495# Login to Reddit96print_substep("Logging in to Reddit...")97page = context.new_page()98page.goto("https://www.reddit.com/login", timeout=0)99page.set_viewport_size(ViewportSize(width=1920, height=1080))100page.wait_for_load_state()101102page.locator(f'input[name="username"]').fill(settings.config["reddit"]["creds"]["username"])103page.locator(f'input[name="password"]').fill(settings.config["reddit"]["creds"]["password"])104page.get_by_role("button", name="Log In").click()105page.wait_for_timeout(5000)106107login_error_div = page.locator(".AnimatedForm__errorMessage").first108if login_error_div.is_visible():109login_error_message = login_error_div.inner_text()110if login_error_message.strip() == "":111# The div element is empty, no error112pass113else:114# The div contains an error message115print_substep(116"Your reddit credentials are incorrect! Please modify them accordingly in the config.toml file.",117style="red",118)119exit()120else:121pass122123page.wait_for_load_state()124# Handle the redesign125# Check if the redesign optout cookie is set126if page.locator("#redesign-beta-optin-btn").is_visible():127# Clear the redesign optout cookie128clear_cookie_by_name(context, "redesign_optout")129# Reload the page for the redesign to take effect130page.reload()131# Get the thread screenshot132page.goto(reddit_object["thread_url"], timeout=0)133page.set_viewport_size(ViewportSize(width=W, height=H))134page.wait_for_load_state()135page.wait_for_timeout(5000)136137if page.locator(138"#t3_12hmbug > div > div._3xX726aBn29LDbsDtzr_6E._1Ap4F5maDtT1E1YuCiaO0r.D3IL3FD0RFy_mkKLPwL4 > div > div > button"139).is_visible():140# This means the post is NSFW and requires to click the proceed button.141142print_substep("Post is NSFW. You are spicy...")143page.locator(144"#t3_12hmbug > div > div._3xX726aBn29LDbsDtzr_6E._1Ap4F5maDtT1E1YuCiaO0r.D3IL3FD0RFy_mkKLPwL4 > div > div > button"145).click()146page.wait_for_load_state() # Wait for page to fully load147148# translate code149if page.locator(150"#SHORTCUT_FOCUSABLE_DIV > div:nth-child(7) > div > div > div > header > div > div._1m0iFpls1wkPZJVo38-LSh > button > i"151).is_visible():152page.locator(153"#SHORTCUT_FOCUSABLE_DIV > div:nth-child(7) > div > div > div > header > div > div._1m0iFpls1wkPZJVo38-LSh > button > i"154).click() # Interest popup is showing, this code will close it155156if lang:157print_substep("Translating post...")158texts_in_tl = translators.translate_text(159reddit_object["thread_title"],160to_language=lang,161translator="google",162)163164page.evaluate(165"tl_content => document.querySelector('[data-adclicklocation=\"title\"] > div > div > h1').textContent = tl_content",166texts_in_tl,167)168else:169print_substep("Skipping translation...")170171postcontentpath = f"assets/temp/{reddit_id}/png/title.png"172try:173if settings.config["settings"]["zoom"] != 1:174# store zoom settings175zoom = settings.config["settings"]["zoom"]176# zoom the body of the page177page.evaluate("document.body.style.zoom=" + str(zoom))178# as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom179location = page.locator('[data-test-id="post-content"]').bounding_box()180for i in location:181location[i] = float("{:.2f}".format(location[i] * zoom))182page.screenshot(clip=location, path=postcontentpath)183else:184page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath)185except Exception as e:186print_substep("Something went wrong!", style="red")187resp = input(188"Something went wrong with making the screenshots! Do you want to skip the post? (y/n) "189)190191if resp.casefold().startswith("y"):192save_data("", "", "skipped", reddit_id, "")193print_substep(194"The post is successfully skipped! You can now restart the program and this post will skipped.",195"green",196)197198resp = input("Do you want the error traceback for debugging purposes? (y/n)")199if not resp.casefold().startswith("y"):200exit()201202raise e203204if storymode:205page.locator('[data-click-id="text"]').first.screenshot(206path=f"assets/temp/{reddit_id}/png/story_content.png"207)208else:209for idx, comment in enumerate(210track(211reddit_object["comments"][:screenshot_num],212"Downloading screenshots...",213)214):215# Stop if we have reached the screenshot_num216if idx >= screenshot_num:217break218219if page.locator('[data-testid="content-gate"]').is_visible():220page.locator('[data-testid="content-gate"] button').click()221222page.goto(f"https://new.reddit.com/{comment['comment_url']}")223224# translate code225226if settings.config["reddit"]["thread"]["post_lang"]:227comment_tl = translators.translate_text(228comment["comment_body"],229translator="google",230to_language=settings.config["reddit"]["thread"]["post_lang"],231)232page.evaluate(233'([tl_content, tl_id]) => document.querySelector(`#t1_${tl_id} > div:nth-child(2) > div > div[data-testid="comment"] > div`).textContent = tl_content',234[comment_tl, comment["comment_id"]],235)236try:237if settings.config["settings"]["zoom"] != 1:238# store zoom settings239zoom = settings.config["settings"]["zoom"]240# zoom the body of the page241page.evaluate("document.body.style.zoom=" + str(zoom))242# scroll comment into view243page.locator(f"#t1_{comment['comment_id']}").scroll_into_view_if_needed()244# as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom245location = page.locator(f"#t1_{comment['comment_id']}").bounding_box()246for i in location:247location[i] = float("{:.2f}".format(location[i] * zoom))248page.screenshot(249clip=location,250path=f"assets/temp/{reddit_id}/png/comment_{idx}.png",251)252else:253page.locator(f"#t1_{comment['comment_id']}").screenshot(254path=f"assets/temp/{reddit_id}/png/comment_{idx}.png"255)256except TimeoutError:257del reddit_object["comments"]258screenshot_num += 1259print("TimeoutError: Skipping screenshot...")260continue261262# close browser instance when we are done using it263browser.close()264265print_substep("Screenshots downloaded Successfully.", style="bold green")266267268