|
|
|
@ -11,7 +11,7 @@ from rich.progress import track
|
|
|
|
|
|
|
|
|
|
from utils import settings
|
|
|
|
|
from utils.console import print_step, print_substep
|
|
|
|
|
from utils.imagenarator import imagemaker
|
|
|
|
|
from utils.imagenarator import imagemaker, comment_image_maker
|
|
|
|
|
from utils.playwright import clear_cookie_by_name
|
|
|
|
|
from utils.videos import save_data
|
|
|
|
|
|
|
|
|
@ -98,41 +98,9 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
|
|
|
|
|
context.add_cookies(cookies) # load preference cookies
|
|
|
|
|
|
|
|
|
|
# Login to Reddit
|
|
|
|
|
print_substep("Logging in to Reddit...")
|
|
|
|
|
print_substep("Opening Reddit... ")
|
|
|
|
|
page = context.new_page()
|
|
|
|
|
page.goto("https://www.reddit.com/login", timeout=0)
|
|
|
|
|
page.set_viewport_size(ViewportSize(width=1920, height=1080))
|
|
|
|
|
page.wait_for_load_state()
|
|
|
|
|
|
|
|
|
|
page.locator('[autocomplete="username"]').fill(settings.config["reddit"]["creds"]["username"])
|
|
|
|
|
page.locator('[autocomplete="current-password"]').fill(settings.config["reddit"]["creds"]["password"])
|
|
|
|
|
page.get_by_role("button", name="Log In").click()
|
|
|
|
|
page.wait_for_timeout(5000)
|
|
|
|
|
|
|
|
|
|
login_error_div = page.locator(".AnimatedForm__errorMessage").first
|
|
|
|
|
if login_error_div.is_visible():
|
|
|
|
|
login_error_message = login_error_div.inner_text()
|
|
|
|
|
if login_error_message.strip() == "":
|
|
|
|
|
# The div element is empty, no error
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
# The div contains an error message
|
|
|
|
|
print_substep(
|
|
|
|
|
"Your reddit credentials are incorrect! Please modify them accordingly in the config.toml file.",
|
|
|
|
|
style="red",
|
|
|
|
|
)
|
|
|
|
|
exit()
|
|
|
|
|
else:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
page.wait_for_load_state()
|
|
|
|
|
# Handle the redesign
|
|
|
|
|
# Check if the redesign optout cookie is set
|
|
|
|
|
if page.locator("#redesign-beta-optin-btn").is_visible():
|
|
|
|
|
# Clear the redesign optout cookie
|
|
|
|
|
clear_cookie_by_name(context, "redesign_optout")
|
|
|
|
|
# Reload the page for the redesign to take effect
|
|
|
|
|
page.reload()
|
|
|
|
|
# Get the thread screenshot
|
|
|
|
|
page.goto(reddit_object["thread_url"].replace("//r", "/r"), timeout=0)
|
|
|
|
|
page.set_viewport_size(ViewportSize(width=W, height=H))
|
|
|
|
@ -211,63 +179,6 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
|
|
|
|
|
page.locator('[data-post-click-location="text-body"]').first.screenshot(
|
|
|
|
|
path=f"assets/temp/{reddit_id}/png/story_content.png"
|
|
|
|
|
)
|
|
|
|
|
elif not mememode:
|
|
|
|
|
for idx, comment in enumerate(
|
|
|
|
|
track(
|
|
|
|
|
reddit_object["comments"][:screenshot_num],
|
|
|
|
|
"Downloading screenshots...",
|
|
|
|
|
)
|
|
|
|
|
):
|
|
|
|
|
|
|
|
|
|
print(f"https://www.reddit.com{comment['comment_url']}")
|
|
|
|
|
# Stop if we have reached the screenshot_num
|
|
|
|
|
if idx >= screenshot_num:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
if page.locator('[data-testid="content-gate"]').is_visible():
|
|
|
|
|
page.locator('[data-testid="content-gate"] button').click()
|
|
|
|
|
|
|
|
|
|
page.goto(f"https://www.reddit.com/{comment['comment_url']}")
|
|
|
|
|
|
|
|
|
|
# translate code
|
|
|
|
|
|
|
|
|
|
if settings.config["reddit"]["thread"]["post_lang"]:
|
|
|
|
|
comment_tl = translators.translate_text(
|
|
|
|
|
comment["comment_body"],
|
|
|
|
|
translator="google",
|
|
|
|
|
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',
|
|
|
|
|
[comment_tl, comment["comment_id"]],
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
if settings.config["settings"]["zoom"] != 1:
|
|
|
|
|
# store zoom settings
|
|
|
|
|
zoom = settings.config["settings"]["zoom"]
|
|
|
|
|
# zoom the body of the page
|
|
|
|
|
page.evaluate("document.body.style.zoom=" + str(zoom))
|
|
|
|
|
|
|
|
|
|
# scroll comment into view
|
|
|
|
|
page.locator(f"#thing_t1_{comment['comment_id']} > div.entry.unvoted").scroll_into_view_if_needed()
|
|
|
|
|
# as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom
|
|
|
|
|
location = page.locator(f"#thing_t1_{comment['comment_id']} > div.entry.unvoted").bounding_box()#thing_t1_l37rczw > div.entry.unvoted
|
|
|
|
|
|
|
|
|
|
for i in location:
|
|
|
|
|
location[i] = float("{:.2f}".format(location[i] * zoom))
|
|
|
|
|
page.screenshot(
|
|
|
|
|
clip=location,
|
|
|
|
|
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png",
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
page.locator(f"#t1_{comment['comment_id']}").screenshot(
|
|
|
|
|
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png"
|
|
|
|
|
)
|
|
|
|
|
except TimeoutError:
|
|
|
|
|
del reddit_object["comments"]
|
|
|
|
|
screenshot_num += 1
|
|
|
|
|
print("TimeoutError: Skipping screenshot...")
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# close browser instance when we are done using it
|
|
|
|
|
browser.close()
|
|
|
|
|