Update screenshot_downloader.py

pull/2325/head
TechWIthTy 4 months ago committed by GitHub
parent 64bf647de9
commit b06773ea08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,3 +1,28 @@
Glitch
death305
Invisible
Glitch 12:11 PM
Hey what file do i need to change? How does this work with the reddit api change?
a[SYNX]
12:11 PM
It uses Reddit Apps, and uses a web scraper using your own reddit account to fetch reddit posts off subreddits
The file you need to change is: video_creation/screenshot_downloader.py
Glitch 12:12 PM
Ah okay i tried it a while ago
a[SYNX]
12:12 PM
Replace all code with this:
import json
import re
from pathlib import Path
from typing import Dict, Final
import translators
Expand
message.txt
15 KB

import json import json
import re import re
from pathlib import Path from pathlib import Path
@ -177,12 +202,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 +247,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 +254,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:
@ -264,3 +330,5 @@ 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")
message.txt
15 KB

Loading…
Cancel
Save