Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
elebumm
GitHub Repository: elebumm/RedditVideoMakerBot
Path: blob/master/video_creation/screenshot_downloader.py
327 views
1
import json
2
import re
3
from pathlib import Path
4
from typing import Dict, Final
5
6
import translators
7
from playwright.sync_api import ViewportSize, sync_playwright
8
from rich.progress import track
9
10
from utils import settings
11
from utils.console import print_step, print_substep
12
from utils.imagenarator import imagemaker
13
from utils.playwright import clear_cookie_by_name
14
from utils.videos import save_data
15
16
__all__ = ["get_screenshots_of_reddit_posts"]
17
18
19
def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
20
"""Downloads screenshots of reddit posts as seen on the web. Downloads to assets/temp/png
21
22
Args:
23
reddit_object (Dict): Reddit object received from reddit/subreddit.py
24
screenshot_num (int): Number of screenshots to download
25
"""
26
# settings values
27
W: Final[int] = int(settings.config["settings"]["resolution_w"])
28
H: Final[int] = int(settings.config["settings"]["resolution_h"])
29
lang: Final[str] = settings.config["reddit"]["thread"]["post_lang"]
30
storymode: Final[bool] = settings.config["settings"]["storymode"]
31
32
print_step("Downloading screenshots of reddit posts...")
33
reddit_id = re.sub(r"[^\w\s-]", "", reddit_object["thread_id"])
34
# ! Make sure the reddit screenshots folder exists
35
Path(f"assets/temp/{reddit_id}/png").mkdir(parents=True, exist_ok=True)
36
37
# set the theme and disable non-essential cookies
38
if settings.config["settings"]["theme"] == "dark":
39
cookie_file = open("./video_creation/data/cookie-dark-mode.json", encoding="utf-8")
40
bgcolor = (33, 33, 36, 255)
41
txtcolor = (240, 240, 240)
42
transparent = False
43
elif settings.config["settings"]["theme"] == "transparent":
44
if storymode:
45
# Transparent theme
46
bgcolor = (0, 0, 0, 0)
47
txtcolor = (255, 255, 255)
48
transparent = True
49
cookie_file = open("./video_creation/data/cookie-dark-mode.json", encoding="utf-8")
50
else:
51
# Switch to dark theme
52
cookie_file = open("./video_creation/data/cookie-dark-mode.json", encoding="utf-8")
53
bgcolor = (33, 33, 36, 255)
54
txtcolor = (240, 240, 240)
55
transparent = False
56
else:
57
cookie_file = open("./video_creation/data/cookie-light-mode.json", encoding="utf-8")
58
bgcolor = (255, 255, 255, 255)
59
txtcolor = (0, 0, 0)
60
transparent = False
61
62
if storymode and settings.config["settings"]["storymodemethod"] == 1:
63
# for idx,item in enumerate(reddit_object["thread_post"]):
64
print_substep("Generating images...")
65
return imagemaker(
66
theme=bgcolor,
67
reddit_obj=reddit_object,
68
txtclr=txtcolor,
69
transparent=transparent,
70
)
71
72
screenshot_num: int
73
with sync_playwright() as p:
74
print_substep("Launching Headless Browser...")
75
76
browser = p.chromium.launch(
77
headless=True
78
) # headless=False will show the browser for debugging purposes
79
# Device scale factor (or dsf for short) allows us to increase the resolution of the screenshots
80
# When the dsf is 1, the width of the screenshot is 600 pixels
81
# so we need a dsf such that the width of the screenshot is greater than the final resolution of the video
82
dsf = (W // 600) + 1
83
84
context = browser.new_context(
85
locale=lang or "en-us",
86
color_scheme="dark",
87
viewport=ViewportSize(width=W, height=H),
88
device_scale_factor=dsf,
89
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
90
)
91
cookies = json.load(cookie_file)
92
cookie_file.close()
93
94
context.add_cookies(cookies) # load preference cookies
95
96
# Login to Reddit
97
print_substep("Logging in to Reddit...")
98
page = context.new_page()
99
page.goto("https://www.reddit.com/login", timeout=0)
100
page.set_viewport_size(ViewportSize(width=1920, height=1080))
101
page.wait_for_load_state()
102
103
page.locator(f'input[name="username"]').fill(settings.config["reddit"]["creds"]["username"])
104
page.locator(f'input[name="password"]').fill(settings.config["reddit"]["creds"]["password"])
105
page.get_by_role("button", name="Log In").click()
106
page.wait_for_timeout(5000)
107
108
login_error_div = page.locator(".AnimatedForm__errorMessage").first
109
if login_error_div.is_visible():
110
login_error_message = login_error_div.inner_text()
111
if login_error_message.strip() == "":
112
# The div element is empty, no error
113
pass
114
else:
115
# The div contains an error message
116
print_substep(
117
"Your reddit credentials are incorrect! Please modify them accordingly in the config.toml file.",
118
style="red",
119
)
120
exit()
121
else:
122
pass
123
124
page.wait_for_load_state()
125
# Handle the redesign
126
# Check if the redesign optout cookie is set
127
if page.locator("#redesign-beta-optin-btn").is_visible():
128
# Clear the redesign optout cookie
129
clear_cookie_by_name(context, "redesign_optout")
130
# Reload the page for the redesign to take effect
131
page.reload()
132
# Get the thread screenshot
133
page.goto(reddit_object["thread_url"], timeout=0)
134
page.set_viewport_size(ViewportSize(width=W, height=H))
135
page.wait_for_load_state()
136
page.wait_for_timeout(5000)
137
138
if page.locator(
139
"#t3_12hmbug > div > div._3xX726aBn29LDbsDtzr_6E._1Ap4F5maDtT1E1YuCiaO0r.D3IL3FD0RFy_mkKLPwL4 > div > div > button"
140
).is_visible():
141
# This means the post is NSFW and requires to click the proceed button.
142
143
print_substep("Post is NSFW. You are spicy...")
144
page.locator(
145
"#t3_12hmbug > div > div._3xX726aBn29LDbsDtzr_6E._1Ap4F5maDtT1E1YuCiaO0r.D3IL3FD0RFy_mkKLPwL4 > div > div > button"
146
).click()
147
page.wait_for_load_state() # Wait for page to fully load
148
149
# translate code
150
if page.locator(
151
"#SHORTCUT_FOCUSABLE_DIV > div:nth-child(7) > div > div > div > header > div > div._1m0iFpls1wkPZJVo38-LSh > button > i"
152
).is_visible():
153
page.locator(
154
"#SHORTCUT_FOCUSABLE_DIV > div:nth-child(7) > div > div > div > header > div > div._1m0iFpls1wkPZJVo38-LSh > button > i"
155
).click() # Interest popup is showing, this code will close it
156
157
if lang:
158
print_substep("Translating post...")
159
texts_in_tl = translators.translate_text(
160
reddit_object["thread_title"],
161
to_language=lang,
162
translator="google",
163
)
164
165
page.evaluate(
166
"tl_content => document.querySelector('[data-adclicklocation=\"title\"] > div > div > h1').textContent = tl_content",
167
texts_in_tl,
168
)
169
else:
170
print_substep("Skipping translation...")
171
172
postcontentpath = f"assets/temp/{reddit_id}/png/title.png"
173
try:
174
if settings.config["settings"]["zoom"] != 1:
175
# store zoom settings
176
zoom = settings.config["settings"]["zoom"]
177
# zoom the body of the page
178
page.evaluate("document.body.style.zoom=" + str(zoom))
179
# as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom
180
location = page.locator('[data-test-id="post-content"]').bounding_box()
181
for i in location:
182
location[i] = float("{:.2f}".format(location[i] * zoom))
183
page.screenshot(clip=location, path=postcontentpath)
184
else:
185
page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath)
186
except Exception as e:
187
print_substep("Something went wrong!", style="red")
188
resp = input(
189
"Something went wrong with making the screenshots! Do you want to skip the post? (y/n) "
190
)
191
192
if resp.casefold().startswith("y"):
193
save_data("", "", "skipped", reddit_id, "")
194
print_substep(
195
"The post is successfully skipped! You can now restart the program and this post will skipped.",
196
"green",
197
)
198
199
resp = input("Do you want the error traceback for debugging purposes? (y/n)")
200
if not resp.casefold().startswith("y"):
201
exit()
202
203
raise e
204
205
if storymode:
206
page.locator('[data-click-id="text"]').first.screenshot(
207
path=f"assets/temp/{reddit_id}/png/story_content.png"
208
)
209
else:
210
for idx, comment in enumerate(
211
track(
212
reddit_object["comments"][:screenshot_num],
213
"Downloading screenshots...",
214
)
215
):
216
# Stop if we have reached the screenshot_num
217
if idx >= screenshot_num:
218
break
219
220
if page.locator('[data-testid="content-gate"]').is_visible():
221
page.locator('[data-testid="content-gate"] button').click()
222
223
page.goto(f"https://new.reddit.com/{comment['comment_url']}")
224
225
# translate code
226
227
if settings.config["reddit"]["thread"]["post_lang"]:
228
comment_tl = translators.translate_text(
229
comment["comment_body"],
230
translator="google",
231
to_language=settings.config["reddit"]["thread"]["post_lang"],
232
)
233
page.evaluate(
234
'([tl_content, tl_id]) => document.querySelector(`#t1_${tl_id} > div:nth-child(2) > div > div[data-testid="comment"] > div`).textContent = tl_content',
235
[comment_tl, comment["comment_id"]],
236
)
237
try:
238
if settings.config["settings"]["zoom"] != 1:
239
# store zoom settings
240
zoom = settings.config["settings"]["zoom"]
241
# zoom the body of the page
242
page.evaluate("document.body.style.zoom=" + str(zoom))
243
# scroll comment into view
244
page.locator(f"#t1_{comment['comment_id']}").scroll_into_view_if_needed()
245
# as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom
246
location = page.locator(f"#t1_{comment['comment_id']}").bounding_box()
247
for i in location:
248
location[i] = float("{:.2f}".format(location[i] * zoom))
249
page.screenshot(
250
clip=location,
251
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png",
252
)
253
else:
254
page.locator(f"#t1_{comment['comment_id']}").screenshot(
255
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png"
256
)
257
except TimeoutError:
258
del reddit_object["comments"]
259
screenshot_num += 1
260
print("TimeoutError: Skipping screenshot...")
261
continue
262
263
# close browser instance when we are done using it
264
browser.close()
265
266
print_substep("Screenshots downloaded Successfully.", style="bold green")
267
268