From 1f6ea04b46c35c4d9452cda97c4fc784904149ce Mon Sep 17 00:00:00 2001 From: Drugsosos <44712637+Drugsosos@users.noreply.github.com> Date: Tue, 12 Jul 2022 01:35:43 +0300 Subject: [PATCH] fixed NSFW button & improved logger --- video_creation/data/videos.json | 11 +--- video_creation/final_video.py | 13 +++-- video_creation/screenshot_downloader.py | 77 +++++++++++++++---------- 3 files changed, 55 insertions(+), 46 deletions(-) diff --git a/video_creation/data/videos.json b/video_creation/data/videos.json index 5c92929..0637a08 100644 --- a/video_creation/data/videos.json +++ b/video_creation/data/videos.json @@ -1,10 +1 @@ -[ - { - "subreddit": "AskReddit", - "id": "vwgslz", - "time": "1657573375", - "background_credit": "bbswitzer", - "reddit_title": "Which singer should never have been famous", - "filename": "Which singer should never have been famous.mp4" - } -] \ No newline at end of file +[] \ No newline at end of file diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 46513b8..6da3034 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -92,12 +92,12 @@ def make_final_video( audio_clips.append(audio_title) indexes_for_videos = list() - for idx, audio in track( - enumerate(indexes_of_clips), + for idx in track( + indexes_of_clips, description='Gathering audio clips...', ): temp_audio_clip = create_audio_clip( - audio, + idx, video_duration, ) if video_duration + temp_audio_clip.duration <= max_length: @@ -107,7 +107,7 @@ def make_final_video( audio_composite = concatenate_audioclips(audio_clips) - console.log(f'[bold green] Video Will Be: {video_duration} Seconds Long') + console.log('[bold green] Video Will Be: %.2f Seconds Long' % video_duration) # Gather all images new_opacity = 1 if opacity is None or float(opacity) >= 1 else float(opacity) # TODO move to pydentic and percents @@ -139,10 +139,11 @@ def make_final_video( ) ) - for photo_idx in indexes_for_videos: + for photo_idx in range(indexes_for_videos.__len__()): image_clips.append( create_image_clip( - f'comment_{photo_idx}', + f'comment_{indexes_for_videos[photo_idx]}', + # + title clip audio_clips[photo_idx + 1].start, audio_clips[photo_idx + 1].end, audio_clips[photo_idx + 1].duration diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 63be15f..3cc4dd4 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -27,12 +27,12 @@ class ExceptionDecorator: """ Factory for decorating functions """ - __exception: Optional[_exceptions] = attrib(default=None) + exception: Optional[_exceptions] = attrib(default=None) __default_exception: _exceptions = attrib(default=BrowserTimeoutError) def __attrs_post_init__(self): - if not self.__exception: - self.__exception = self.__default_exception + if not self.exception: + self.exception = self.__default_exception def __call__( self, @@ -45,13 +45,14 @@ class ExceptionDecorator: except Exception as caughtException: import logging - if isinstance(self.__exception, type): - if not type(caughtException) == self.__exception: - logging.basicConfig(filename='.webdriver.log', filemode='w', encoding='utf-8', - level=logging.DEBUG) + logging.basicConfig(filename='.webdriver.log', filemode='a+', + encoding='utf-8', level=logging.ERROR) + + if isinstance(self.exception, type): + if not type(caughtException) == self.exception: logging.error(f'unexpected error - {caughtException}') else: - if not type(caughtException) in self.__exception: + if not type(caughtException) in self.exception: logging.error(f'unexpected error - {caughtException}') return wrapper @@ -251,6 +252,32 @@ class RedditScreenshot(Browser, Wait): {'timeout': 5000}, ) + async def __close_nsfw( + self, + page_instance: PageCls + ) -> None: + from asyncio import ensure_future + + print_substep('Post is NSFW. You are spicy...') + # To await indirectly reload + navigation = ensure_future(page_instance.waitForNavigation()) + + # Triggers indirectly reload + await self.click( + page_instance, + '//button[text()="Yes"]', + {'timeout': 5000}, + ) + + # Await reload + await navigation + + await (await self.find_xpath( + page_instance, + '//button[text()="Click to see nsfw"]', + {'timeout': 5000}, + )).click() + async def __collect_comment( self, comment_obj: dict, @@ -289,14 +316,14 @@ class RedditScreenshot(Browser, Wait): """ Downloads screenshots of reddit posts as seen on the web. Downloads to assets/temp/png """ - await self.get_browser() print_step('Downloading screenshots of reddit posts...') + print_substep('Launching Headless Browser...') + await self.get_browser() + # ! Make sure the reddit screenshots folder exists Path('assets/temp/png').mkdir(parents=True, exist_ok=True) - print_substep('Launching Headless Browser...') - # Get the thread screenshot reddit_main = await self.browser.newPage() await reddit_main.goto(self.reddit_object['thread_url']) @@ -306,19 +333,7 @@ class RedditScreenshot(Browser, Wait): if self.reddit_object['is_nsfw']: # This means the post is NSFW and requires to click the proceed button. - - print_substep('Post is NSFW. You are spicy...') - await self.click( - reddit_main, - '//button[contains(text(), \'Yes\')]', - {'timeout': 5000}, - ) - - await self.click( - reddit_main, - '//button[contains(text(), \'nsfw\')]', - {'timeout': 5000}, - ) + await self.__close_nsfw(reddit_main) # Translates submission title if settings.config['reddit']['thread']['post_lang']: @@ -336,17 +351,19 @@ class RedditScreenshot(Browser, Wait): else: print_substep("Skipping translation...") - await self.screenshot( - reddit_main, - f'//*[contains(@id, \'t3_{self.reddit_object["thread_id"]}\')]', - {'path': f'assets/temp/png/title.png'}, - ) - async_tasks_primary = [ self.__collect_comment(self.reddit_object['comments'][idx], idx) for idx in self.screenshot_idx ] + async_tasks_primary.append( + self.screenshot( + reddit_main, + f'//*[contains(@id, \'t3_{self.reddit_object["thread_id"]}\')]', + {'path': f'assets/temp/png/title.png'}, + ) + ) + def chunks(lst, n): """Yield successive n-sized chunks from lst.""" for i in range(0, len(lst), n):