|
|
@ -12,9 +12,11 @@ from utils import settings
|
|
|
|
from utils.console import print_step, print_substep
|
|
|
|
from utils.console import print_step, print_substep
|
|
|
|
from utils.imagenarator import imagemaker
|
|
|
|
from utils.imagenarator import imagemaker
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from utils.videos import save_data
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ["download_screenshots_of_reddit_posts"]
|
|
|
|
__all__ = ["download_screenshots_of_reddit_posts"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
|
|
|
|
def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
|
|
|
|
"""Downloads screenshots of reddit posts as seen on the web. Downloads to assets/temp/png
|
|
|
|
"""Downloads screenshots of reddit posts as seen on the web. Downloads to assets/temp/png
|
|
|
|
|
|
|
|
|
|
|
@ -37,7 +39,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
|
|
|
|
with sync_playwright() as p:
|
|
|
|
with sync_playwright() as p:
|
|
|
|
print_substep("Launching Headless Browser...")
|
|
|
|
print_substep("Launching Headless Browser...")
|
|
|
|
|
|
|
|
|
|
|
|
browser = p.chromium.launch() # headless=False #to check for chrome view
|
|
|
|
browser = p.chromium.launch(headless=True) # headless=False will show the browser for debugging purposes
|
|
|
|
context = browser.new_context()
|
|
|
|
context = browser.new_context()
|
|
|
|
# Device scale factor (or dsf for short) allows us to increase the resolution of the screenshots
|
|
|
|
# Device scale factor (or dsf for short) allows us to increase the resolution of the screenshots
|
|
|
|
# When the dsf is 1, the width of the screenshot is 600 pixels
|
|
|
|
# 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):
|
|
|
|
|
|
|
|
|
|
|
|
context.add_cookies(cookies) # load preference cookies
|
|
|
|
context.add_cookies(cookies) # load preference cookies
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Login to Reddit
|
|
|
|
|
|
|
|
print_substep("Logging in to 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('[name="username"]').fill(settings.config["reddit"]["creds"]["username"])
|
|
|
|
|
|
|
|
page.locator('[name="password"]').fill(settings.config["reddit"]["creds"]["password"])
|
|
|
|
|
|
|
|
page.locator("button:has-text('Log In')").click()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
page.wait_for_load_state() # Wait for page to fully load and add 5 seconds
|
|
|
|
|
|
|
|
page.wait_for_timeout(5000)
|
|
|
|
|
|
|
|
|
|
|
|
# Get the thread screenshot
|
|
|
|
# Get the thread screenshot
|
|
|
|
page = context.new_page()
|
|
|
|
page = context.new_page()
|
|
|
|
page.goto(reddit_object["thread_url"], timeout=0)
|
|
|
|
page.goto(reddit_object["thread_url"], timeout=0)
|
|
|
@ -105,7 +121,21 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
|
|
|
|
print_substep("Skipping translation...")
|
|
|
|
print_substep("Skipping translation...")
|
|
|
|
|
|
|
|
|
|
|
|
postcontentpath = f"assets/temp/{reddit_id}/png/title.png"
|
|
|
|
postcontentpath = f"assets/temp/{reddit_id}/png/title.png"
|
|
|
|
|
|
|
|
try:
|
|
|
|
page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath)
|
|
|
|
page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
|
|
print_substep("Something went wrong!",style="red")
|
|
|
|
|
|
|
|
resp = input("Something went wrong with making the screenshots! Do you want to skip the post? (y/n) ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if resp.casefold().startswith("y"):
|
|
|
|
|
|
|
|
save_data("", "", "skipped", reddit_id, "")
|
|
|
|
|
|
|
|
print_substep("The post is successfully skipped! You can now restart the program and this post will skipped.","green")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resp = input("Do you want the error traceback for debugging purposes? (y/n)")
|
|
|
|
|
|
|
|
if not resp.casefold().startswith("y"):
|
|
|
|
|
|
|
|
exit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
|
|
if storymode:
|
|
|
|
if storymode:
|
|
|
|
page.locator('[data-click-id="text"]').first.screenshot(
|
|
|
|
page.locator('[data-click-id="text"]').first.screenshot(
|
|
|
@ -151,6 +181,4 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
|
|
|
|
# close browser instance when we are done using it
|
|
|
|
# close browser instance when we are done using it
|
|
|
|
browser.close()
|
|
|
|
browser.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print_substep("Screenshots downloaded Successfully.", style="bold green")
|
|
|
|
print_substep("Screenshots downloaded Successfully.", style="bold green")
|