From 1f8ebf5eeb3e5bed07c6a0727bf67c5bca050e76 Mon Sep 17 00:00:00 2001 From: electro199 Date: Thu, 2 Feb 2023 15:56:59 +0500 Subject: [PATCH 1/4] Fixed Timeout error and improved return hinting --- TTS/TikTok.py | 7 ++++++- utils/imagenarator.py | 4 ++-- utils/voice.py | 4 ++-- video_creation/screenshot_downloader.py | 15 +++++++++++++-- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/TTS/TikTok.py b/TTS/TikTok.py index b370455..ed21a78 100644 --- a/TTS/TikTok.py +++ b/TTS/TikTok.py @@ -79,6 +79,8 @@ vocals: Final[tuple] = ( class TikTok: """TikTok Text-to-Speech Wrapper""" def __init__(self): + if not settings.config['settings']['tts']['tiktok_sessionid']: + raise TikTokTTSException(5) headers = { "User-Agent": "com.zhiliaoapp.musically/2022600030 (Linux; U; Android 7.1.2; es_ES; SM-G988N; " "Build/NRD90M;tt-ok/3.12.13.1)", @@ -140,7 +142,7 @@ class TikTok: return response.json() @staticmethod - def random_voice(): + def random_voice() -> str: return random.choice(eng_voices) @@ -158,5 +160,8 @@ class TikTokTTSException(Exception): if self._code == 4: return f"Code: {self._code}, reason: the speaker doesn't exist, message: {self._message}" + + if self._code == 5: + return f"Would you mind add session id in config ??" return f"Code: {self._message}, reason: unknown, message: {self._message}" diff --git a/utils/imagenarator.py b/utils/imagenarator.py index 8c6dc58..8e3789e 100644 --- a/utils/imagenarator.py +++ b/utils/imagenarator.py @@ -6,7 +6,7 @@ from PIL import Image, ImageDraw, ImageFont from rich.progress import track from TTS.engine_wrapper import process_text -def draw_multiple_line_text(image, text, font, text_color, padding, wrap=50): +def draw_multiple_line_text(image, text, font, text_color, padding, wrap=50) -> None: """ Draw multiline text over given image """ @@ -24,7 +24,7 @@ def draw_multiple_line_text(image, text, font, text_color, padding, wrap=50): # theme=bgcolor,reddit_obj=reddit_object,txtclr=txtcolor -def imagemaker(theme, reddit_obj: dict, txtclr, padding=5): +def imagemaker(theme, reddit_obj: dict, txtclr, padding=5) -> None: """ Render Images for video """ diff --git a/utils/voice.py b/utils/voice.py index a88c87d..76efc20 100644 --- a/utils/voice.py +++ b/utils/voice.py @@ -13,7 +13,7 @@ if sys.version_info[0] >= 3: from datetime import timezone -def check_ratelimit(response: Response): +def check_ratelimit(response: Response) -> bool: """ Checks if the response is a ratelimit response. If it is, it sleeps for the time specified in the response. @@ -30,7 +30,7 @@ def check_ratelimit(response: Response): return True -def sleep_until(time): +def sleep_until(time) -> None: """ Pause your program until a specific end time. 'time' is either a valid datetime object or unix timestamp in seconds (i.e. seconds since Unix epoch) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 3a76b5b..8e537d1 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -11,6 +11,7 @@ from rich.progress import track from utils import settings from utils.console import print_step, print_substep from utils.imagenarator import imagemaker +from utils.videos import save_data __all__ = ["download_screenshots_of_reddit_posts"] @@ -105,8 +106,18 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): print_substep("Skipping translation...") postcontentpath = f"assets/temp/{reddit_id}/png/title.png" - page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath) - + try : + page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath) + + except TimeoutError as e: + print_step("unable to locate post It is possibly Due to a NSFW post or unstable internet") + resp = input("Do you want to skip the post?(y/n)") + if resp.startswith("y"): + save_data("","","skiped",reddit_id,"") + print("Now you can re run the program this post will skipped") + exit() + raise e + if storymode: page.locator('[data-click-id="text"]').first.screenshot( path=f"assets/temp/{reddit_id}/png/story_content.png" From e6e27f32407a8d8dcb3b05034a91f5b6dc5c4f6b Mon Sep 17 00:00:00 2001 From: electro199 Date: Thu, 2 Feb 2023 16:01:55 +0500 Subject: [PATCH 2/4] changed TikTokTTSException msg --- TTS/TikTok.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TTS/TikTok.py b/TTS/TikTok.py index ed21a78..2bcbd6d 100644 --- a/TTS/TikTok.py +++ b/TTS/TikTok.py @@ -162,6 +162,6 @@ class TikTokTTSException(Exception): return f"Code: {self._code}, reason: the speaker doesn't exist, message: {self._message}" if self._code == 5: - return f"Would you mind add session id in config ??" + return f"You have to add session id in config to use titok TTS" return f"Code: {self._message}, reason: unknown, message: {self._message}" From c0916c13bb93612d2752737ded9eebad55d9acb7 Mon Sep 17 00:00:00 2001 From: electro199 Date: Thu, 2 Feb 2023 17:03:51 +0500 Subject: [PATCH 3/4] Add nsfw flag & improved error handling --- reddit/subreddit.py | 1 + video_creation/screenshot_downloader.py | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 8fb9e9d..ed1e8cf 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -134,6 +134,7 @@ def get_subreddit_threads(POST_ID: str): content["thread_url"] = threadurl content["thread_title"] = submission.title content["thread_id"] = submission.id + content["is_nsfw"] = submission.over_18 content["comments"] = [] if settings.config["settings"]["storymode"]: if settings.config["settings"]["storymodemethod"] == 1: diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 8e537d1..f7fe7db 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -110,13 +110,18 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath) except TimeoutError as e: - print_step("unable to locate post It is possibly Due to a NSFW post or unstable internet") - resp = input("Do you want to skip the post?(y/n)") - if resp.startswith("y"): - save_data("","","skiped",reddit_id,"") - print("Now you can re run the program this post will skipped") - exit() - raise e + if settings.config["is_nsfw"] : + print_step("Unable to get post It is due to a NSFW post") + resp = input("Do you want to skip the post?(y/n)") + if resp.casefold().startswith("y"): + save_data("","","skiped",reddit_id,"") + print("Now you can re run the program this post will skipped") + exit() + else: + raise e + + + if storymode: page.locator('[data-click-id="text"]').first.screenshot( From 0a383ff93b84a2e999871e703a8c4b3aeecdef93 Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 2 Feb 2023 15:38:38 +0100 Subject: [PATCH 4/4] Improved error-handeling --- video_creation/screenshot_downloader.py | 38 ++++++++++++------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index f7fe7db..2be93b4 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -4,7 +4,6 @@ from pathlib import Path from typing import Dict, Final import translators as ts -from playwright.async_api import async_playwright # pylint: disable=unused-import from playwright.sync_api import ViewportSize, sync_playwright from rich.progress import track @@ -13,9 +12,9 @@ from utils.console import print_step, print_substep from utils.imagenarator import imagemaker from utils.videos import save_data - __all__ = ["download_screenshots_of_reddit_posts"] + def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): """Downloads screenshots of reddit posts as seen on the web. Downloads to assets/temp/png @@ -106,23 +105,24 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): print_substep("Skipping translation...") postcontentpath = f"assets/temp/{reddit_id}/png/title.png" - try : + try: page.locator('[data-test-id="post-content"]').screenshot(path=postcontentpath) - - except TimeoutError as e: - if settings.config["is_nsfw"] : - print_step("Unable to get post It is due to a NSFW post") - resp = input("Do you want to skip the post?(y/n)") - if resp.casefold().startswith("y"): - save_data("","","skiped",reddit_id,"") - print("Now you can re run the program this post will skipped") - exit() + except Exception as e: + OKGREEN = '\033[92m' + WARNING = '\033[93m' + ENDC = '\033[0m' + print_step(f"{WARNING}Something went wrong!{ENDC}") + resp = input("Something went wrong with making the screenshots! Do you want to skip the post? (y/n) ") + if resp.casefold().startswith("y"): + save_data("", "", "skipped", reddit_id, "") + print(f"{OKGREEN}The post is successfully skipped! You can now restart the program and this post will skipped.{ENDC}") + resp = input("Do you want the error traceback for debugging purposes? (y/n)") + if resp.casefold().startswith("y"): + print(e) + exit() else: - raise e - - - - + exit() + if storymode: page.locator('[data-click-id="text"]').first.screenshot( path=f"assets/temp/{reddit_id}/png/story_content.png" @@ -167,6 +167,4 @@ def get_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: int): # close browser instance when we are done using it browser.close() - - - print_substep("Screenshots downloaded Successfully.", style="bold green") \ No newline at end of file + print_substep("Screenshots downloaded Successfully.", style="bold green")