Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
elebumm
GitHub Repository: elebumm/RedditVideoMakerBot
Path: blob/master/video_creation/screenshot_downloader.py
494 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 turn off 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
print_substep("Generating images...")
64
return imagemaker(
65
theme=bgcolor,
66
reddit_obj=reddit_object,
67
txtclr=txtcolor,
68
transparent=transparent,
69
)
70
71
screenshot_num: int
72
with sync_playwright() as p:
73
print_substep("Launching Headless Browser...")
74
75
browser = p.chromium.launch(
76
headless=True
77
) # headless=False will show the browser for debugging purposes
78
# Device scale factor (or dsf for short) allows us to increase the resolution of the screenshots
79
# When the dsf is 1, the width of the screenshot is 600 pixels
80
# so we need a dsf such that the width of the screenshot is greater than the final resolution of the video
81
dsf = (W // 600) + 1
82
83
context = browser.new_context(
84
locale=lang or "en-CA,en;q=0.9",
85
color_scheme="dark",
86
viewport=ViewportSize(width=W, height=H),
87
device_scale_factor=dsf,
88
user_agent=f"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{browser.version}.0.0.0 Safari/537.36",
89
extra_http_headers={
90
"Dnt": "1",
91
"Sec-Ch-Ua": '"Not A(Brand";v="8", "Chromium";v="132", "Google Chrome";v="132"',
92
},
93
)
94
cookies = json.load(cookie_file)
95
cookie_file.close()
96
97
context.add_cookies(cookies) # load preference cookies
98
99
# Login to Reddit
100
print_substep("Logging in to Reddit...")
101
page = context.new_page()
102
page.goto("https://www.reddit.com/login", timeout=0)
103
page.set_viewport_size(ViewportSize(width=1920, height=1080))
104
page.wait_for_load_state()
105
106
page.locator(f'input[name="username"]').fill(settings.config["reddit"]["creds"]["username"])
107
page.locator(f'input[name="password"]').fill(settings.config["reddit"]["creds"]["password"])
108
page.get_by_role("button", name="Log In").click()
109
page.wait_for_timeout(5000)
110
111
login_error_div = page.locator(".AnimatedForm__errorMessage").first
112
if login_error_div.is_visible():
113
114
print_substep(
115
"Your reddit credentials are incorrect! Please modify them accordingly in the config.toml file.",
116
style="red",
117
)
118
exit()
119
else:
120
pass
121
122
page.wait_for_load_state()
123
# Handle the redesign
124
# Check if the redesign optout cookie is set
125
if page.locator("#redesign-beta-optin-btn").is_visible():
126
# Clear the redesign optout cookie
127
clear_cookie_by_name(context, "redesign_optout")
128
# Reload the page for the redesign to take effect
129
page.reload()
130
# Get the thread screenshot
131
page.goto(reddit_object["thread_url"], timeout=0)
132
page.set_viewport_size(ViewportSize(width=W, height=H))
133
page.wait_for_load_state()
134
page.wait_for_timeout(5000)
135
136
if page.locator(
137
"#t3_12hmbug > div > div._3xX726aBn29LDbsDtzr_6E._1Ap4F5maDtT1E1YuCiaO0r.D3IL3FD0RFy_mkKLPwL4 > div > div > button"
138
).is_visible():
139
# This means the post is NSFW and requires to click the proceed button.
140
141
print_substep("Post is NSFW. You are spicy...")
142
page.locator(
143
"#t3_12hmbug > div > div._3xX726aBn29LDbsDtzr_6E._1Ap4F5maDtT1E1YuCiaO0r.D3IL3FD0RFy_mkKLPwL4 > div > div > button"
144
).click()
145
page.wait_for_load_state() # Wait for page to fully load
146
147
# translate code
148
if page.locator(
149
"#SHORTCUT_FOCUSABLE_DIV > div:nth-child(7) > div > div > div > header > div > div._1m0iFpls1wkPZJVo38-LSh > button > i"
150
).is_visible():
151
page.locator(
152
"#SHORTCUT_FOCUSABLE_DIV > div:nth-child(7) > div > div > div > header > div > div._1m0iFpls1wkPZJVo38-LSh > button > i"
153
).click() # Interest popup is showing, this code will close it
154
155
if lang:
156
print_substep("Translating post...")
157
texts_in_tl = translators.translate_text(
158
reddit_object["thread_title"],
159
to_language=lang,
160
translator="google",
161
)
162
163
page.evaluate(
164
"tl_content => document.querySelector('[data-adclicklocation=\"title\"] > div > div > h1').textContent = tl_content",
165
texts_in_tl,
166
)
167
else:
168
print_substep("Skipping translation...")
169
170
postcontentpath = f"assets/temp/{reddit_id}/png/title.png"
171
try:
172
if settings.config["settings"]["zoom"] != 1:
173
# store zoom settings
174
zoom = settings.config["settings"]["zoom"]
175
# zoom the body of the page
176
page.evaluate("document.body.style.zoom=" + str(zoom))
177
# as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom
178
location = page.locator('[data-test-id="post-content"]').bounding_box()
179
for i in location:
180
location[i] = float("{:.2f}".format(location[i] * zoom))
181
page.screenshot(clip=location, path=postcontentpath)
182
else:
183
page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath)
184
except Exception as e:
185
print_substep("Something went wrong!", style="red")
186
resp = input(
187
"Something went wrong with making the screenshots! Do you want to skip the post? (y/n) "
188
)
189
190
if resp.casefold().startswith("y"):
191
save_data("", "", "skipped", reddit_id, "")
192
print_substep(
193
"The post is successfully skipped! You can now restart the program and this post will skipped.",
194
"green",
195
)
196
197
resp = input("Do you want the error traceback for debugging purposes? (y/n)")
198
if not resp.casefold().startswith("y"):
199
exit()
200
201
raise e
202
203
if storymode:
204
page.locator('[data-click-id="text"]').first.screenshot(
205
path=f"assets/temp/{reddit_id}/png/story_content.png"
206
)
207
else:
208
for idx, comment in enumerate(
209
track(
210
reddit_object["comments"][:screenshot_num],
211
"Downloading screenshots...",
212
)
213
):
214
# Stop if we have reached the screenshot_num
215
if idx >= screenshot_num:
216
break
217
218
if page.locator('[data-testid="content-gate"]').is_visible():
219
page.locator('[data-testid="content-gate"] button').click()
220
221
page.goto(f"https://new.reddit.com/{comment['comment_url']}")
222
223
# translate code
224
225
if settings.config["reddit"]["thread"]["post_lang"]:
226
comment_tl = translators.translate_text(
227
comment["comment_body"],
228
translator="google",
229
to_language=settings.config["reddit"]["thread"]["post_lang"],
230
)
231
page.evaluate(
232
'([tl_content, tl_id]) => document.querySelector(`#t1_${tl_id} > div:nth-child(2) > div > div[data-testid="comment"] > div`).textContent = tl_content',
233
[comment_tl, comment["comment_id"]],
234
)
235
try:
236
if settings.config["settings"]["zoom"] != 1:
237
# store zoom settings
238
zoom = settings.config["settings"]["zoom"]
239
# zoom the body of the page
240
page.evaluate("document.body.style.zoom=" + str(zoom))
241
# scroll comment into view
242
page.locator(f"#t1_{comment['comment_id']}").scroll_into_view_if_needed()
243
# as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom
244
location = page.locator(f"#t1_{comment['comment_id']}").bounding_box()
245
for i in location:
246
location[i] = float("{:.2f}".format(location[i] * zoom))
247
page.screenshot(
248
clip=location,
249
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png",
250
)
251
else:
252
page.locator(f"#t1_{comment['comment_id']}").screenshot(
253
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png"
254
)
255
except TimeoutError:
256
del reddit_object["comments"]
257
screenshot_num += 1
258
print("TimeoutError: Skipping screenshot...")
259
continue
260
261
# close browser instance when we are done using it
262
browser.close()
263
264
print_substep("Screenshots downloaded Successfully.", style="bold green")
265
266