downloader mods

pull/564/head
SinecKers 2 years ago
parent a37096f874
commit c50931ab6e

@ -1,5 +1,6 @@
import json import json
from os import getenv from os import getenv
import os
from pathlib import Path from pathlib import Path
from playwright.async_api import async_playwright from playwright.async_api import async_playwright
@ -10,6 +11,10 @@ from utils.console import print_step, print_substep
import json import json
from rich.console import Console from rich.console import Console
import translators as ts
from PIL import Image, ImageDraw, ImageFont
import textwrap
console = Console() console = Console()
storymode = False storymode = False
@ -40,7 +45,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num):
context.add_cookies(cookies) # load preference cookies context.add_cookies(cookies) # load preference cookies
# 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"])
page.set_viewport_size(ViewportSize(width=1920, height=1080)) page.set_viewport_size(ViewportSize(width=1920, height=1080))
if page.locator('[data-testid="content-gate"]').is_visible(): if page.locator('[data-testid="content-gate"]').is_visible():
# This means the post is NSFW and requires to click the proceed button. # This means the post is NSFW and requires to click the proceed button.
@ -51,7 +56,33 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num):
'[data-click-id="text"] button' '[data-click-id="text"] button'
).click() # Remove "Click to see nsfw" Button in Screenshot ).click() # Remove "Click to see nsfw" Button in Screenshot
page.locator('[data-test-id="post-content"]').screenshot(path="assets/temp/png/title.png") page.locator('[data-test-id="post-content"]').screenshot(
path="assets/temp/png/title.png"
)
# translate code
if getenv("POSTLANG"):
print_substep("Translating post...")
texts_in_tl = ts.google(reddit_object["thread_title"], to_language=os.getenv("POSTLANG"))
img = Image.open("assets/temp/png/title.png")
width = img.size[0]
height = img.size[1]
d1 = ImageDraw.Draw(img)
font = ImageFont.truetype("arial.ttf", 22)
wrapper = textwrap.TextWrapper(width=60)
wrapped_str = wrapper.fill(text=texts_in_tl)
d1.rectangle((7, 25, width - 20, height - 35), fill='white')
d1.text((10, 30), f"{wrapped_str}", font=font, fill=(0, 0, 0))
img.save("assets/temp/png/title.png")
else:
print_substep("Skipping translation...")
if storymode: if storymode:
page.locator('[data-click-id="text"]').screenshot( page.locator('[data-click-id="text"]').screenshot(
path="assets/temp/png/story_content.png" path="assets/temp/png/story_content.png"
@ -60,7 +91,6 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num):
for idx, comment in track( for idx, comment in track(
enumerate(reddit_object["comments"]), "Downloading screenshots..." enumerate(reddit_object["comments"]), "Downloading screenshots..."
): ):
# 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
@ -68,8 +98,25 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num):
if page.locator('[data-testid="content-gate"]').is_visible(): if page.locator('[data-testid="content-gate"]').is_visible():
page.locator('[data-testid="content-gate"] button').click() page.locator('[data-testid="content-gate"] button').click()
page.goto(f'https://reddit.com{comment["comment_url"]}', timeout=0) page.goto(f'https://reddit.com{comment["comment_url"]}')
page.locator(f"#t1_{comment['comment_id']}").screenshot( page.locator(f"#t1_{comment['comment_id']}").screenshot(
path=f"assets/temp/png/comment_{idx}.png" path=f"assets/temp/png/comment_{idx}.png"
) )
if getenv("POSTLANG"):
img_comment = Image.open(f"assets/temp/png/comment_{idx}.png")
width2 = img_comment.size[0]
height2 = img_comment.size[1]
comment_tl = ts.google(comment["comment_body"], to_language=os.getenv("POSTLANG"))
wrapper1 = textwrap.TextWrapper(width=78)
wrapped_str1 = wrapper1.fill(text=comment_tl)
d2 = ImageDraw.Draw(img_comment)
font_comment = ImageFont.truetype("arial.ttf", 16)
d2.rectangle((30, 40, width2 - 5, height2 - 35), fill='#F5F6F6')
d2.text((40, 50), f"{wrapped_str1}", font=font_comment, fill=(0, 0, 0))
img_comment.save(f"assets/temp/png/comment_{idx}.png")
print_substep("Screenshots downloaded Successfully.", style="bold green") print_substep("Screenshots downloaded Successfully.", style="bold green")

@ -10,11 +10,13 @@ from rich.progress import track
from TTS.swapper import TTS from TTS.swapper import TTS
console = Console()
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
console = Console() import translators as ts
import os
VIDEO_LENGTH: int = 40 # secs VIDEO_LENGTH: int = 40 # secs
@ -29,32 +31,44 @@ def save_text_to_mp3(reddit_obj):
# Create a folder for the mp3 files. # Create a folder for the mp3 files.
Path("assets/temp/mp3").mkdir(parents=True, exist_ok=True) Path("assets/temp/mp3").mkdir(parents=True, exist_ok=True)
if os.getenv("POSTLANG"):
print_substep("Translating Texts...")
tl_title = ts.google(reddit_obj["thread_title"], to_language=os.getenv("POSTLANG"))
else:
print_substep("Skipping Translation...")
tl_title = reddit_obj["thread_title"]
TextToSpeech = TTS() TextToSpeech = TTS()
TextToSpeech.tts( TextToSpeech.tts(
sanitize_text(reddit_obj["thread_title"]), sanitize_text(tl_title),
filename="assets/temp/mp3/title.mp3", filename=f"assets/temp/mp3/title.mp3",
random_speaker=False, random_speaker=False,
) )
try: try:
length += MP3("assets/temp/mp3/title.mp3").info.length length += MP3(f"assets/temp/mp3/title.mp3").info.length
except HeaderNotFoundError: # note to self AudioFileClip except HeaderNotFoundError: # note to self AudioFileClip
length += sox.file_info.duration("assets/temp/mp3/title.mp3") length += sox.file_info.duration(f"assets/temp/mp3/title.mp3")
if getenv("STORYMODE").casefold() == "true": if getenv("STORYMODE").casefold() == "true":
TextToSpeech.tts( TextToSpeech.tts(
sanitize_text(reddit_obj["thread_content"]), sanitize_text(reddit_obj["thread_content"]),
filename="assets/temp/mp3/story_content.mp3", filename=f"assets/temp/mp3/story_content.mp3",
random_speaker=False, random_speaker=False,
) )
# 'story_content' # 'story_content'
com = 0 com = 0
for comment in track((reddit_obj["comments"]), "Saving..."): for comment in track((reddit_obj["comments"]), "Saving..."):
# ! Stop creating mp3 files if the length is greater than VIDEO_LENGTH seconds. This can be longer # ! Stop creating mp3 files if the length is greater than VIDEO_LENGTH seconds. This can be longer, but this is just a good_voices starting point
# but this is just a good_voices starting point
if length > VIDEO_LENGTH: if length > VIDEO_LENGTH:
break break
if os.getenv("POSTLANG"):
tl_comment = ts.google(comment["comment_body"], to_language=os.getenv("POSTLANG"))
else:
tl_comment = comment["comment_body"]
TextToSpeech.tts( TextToSpeech.tts(
sanitize_text(comment["comment_body"]), sanitize_text(tl_comment),
filename=f"assets/temp/mp3/{com}.mp3", filename=f"assets/temp/mp3/{com}.mp3",
random_speaker=False, random_speaker=False,
) )

Loading…
Cancel
Save