@ -1,10 +1,11 @@
from playwright . sync_api import sync_playwright
from playwright . sync_api import sync_playwright , ViewportSize
from pathlib import Path
from pathlib import Path
from rich . progress import track
from rich . progress import track
from utils . console import print_step , print_substep
from utils . console import print_step , print_substep
import json
def download_screenshots_of_reddit_posts ( reddit_object , screenshot_num ):
def download_screenshots_of_reddit_posts ( reddit_object , screenshot_num , theme ):
""" Downloads screenshots of reddit posts as they are seen on the web.
""" Downloads screenshots of reddit posts as they are seen on the web.
Args :
Args :
@ -20,11 +21,17 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num):
print_substep ( " Launching Headless Browser... " )
print_substep ( " Launching Headless Browser... " )
browser = p . chromium . launch ( )
browser = p . chromium . launch ( )
context = browser . new_context ( )
if theme . casefold ( ) == " dark " :
cookie_file = open ( ' video_creation/cookies.json ' )
cookies = json . load ( cookie_file )
context . add_cookies ( cookies )
# Get the thread screenshot
# Get the thread screenshot
page = browser . new_page ( )
page = context . new_page ( )
page . goto ( reddit_object [ " thread_url " ] )
page . goto ( reddit_object [ " thread_url " ] )
page . set_viewport_size ( ViewportSize ( width = 1920 , height = 1080 ) )
if page . locator ( ' [data-testid= " content-gate " ] ' ) . is_visible ( ) :
if page . locator ( ' [data-testid= " content-gate " ] ' ) . is_visible ( ) :
# This means the post is NSFW and requires to click the proceed button.
# This means the post is NSFW and requires to click the proceed button.
@ -50,4 +57,6 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num):
page . locator ( f " #t1_ { comment [ ' comment_id ' ] } " ) . screenshot (
page . locator ( f " #t1_ { comment [ ' comment_id ' ] } " ) . screenshot (
path = f " assets/png/comment_ { idx } .png "
path = f " assets/png/comment_ { idx } .png "
)
)
print_substep ( " Screenshots downloaded successfully. " , style = " bold green " )
print_substep ( " Screenshots downloaded Successfully. " ,
style = " bold green " )