12
12
from utils .console import print_step , print_substep
13
13
from utils .imagenarator import imagemaker
14
14
15
+ from utils .videos import save_data
15
16
16
17
__all__ = ["download_screenshots_of_reddit_posts" ]
17
18
19
+
18
20
def get_screenshots_of_reddit_posts (reddit_object : dict , screenshot_num : int ):
19
21
"""Downloads screenshots of reddit posts as seen on the web. Downloads to assets/temp/png
20
22
@@ -37,7 +39,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
37
39
with sync_playwright () as p :
38
40
print_substep ("Launching Headless Browser..." )
39
41
40
- browser = p .chromium .launch () # headless=False #to check for chrome view
42
+ browser = p .chromium .launch (headless = True ) # headless=False will show the browser for debugging purposes
41
43
context = browser .new_context ()
42
44
# Device scale factor (or dsf for short) allows us to increase the resolution of the screenshots
43
45
# When the dsf is 1, the width of the screenshot is 600 pixels
@@ -71,6 +73,20 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
71
73
72
74
context .add_cookies (cookies ) # load preference cookies
73
75
76
+ # Login to Reddit
77
+ print_substep ("Logging in to Reddit..." )
78
+ page = context .new_page ()
79
+ page .goto ("https://www.reddit.com/login" , timeout = 0 )
80
+ page .set_viewport_size (ViewportSize (width = 1920 , height = 1080 ))
81
+ page .wait_for_load_state ()
82
+
83
+ page .locator ('[name="username"]' ).fill (settings .config ["reddit" ]["creds" ]["username" ])
84
+ page .locator ('[name="password"]' ).fill (settings .config ["reddit" ]["creds" ]["password" ])
85
+ page .locator ("button:has-text('Log In')" ).click ()
86
+
87
+ page .wait_for_load_state () # Wait for page to fully load and add 5 seconds
88
+ page .wait_for_timeout (5000 )
89
+
74
90
# Get the thread screenshot
75
91
page = context .new_page ()
76
92
page .goto (reddit_object ["thread_url" ], timeout = 0 )
@@ -105,7 +121,21 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
105
121
print_substep ("Skipping translation..." )
106
122
107
123
postcontentpath = f"assets/temp/{ reddit_id } /png/title.png"
108
- page .locator ('[data-test-id="post-content"]' ).screenshot (path = postcontentpath )
124
+ try :
125
+ page .locator ('[data-test-id="post-content"]' ).screenshot (path = postcontentpath )
126
+ except Exception as e :
127
+ print_substep ("Something went wrong!" ,style = "red" )
128
+ resp = input ("Something went wrong with making the screenshots! Do you want to skip the post? (y/n) " )
129
+
130
+ if resp .casefold ().startswith ("y" ):
131
+ save_data ("" , "" , "skipped" , reddit_id , "" )
132
+ print_substep ("The post is successfully skipped! You can now restart the program and this post will skipped." ,"green" )
133
+
134
+ resp = input ("Do you want the error traceback for debugging purposes? (y/n)" )
135
+ if not resp .casefold ().startswith ("y" ):
136
+ exit ()
137
+
138
+ raise e
109
139
110
140
if storymode :
111
141
page .locator ('[data-click-id="text"]' ).first .screenshot (
@@ -151,6 +181,4 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
151
181
# close browser instance when we are done using it
152
182
browser .close ()
153
183
154
-
155
-
156
- print_substep ("Screenshots downloaded Successfully." , style = "bold green" )
184
+ print_substep ("Screenshots downloaded Successfully." , style = "bold green" )
0 commit comments