include spinners or loading bar from console that people would know if the program is stuck or not

this would help issues like #358, that they would not be waiting for nothing
pull/280/head
iaacornus 3 years ago
parent 81bb39fecc
commit cce278ab83
No known key found for this signature in database
GPG Key ID: 281739AE7252598C

@ -3,6 +3,7 @@ from pathlib import Path
from playwright.sync_api import sync_playwright, ViewportSize from playwright.sync_api import sync_playwright, ViewportSize
from rich.progress import track from rich.progress import track
from rich.console import Console
from utils.console import print_step, print_substep from utils.console import print_step, print_substep
@ -14,46 +15,48 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme):
reddit_object: The Reddit Object you received in askreddit.py reddit_object: The Reddit Object you received in askreddit.py
screenshot_num: The number of screenshots you want to download. screenshot_num: The number of screenshots you want to download.
""" """
console = Console()
print_step("Downloading Screenshots of Reddit Posts 📷") print_step("Downloading Screenshots of Reddit Posts 📷")
# ! Make sure the reddit screenshots folder exists # ! Make sure the reddit screenshots folder exists
Path("assets/png").mkdir(parents=True, exist_ok=True) Path("assets/png").mkdir(parents=True, exist_ok=True)
print_substep("Launching Headless Browser...") with console.status("[bold]Launching Headless Browser ...", spinner="simpleDots"):
with sync_playwright() as browser_: with sync_playwright() as browser_:
browser = browser_.chromium.launch() browser = browser_.chromium.launch()
context = browser.new_context() context = browser.new_context()
if theme.casefold() == "dark": if theme.casefold() == "dark":
with open("video_creation/cookies.json", encoding="utf-8") as cookie_file: with open("video_creation/cookies.json", encoding="utf-8") as cookie_file:
cookies = json.load(cookie_file) cookies = json.load(cookie_file)
context.add_cookies(cookies) context.add_cookies(cookies)
# Get the thread screenshot # Get the thread screenshot
page = context.new_page() page = context.new_page()
page.goto(reddit_object["thread_url"]) 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.
print_substep("Post is NSFW. You are spicy...") print_substep("Post is NSFW. You are spicy...")
page.locator('[data-testid="content-gate"] button').click() page.locator('[data-testid="content-gate"] button').click()
page.locator('[data-test-id="post-content"]').screenshot(path="assets/png/title.png") page.locator('[data-test-id="post-content"]').screenshot(path="assets/png/title.png")
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
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"]}') page.goto(f'https://reddit.com{comment["comment_url"]}')
page.locator( page.locator(
f"#t1_{comment['comment_id']}" f"#t1_{comment['comment_id']}"
).screenshot(path=f"assets/png/comment_{idx}.png") ).screenshot(path=f"assets/png/comment_{idx}.png")
print_substep("Screenshots downloaded Successfully.", style="bold green") print_substep("Screenshots downloaded Successfully.", style="bold green")

Loading…
Cancel
Save