diff --git a/reddit/subreddit.py b/reddit/subreddit.py index daeb439..d6216a3 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -1,4 +1,5 @@ import re +import sys import praw from praw.models import MoreComments @@ -38,14 +39,16 @@ def get_subreddit_threads(POST_ID: str): client_secret=settings.config["reddit"]["creds"]["client_secret"], user_agent="Accessing Reddit threads", username=username, - passkey=passkey, + password=passkey, check_for_async=False, ) except ResponseException as e: if e.response.status_code == 401: print("Invalid credentials - please check them in config.toml") - except: - print("Something went wrong...") + sys.exit(1) + except Exception as e: + print(f"Something went wrong logging into Reddit: {e}") + sys.exit(1) # Ask user for subreddit input print_step("Getting subreddit threads...") diff --git a/video_creation/final_video.py b/video_creation/final_video.py index c4f3a0b..a73931e 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -1,6 +1,8 @@ import multiprocessing import os import re +import shutil +import subprocess import tempfile import textwrap import threading @@ -10,6 +12,21 @@ from pathlib import Path from typing import Dict, Final, Tuple import ffmpeg + + +def _get_video_codec() -> str: + try: + result = subprocess.run( + ["ffmpeg", "-encoders"], capture_output=True, text=True, timeout=10 + ) + if "h264_nvenc" in result.stdout: + return "h264_nvenc" + except (subprocess.TimeoutExpired, FileNotFoundError): + pass + return "libx264" + + +VIDEO_CODEC = _get_video_codec() import translators from PIL import Image, ImageDraw, ImageFont from rich.console import Console @@ -93,7 +110,7 @@ def prepare_background(reddit_id: str, W: int, H: int) -> str: output_path, an=None, **{ - "c:v": "h264_nvenc", + "c:v": VIDEO_CODEC, "b:v": "20M", "b:a": "192k", "threads": multiprocessing.cpu_count(), @@ -438,7 +455,7 @@ def make_final_video( path, f="mp4", **{ - "c:v": "h264_nvenc", + "c:v": VIDEO_CODEC, "b:v": "20M", "b:a": "192k", "threads": multiprocessing.cpu_count(), @@ -468,7 +485,7 @@ def make_final_video( path, f="mp4", **{ - "c:v": "h264_nvenc", + "c:v": VIDEO_CODEC, "b:v": "20M", "b:a": "192k", "threads": multiprocessing.cpu_count(), diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 50f8a99..9164e96 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -145,16 +145,11 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): page.wait_for_load_state() page.wait_for_timeout(5000) - if page.locator( - "#t3_12hmbug > div > div._3xX726aBn29LDbsDtzr_6E._1Ap4F5maDtT1E1YuCiaO0r.D3IL3FD0RFy_mkKLPwL4 > div > div > button" - ).is_visible(): - # This means the post is NSFW and requires to click the proceed button. - + nsfw_button = page.locator("button:has-text('Yes'), button:has-text('Continue'), [data-testid='content-gate'] button").first + if nsfw_button.is_visible(): print_substep("Post is NSFW. You are spicy...") - page.locator( - "#t3_12hmbug > div > div._3xX726aBn29LDbsDtzr_6E._1Ap4F5maDtT1E1YuCiaO0r.D3IL3FD0RFy_mkKLPwL4 > div > div > button" - ).click() - page.wait_for_load_state() # Wait for page to fully load + nsfw_button.click() + page.wait_for_load_state() # translate code if page.locator(