moved playwright to async api

pull/958/head
Drugsosos 3 years ago
parent 7375364003
commit 66af78c875
No known key found for this signature in database
GPG Key ID: 8E35176FE617E28D

@ -91,7 +91,7 @@ class TTSEngine:
if self.__total_length + clip_length <= self.max_length:
self.__total_length += clip_length
print(clip_length, '/', self.__total_length)
# print(clip_length, '/', self.__total_length) # TODO remove debug stuff
return True
return False

@ -28,6 +28,7 @@ from video_creation.background import download_background, chop_background_video
console = Console()
W, H = 1080, 1920 # TODO move to config
max_length: int = 50 # TODO move to config
def name_normalize(
@ -105,7 +106,7 @@ def make_final_video(
audio_composite = concatenate_audioclips(audio_clips)
console.log(f'[bold green] Video Will Be: {audio_composite.end} Seconds Long')
console.log('[bold green] Video Will Be: %.2f Seconds Long' % audio_composite.end)
# Gather all images
new_opacity = 1 if opacity is None or float(opacity) >= 1 else float(opacity) # TODO move to pydentic and percents

@ -3,11 +3,8 @@ import json
from pathlib import Path
from typing import Dict
from utils import settings
from playwright.async_api import async_playwright # pylint: disable=unused-import
from playwright.async_api import async_playwright, ViewportSize # pylint: disable=unused-import
# do not remove the above line
from playwright.sync_api import sync_playwright, ViewportSize
from rich.progress import track
import translators as ts
@ -16,7 +13,7 @@ from utils.console import print_step, print_substep
storymode = False
def download_screenshots_of_reddit_posts(reddit_object: dict, voiced_idx: list):
async def download_screenshots_of_reddit_posts(reddit_object: dict, voiced_idx: list):
"""Downloads screenshots of reddit posts as seen on the web. Downloads to assets/temp/png
Args:
@ -28,28 +25,28 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, voiced_idx: list):
# ! Make sure the reddit screenshots folder exists
Path("assets/temp/png").mkdir(parents=True, exist_ok=True)
with sync_playwright() as p:
async with async_playwright() as p:
print_substep("Launching Headless Browser...")
browser = p.chromium.launch()
context = browser.new_context()
browser = await p.chromium.launch()
context = await browser.new_context()
if settings.config["settings"]["theme"] == "dark":
cookie_file = open("./video_creation/data/cookie-dark-mode.json", encoding="utf-8")
else:
cookie_file = open("./video_creation/data/cookie-light-mode.json", encoding="utf-8")
cookies = json.load(cookie_file)
context.add_cookies(cookies) # load preference cookies
await context.add_cookies(cookies) # load preference cookies
# Get the thread screenshot
page = context.new_page()
page.goto(reddit_object["thread_url"], timeout=0)
page.set_viewport_size(ViewportSize(width=1920, height=1080))
page = await context.new_page()
await page.goto(reddit_object["thread_url"], timeout=0)
await page.set_viewport_size(ViewportSize(width=1920, height=1080))
if page.locator('[data-testid="content-gate"]').is_visible():
# This means the post is NSFW and requires to click the proceed button.
print_substep("Post is NSFW. You are spicy...")
page.locator('[data-testid="content-gate"] button').click()
page.locator(
await page.locator('[data-testid="content-gate"] button').click()
await page.locator(
'[data-click-id="text"] button'
).click() # Remove "Click to see nsfw" Button in Screenshot
@ -62,17 +59,17 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, voiced_idx: list):
to_language=settings.config["reddit"]["thread"]["post_lang"],
)
page.evaluate(
await page.evaluate(
"tl_content => document.querySelector('[data-test-id=\"post-content\"] > div:nth-child(3) > div > div').textContent = tl_content",
texts_in_tl,
)
else:
print_substep("Skipping translation...")
page.locator('[data-test-id="post-content"]').screenshot(path="assets/temp/png/title.png")
await page.locator('[data-test-id="post-content"]').screenshot(path="assets/temp/png/title.png")
if storymode:
page.locator('[data-click-id="text"]').screenshot(
await page.locator('[data-click-id="text"]').screenshot(
path="assets/temp/png/story_content.png"
)
else:
@ -82,10 +79,10 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, voiced_idx: list):
):
comment = reddit_object["comments"][idx]
if page.locator('[data-testid="content-gate"]').is_visible():
page.locator('[data-testid="content-gate"] button').click()
if await page.locator('[data-testid="content-gate"]').is_visible():
await page.locator('[data-testid="content-gate"] button').click()
page.goto(f'https://reddit.com{comment["comment_url"]}', timeout=0)
await page.goto(f'https://reddit.com{comment["comment_url"]}', timeout=0)
# translate code
@ -94,12 +91,12 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, voiced_idx: list):
comment["comment_body"],
to_language=settings.config["reddit"]["thread"]["post_lang"],
)
page.evaluate(
await 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"]],
)
page.locator(f"#t1_{comment['comment_id']}").screenshot(
await page.locator(f"#t1_{comment['comment_id']}").screenshot(
path=f"assets/temp/png/comment_{idx}.png"
)

Loading…
Cancel
Save