No more screnshooting for askreddit and like that

pull/2041/head
cyteon 1 year ago
parent cf5dc82492
commit c2355af25b

@ -14,6 +14,8 @@ from utils import settings
from utils.console import print_step, print_substep from utils.console import print_step, print_substep
from utils.voice import sanitize_text from utils.voice import sanitize_text
DEFAULT_MAX_LENGTH: int = ( DEFAULT_MAX_LENGTH: int = (
50 # Video length variable, edit this on your own risk. It should work, but it's not supported 50 # Video length variable, edit this on your own risk. It should work, but it's not supported
) )
@ -52,7 +54,10 @@ class TTSEngine:
def add_periods( def add_periods(
self, self,
): # adds periods to the end of paragraphs (where people often forget to put them) so tts doesn't blend sentences ): # adds periods to the end of paragraphs (where people often forget to put them) so tts doesn't blend sentences
i = 1
for comment in self.reddit_object["comments"]: for comment in self.reddit_object["comments"]:
# remove links # remove links
regex_urls = r"((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*" regex_urls = r"((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*"
comment["comment_body"] = re.sub(regex_urls, " ", comment["comment_body"]) comment["comment_body"] = re.sub(regex_urls, " ", comment["comment_body"])
@ -67,6 +72,8 @@ class TTSEngine:
comment["comment_body"] = re.sub(r'\."\.', '".', comment["comment_body"]) comment["comment_body"] = re.sub(r'\."\.', '".', comment["comment_body"])
def run(self) -> Tuple[int, int]: def run(self) -> Tuple[int, int]:
from utils.imagenarator import comment_image_maker
Path(self.path).mkdir(parents=True, exist_ok=True) Path(self.path).mkdir(parents=True, exist_ok=True)
print_step("Saving Text to MP3 files...") print_step("Saving Text to MP3 files...")
@ -86,6 +93,7 @@ class TTSEngine:
self.call_tts(f"postaudio-{idx}", process_text(text)) self.call_tts(f"postaudio-{idx}", process_text(text))
else: else:
os.makedirs("assets/temp/" + self.redditid + "/png")
for idx, comment in track(enumerate(self.reddit_object["comments"]), "Saving..."): for idx, comment in track(enumerate(self.reddit_object["comments"]), "Saving..."):
# ! Stop creating mp3 files if the length is greater than max length. # ! Stop creating mp3 files if the length is greater than max length.
if self.length > self.max_length and idx > 1: if self.length > self.max_length and idx > 1:
@ -99,6 +107,8 @@ class TTSEngine:
else: # If the comment is not too long, just call the tts engine else: # If the comment is not too long, just call the tts engine
self.call_tts(f"{idx}", process_text(comment["comment_body"])) self.call_tts(f"{idx}", process_text(comment["comment_body"]))
comment_image_maker((0, 0, 0, 0), self.reddit_object, comment["comment_body"], idx, (255, 255, 255), transparent=True)
print_substep("Saved Text to MP3 files successfully.", style="bold green") print_substep("Saved Text to MP3 files successfully.", style="bold green")
return self.length, idx return self.length, idx

@ -2,6 +2,8 @@ import os
import re import re
import textwrap import textwrap
from utils import settings
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
from rich.progress import track from rich.progress import track
@ -23,7 +25,10 @@ def draw_multiple_line_text(
line_width, line_height = font.getsize(line) line_width, line_height = font.getsize(line)
if transparent: if transparent:
shadowcolor = "black" shadowcolor = "black"
for i in range(1, 5): for i in range(
1,
5 if settings.config["storymode"] else 8 # 5 was the normal value but increased to 8 for stuff like askreddit due to clipping
):
draw.text( draw.text(
((image_width - line_width) / 2 - i, y - i), ((image_width - line_width) / 2 - i, y - i),
line, line,
@ -62,21 +67,30 @@ def imagemaker(theme, reddit_obj: dict, txtclr, padding=5, transparent=False) ->
if transparent: if transparent:
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), 100) font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), 100)
tfont = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), 100)
else: else:
tfont = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), 100) # for title
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Regular.ttf"), 100) font = ImageFont.truetype(os.path.join("fonts", "Roboto-Regular.ttf"), 100)
size = (1920, 1080) size = (1920, 1080)
image = Image.new("RGBA", size, theme)
# for title
draw_multiple_line_text(image, title, tfont, txtclr, padding, wrap=30, transparent=transparent)
image.save(f"assets/temp/{id}/png/title.png")
for idx, text in track(enumerate(texts), "Rendering Image"): for idx, text in track(enumerate(texts), "Rendering Image"):
image = Image.new("RGBA", size, theme) image = Image.new("RGBA", size, theme)
text = process_text(text, False) text = process_text(text, False)
draw_multiple_line_text(image, text, font, txtclr, padding, wrap=30, transparent=transparent) draw_multiple_line_text(image, text, font, txtclr, padding, wrap=30, transparent=transparent)
image.save(f"assets/temp/{id}/png/img{idx}.png") image.save(f"assets/temp/{id}/png/img{idx}.png")
def comment_image_maker(theme, reddit_obj: dict, text, idx, txtclr, padding=5, transparent=False) -> None:
"""
Render Images for video
"""
id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"])
if transparent:
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Bold.ttf"), 100)
else:
font = ImageFont.truetype(os.path.join("fonts", "Roboto-Regular.ttf"), 100)
size = (1920, 1080)
#for idx, text in track(enumerate(texts), "Rendering Image"):
image = Image.new("RGBA", size, theme)
text = process_text(text, False)
draw_multiple_line_text(image, text, font, txtclr, padding, wrap=30, transparent=transparent)
image.save(f"assets/temp/{id}/png/comment_{idx}.png")

@ -11,7 +11,7 @@ from rich.progress import track
from utils import settings 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, comment_image_maker
from utils.playwright import clear_cookie_by_name from utils.playwright import clear_cookie_by_name
from utils.videos import save_data 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 context.add_cookies(cookies) # load preference cookies
# Login to Reddit # Login to Reddit
print_substep("Logging in to Reddit...") print_substep("Opening Reddit... ")
page = context.new_page() 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 # Get the thread screenshot
page.goto(reddit_object["thread_url"].replace("//r", "/r"), timeout=0) page.goto(reddit_object["thread_url"].replace("//r", "/r"), timeout=0)
page.set_viewport_size(ViewportSize(width=W, height=H)) 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( page.locator('[data-post-click-location="text-body"]').first.screenshot(
path=f"assets/temp/{reddit_id}/png/story_content.png" 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 # close browser instance when we are done using it
browser.close() browser.close()

Loading…
Cancel
Save