pull/1960/merge
Ali AbuSaleh 2 years ago committed by GitHub
commit b7ef3f254c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -12,7 +12,7 @@ jobs:
issues: write issues: write
pull-requests: write pull-requests: write
steps: steps:
- uses: actions/stale@v4 - uses: actions/stale@v9
id: stale-issue id: stale-issue
name: stale-issue name: stale-issue
with: with:
@ -31,7 +31,7 @@ jobs:
ascending: true ascending: true
#debug-only: true #debug-only: true
- uses: actions/stale@v4 - uses: actions/stale@v9
id: stale-pr id: stale-pr
name: stale-pr name: stale-pr
with: with:

@ -87,7 +87,7 @@ Please read our [contributing guidelines](CONTRIBUTING.md) for more detailed inf
Elebumm (Lewis#6305) - https://github.com/elebumm (Founder) Elebumm (Lewis#6305) - https://github.com/elebumm (Founder)
Jason (JasonLovesDoggo#1904) - https://github.com/JasonLovesDoggo (Maintainer) Jason (personality.json) - https://github.com/JasonLovesDoggo (Maintainer)
Simon (OpenSourceSimon) - https://github.com/OpenSourceSimon Simon (OpenSourceSimon) - https://github.com/OpenSourceSimon

@ -73,7 +73,7 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
print_substep("Launching Headless Browser...") print_substep("Launching Headless Browser...")
browser = p.chromium.launch( browser = p.chromium.launch(
headless=True headless=False
) # headless=False will show the browser for debugging purposes ) # headless=False will show the browser for debugging purposes
# 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
@ -97,10 +97,15 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
page.goto("https://www.reddit.com/login", timeout=0) page.goto("https://www.reddit.com/login", timeout=0)
page.set_viewport_size(ViewportSize(width=1920, height=1080)) page.set_viewport_size(ViewportSize(width=1920, height=1080))
page.wait_for_load_state() page.wait_for_load_state()
if selector_exists(page, "input#login-username"):
page.locator('[name="username"]').fill(settings.config["reddit"]["creds"]["username"]) page.locator('input#login-username').fill(settings.config["reddit"]["creds"]["username"])
page.locator('[name="password"]').fill(settings.config["reddit"]["creds"]["password"]) page.locator('input#login-password').fill(settings.config["reddit"]["creds"]["password"])
page.locator("button[class$='m-full-width']").click() page.locator("div[slot='primaryButton'] button.login").click()
else:
page.locator('[name="username"]').fill(settings.config["reddit"]["creds"]["username"])
page.locator('[name="password"]').fill(settings.config["reddit"]["creds"]["password"])
page.locator("button[class$='m-full-width']").click()
page.wait_for_timeout(5000) page.wait_for_timeout(5000)
login_error_div = page.locator(".AnimatedForm__errorMessage").first login_error_div = page.locator(".AnimatedForm__errorMessage").first
@ -175,12 +180,19 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
# zoom the body of the page # zoom the body of the page
page.evaluate("document.body.style.zoom=" + str(zoom)) page.evaluate("document.body.style.zoom=" + str(zoom))
# as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom # as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom
location = page.locator('[data-test-id="post-content"]').bounding_box() if selector_exists(page, '[data-test-id="post-content"]'):
location = page.locator('[data-test-id="post-content"]').bounding_box()
else:
location = page.locator(f'#t3_{reddit_id}').bounding_box()
for i in location: for i in location:
location[i] = float("{:.2f}".format(location[i] * zoom)) location[i] = float("{:.2f}".format(location[i] * zoom))
page.screenshot(clip=location, path=postcontentpath) page.screenshot(clip=location, path=postcontentpath)
else: else:
page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath) if selector_exists(page, '[data-test-id="post-content"]'):
page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath)
else:
page.locator(f'#t3_{reddit_id}').screenshot(path=postcontentpath)
except Exception as e: except Exception as e:
print_substep("Something went wrong!", style="red") print_substep("Something went wrong!", style="red")
resp = input( resp = input(
@ -211,6 +223,8 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
"Downloading screenshots...", "Downloading screenshots...",
) )
): ):
print_substep(f"Downloading screenshot {idx + 1} of {screenshot_num}...")
print_substep(f"Comment: {comment['comment_body']}")
# Stop if we have reached the screenshot_num # Stop if we have reached the screenshot_num
if idx >= screenshot_num: if idx >= screenshot_num:
break break
@ -239,9 +253,13 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
# zoom the body of the page # zoom the body of the page
page.evaluate("document.body.style.zoom=" + str(zoom)) page.evaluate("document.body.style.zoom=" + str(zoom))
# scroll comment into view # scroll comment into view
page.locator(f"#t1_{comment['comment_id']}").scroll_into_view_if_needed() if selector_exists(page, '[data-testid="comment"]'):
# as zooming the body doesn't change the properties of the divs, we need to adjust for the zoom page.locator(f"#t1_{comment['comment_id']}").scroll_into_view_if_needed()
location = page.locator(f"#t1_{comment['comment_id']}").bounding_box() location = page.locator(f"#t1_{comment['comment_id']}").bounding_box()
else:
page.locator(f"#t1_{comment['comment_id']}-comment-rtjson-content").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"#t1_{comment['comment_id']}-comment-rtjson-content").bounding_box()
for i in location: for i in location:
location[i] = float("{:.2f}".format(location[i] * zoom)) location[i] = float("{:.2f}".format(location[i] * zoom))
page.screenshot( page.screenshot(
@ -249,9 +267,14 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png", path=f"assets/temp/{reddit_id}/png/comment_{idx}.png",
) )
else: else:
page.locator(f"#t1_{comment['comment_id']}").screenshot( if selector_exists(page, f"#t1_{comment['comment_id']}"):
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png" page.locator(f"#t1_{comment['comment_id']}").screenshot(
) path=f"assets/temp/{reddit_id}/png/comment_{idx}.png"
)
else:
page.locator(f"#t1_{comment['comment_id']}-comment-rtjson-content").screenshot(
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png"
)
except TimeoutError: except TimeoutError:
del reddit_object["comments"] del reddit_object["comments"]
screenshot_num += 1 screenshot_num += 1
@ -262,3 +285,13 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
browser.close() browser.close()
print_substep("Screenshots downloaded Successfully.", style="bold green") print_substep("Screenshots downloaded Successfully.", style="bold green")
def selector_exists(page, selector, timeout=3000):
try:
element = page.locator(selector).is_visible(timeout=timeout)
return element
except:
return False
Loading…
Cancel
Save