Update screenshot_downloader.py

pull/2363/head
erzurum2525 2 months ago committed by GitHub
parent 64bf647de9
commit a0a055205b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -177,12 +177,12 @@ 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() location = page.locator('shreddit-post').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) page.locator('shreddit-post').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(
@ -222,8 +222,6 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
page.goto(f"https://new.reddit.com/{comment['comment_url']}") page.goto(f"https://new.reddit.com/{comment['comment_url']}")
# translate code
if settings.config["reddit"]["thread"]["post_lang"]: if settings.config["reddit"]["thread"]["post_lang"]:
comment_tl = translators.translate_text( comment_tl = translators.translate_text(
comment["comment_body"], comment["comment_body"],
@ -231,27 +229,70 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int):
to_language=settings.config["reddit"]["thread"]["post_lang"], to_language=settings.config["reddit"]["thread"]["post_lang"],
) )
page.evaluate( page.evaluate(
'([tl_content, tl_id]) => document.querySelector(`#t1_${tl_id} > div:nth-child(2) > div > div[data-testid="comment"] > div`).textContent = tl_content', '([tl_content, tl_id]) => document.querySelector(`shreddit-comment[thingid="t1_${tl_id}"] > div:nth-child(2) > div > div[data-testid="comment"] > div`).textContent = tl_content',
[comment_tl, comment["comment_id"]], [comment_tl, comment["comment_id"]],
) )
try: try:
target = f'shreddit-comment[thingid="t1_{comment["comment_id"]}"] div#t1_{comment["comment_id"]}-comment-rtjson-content'
visible = page.locator(target).is_visible()
if not visible:
class ElementVisible(Exception):pass
try:
for _ in range(30):
page.evaluate("""
(target) => {
const element = document.querySelector(target);
if (element) {
element.style.display = 'block'; // 'inline'
element.style.visibility = 'visible';
}
}
""", target)
page.wait_for_timeout(1000)
visible = page.locator(target).is_visible()
if visible:
raise ElementVisible
target = f'shreddit-comment[thingid="t1_{comment["comment_id"]}"] div#t1_{comment["comment_id"]}-comment-rtjson-content div#-post-rtjson-content'
visible = page.locator(target).is_visible()
if not visible:
for _ in range(30):
page.evaluate("""
(target) => {
const element = document.querySelector(target);
if (element) {
element.style.display = 'block'; // 'inline'
element.style.visibility = 'visible';
}
}
""", target)
page.wait_for_timeout(1000)
visible = page.locator(target).is_visible()
if visible:
raise ElementVisible
target = f'shreddit-comment[thingid="t1_{comment["comment_id"]}"]'
except ElementVisible:
pass
if settings.config["settings"]["zoom"] != 1: if settings.config["settings"]["zoom"] != 1:
# store zoom settings # store zoom settings
zoom = settings.config["settings"]["zoom"] zoom = settings.config["settings"]["zoom"]
# 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() page.locator(target).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']}").bounding_box() # Adjust for the zoom and get bounding box of the target element
location = page.locator(target).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(
clip=location, clip=location,
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( page.locator(target).screenshot(
path=f"assets/temp/{reddit_id}/png/comment_{idx}.png" path=f"assets/temp/{reddit_id}/png/comment_{idx}.png"
) )
except TimeoutError: except TimeoutError:

Loading…
Cancel
Save