@ -1,7 +1,7 @@
import json
import re
from pathlib import Path
from typing import Dict
from typing import Dict , Final
import translators as ts
from playwright . async_api import async_playwright # pylint: disable=unused-import
@ -12,8 +12,8 @@ from utils import settings
from utils . console import print_step , print_substep
from utils . imagenarator import imagemaker
# do not remove the above line
__all__ = [ " download_screenshots_of_reddit_posts " ]
def get_screenshots_of_reddit_posts ( reddit_object : dict , screenshot_num : int ) :
""" Downloads screenshots of reddit posts as seen on the web. Downloads to assets/temp/png
@ -22,13 +22,16 @@ 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
"""
# settings values
W : Final [ int ] = int ( settings . config [ " settings " ] [ " resolution_w " ] )
H : Final [ int ] = int ( settings . config [ " settings " ] [ " resolution_h " ] )
lang : Final [ str ] = settings . config [ " reddit " ] [ " thread " ] [ " post_lang " ]
storymode : Final [ bool ] = settings . config [ " settings " ] [ " storymode " ]
print_step ( " Downloading screenshots of reddit posts... " )
id = re . sub ( r " [^ \ w \ s-] " , " " , reddit_object [ " thread_id " ] )
reddit_id = re . sub ( r " [^ \ w \ s-] " , " " , reddit_object [ " thread_id " ] )
# ! Make sure the reddit screenshots folder exists
Path ( f " assets/temp/ { id} /png " ) . mkdir ( parents = True , exist_ok = True )
Path ( f " assets/temp/ { reddit_id } /png " ) . mkdir ( parents = True , exist_ok = True )
def download ( cookie_file , num = None ) :
screenshot_num = num
@ -38,13 +41,37 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
browser = p . chromium . launch ( ) # headless=False #to check for chrome view
context = browser . new_context ( )
# Device scale factor (or dsf for short) allows us to increase the resolution of the screenshots
# When the dsf is 1, the width of the screenshot is 600 pixels
# so we need a dsf such that the width of the screenshot is greater than the final resolution of the video
dsf = ( W / / 600 ) + 1
context = browser . new_context (
locale = lang or " en-us " ,
color_scheme = " dark " ,
viewport = ViewportSize ( width = W , height = H ) ,
device_scale_factor = dsf ,
)
# set the theme and disable non-essential cookies
if settings . config [ " settings " ] [ " theme " ] == " dark " :
cookie_file = open (
" ./video_creation/data/cookie-dark-mode.json " , encoding = " utf-8 "
)
else :
cookie_file = open (
" ./video_creation/data/cookie-light-mode.json " , encoding = " utf-8 "
)
cookies = json . load ( cookie_file )
cookie_file . close ( )
context . add_cookies ( cookies ) # load preference cookies
# Get the thread screenshot
page = context . new_page ( )
page . goto ( reddit_object [ " thread_url " ] , timeout = 0 )
page . set_viewport_size ( ViewportSize ( width = 1080 , height = 1920 ) )
page . set_viewport_size ( ViewportSize ( width = W , height = H ) )
if page . locator ( ' [data-testid= " content-gate " ] ' ) . is_visible ( ) :
# This means the post is NSFW and requires to click the proceed button.
@ -59,11 +86,11 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
# translate code
if settings. config [ " reddit " ] [ " thread " ] [ " post_ lang" ] :
if lang:
print_substep ( " Translating post... " )
texts_in_tl = ts . google (
reddit_object [ " thread_title " ] ,
to_language = settings. config [ " reddit " ] [ " thread " ] [ " post_ lang" ] ,
to_language = lang,
)
page . evaluate (
@ -72,22 +99,20 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
)
else :
print_substep ( " Skipping translation... " )
postcontentpath = f " assets/temp/ { id } /png/title.png "
page . locator ( ' [data-test-id= " post-content " ] ' ) . screenshot (
path = postcontentpath
)
if settings . config [ " settings " ] [ " storymode " ] :
postcontentpath = f " assets/temp/ { reddit_id } /png/title.png "
page . locator ( ' [data-test-id= " post-content " ] ' ) . screenshot ( path = postcontentpath )
try: # new change
page . locator ( ' [data-click-id= " text " ] ' ) . first . screenshot (
path = f " assets/temp/ { id} /png/story_content.png "
if s to rymode :
page . locator ( ' [data-click-id= " text " ] ' ) . screenshot (
path = f " assets/temp/ { reddit_ id} /png/story_content.png "
)
except :
exit
if not settings . config [ " settings " ] [ " storymode " ] :
else :
for idx , comment in enumerate (
track ( reddit_object [ " comments " ] , " Downloading screenshots... " )
track (
reddit_object [ " comments " ] [ : screenshot_num ] ,
" Downloading screenshots... " ,
)
) :
# Stop if we have reached the screenshot_num
if idx > = screenshot_num :
@ -103,9 +128,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
if settings . config [ " reddit " ] [ " thread " ] [ " post_lang " ] :
comment_tl = ts . google (
comment [ " comment_body " ] ,
to_language = settings . config [ " reddit " ] [ " thread " ] [
" post_lang "
] ,
to_language = settings . config [ " reddit " ] [ " thread " ] [ " post_lang " ] ,
)
page . evaluate (
' ([tl_content, tl_id]) => document.querySelector(`#t1_$ {tl_id} > div:nth-child(2) > div > div[data-testid= " comment " ] > div`).textContent = tl_content ' ,
@ -113,36 +136,15 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
)
try :
page . locator ( f " #t1_ { comment [ ' comment_id ' ] } " ) . screenshot (
path = f " assets/temp/ { id} /png/comment_ { idx } .png "
path = f " assets/temp/ { reddit_ id} /png/comment_ { idx } .png "
)
except TimeoutError :
del reddit_object [ " comments " ]
screenshot_num - = 1
screenshot_num + = 1
print ( " TimeoutError: Skipping screenshot... " )
continue
print_substep ( " Screenshots downloaded Successfully. " , style = " bold green " )
# story=False
theme = settings . config [ " settings " ] [ " theme " ]
if theme == " dark " :
cookie_file = open (
" ./video_creation/data/cookie-dark-mode.json " , encoding = " utf-8 "
)
bgcolor = ( 33 , 33 , 36 , 255 )
txtcolor = ( 240 , 240 , 240 )
else :
cookie_file = open (
" ./video_creation/data/cookie-light-mode.json " , encoding = " utf-8 "
)
bgcolor = ( 255 , 255 , 255 , 255 )
txtcolor = ( 0 , 0 , 0 )
if settings . config [ " settings " ] [ " storymode " ] :
if settings . config [ " settings " ] [ " storymodemethod " ] == 1 :
# for idx,item in enumerate(reddit_object["thread_post"]):
imagemaker ( theme = bgcolor , reddit_obj = reddit_object , txtclr = txtcolor )
if (
settings . config [ " settings " ] [ " storymodemethod " ] == 0
or not settings . config [ " settings " ] [ " storymode " ]
) :
download ( cookie_file , screenshot_num )
# close browser instance when we are done using it
browser . close ( )
print_substep ( " Screenshots downloaded Successfully. " , style = " bold green " )