From a37096f874907b61d782381eeb42a7cb6532c405 Mon Sep 17 00:00:00 2001 From: SinecKers Date: Sun, 19 Jun 2022 04:50:45 +0300 Subject: [PATCH 01/10] test --- ' | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ' diff --git a/' b/' new file mode 100644 index 0000000..e69de29 From c50931ab6ed3e064438494b247075118b970ddf2 Mon Sep 17 00:00:00 2001 From: SinecKers Date: Sun, 19 Jun 2022 04:57:07 +0300 Subject: [PATCH 02/10] downloader mods --- video_creation/screenshot_downloader.py | 59 ++++++++++++++++++++++--- video_creation/voices.py | 36 ++++++++++----- 2 files changed, 78 insertions(+), 17 deletions(-) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 6147dff..63f1f95 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -1,5 +1,6 @@ import json from os import getenv +import os from pathlib import Path from playwright.async_api import async_playwright @@ -10,6 +11,10 @@ from utils.console import print_step, print_substep import json from rich.console import Console +import translators as ts +from PIL import Image, ImageDraw, ImageFont +import textwrap + console = Console() storymode = False @@ -18,8 +23,8 @@ storymode = False def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): """Downloads screenshots of reddit posts as they are seen on the web. Args: - reddit_object: The Reddit Object you received in askreddit.py - screenshot_num: The number of screenshots you want to download. + reddit_object: The Reddit Object you received in askreddit.py + screenshot_num: The number of screenshots you want to download. """ print_step("Downloading screenshots of reddit posts...") @@ -40,7 +45,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): context.add_cookies(cookies) # load preference cookies # Get the thread screenshot page = context.new_page() - page.goto(reddit_object["thread_url"], timeout=0) + page.goto(reddit_object["thread_url"]) 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. @@ -51,7 +56,33 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): '[data-click-id="text"] button' ).click() # Remove "Click to see nsfw" Button in Screenshot - page.locator('[data-test-id="post-content"]').screenshot(path="assets/temp/png/title.png") + page.locator('[data-test-id="post-content"]').screenshot( + path="assets/temp/png/title.png" + ) + + # translate code + + if getenv("POSTLANG"): + print_substep("Translating post...") + texts_in_tl = ts.google(reddit_object["thread_title"], to_language=os.getenv("POSTLANG")) + + img = Image.open("assets/temp/png/title.png") + + width = img.size[0] + height = img.size[1] + + d1 = ImageDraw.Draw(img) + font = ImageFont.truetype("arial.ttf", 22) + + wrapper = textwrap.TextWrapper(width=60) + wrapped_str = wrapper.fill(text=texts_in_tl) + + d1.rectangle((7, 25, width - 20, height - 35), fill='white') + d1.text((10, 30), f"{wrapped_str}", font=font, fill=(0, 0, 0)) + img.save("assets/temp/png/title.png") + else: + print_substep("Skipping translation...") + if storymode: page.locator('[data-click-id="text"]').screenshot( path="assets/temp/png/story_content.png" @@ -60,7 +91,6 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): for idx, comment in track( enumerate(reddit_object["comments"]), "Downloading screenshots..." ): - # Stop if we have reached the screenshot_num if idx >= screenshot_num: break @@ -68,8 +98,25 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): if page.locator('[data-testid="content-gate"]').is_visible(): page.locator('[data-testid="content-gate"] button').click() - page.goto(f'https://reddit.com{comment["comment_url"]}', timeout=0) + page.goto(f'https://reddit.com{comment["comment_url"]}') page.locator(f"#t1_{comment['comment_id']}").screenshot( path=f"assets/temp/png/comment_{idx}.png" ) + + if getenv("POSTLANG"): + img_comment = Image.open(f"assets/temp/png/comment_{idx}.png") + width2 = img_comment.size[0] + height2 = img_comment.size[1] + + comment_tl = ts.google(comment["comment_body"], to_language=os.getenv("POSTLANG")) + + wrapper1 = textwrap.TextWrapper(width=78) + wrapped_str1 = wrapper1.fill(text=comment_tl) + + d2 = ImageDraw.Draw(img_comment) + font_comment = ImageFont.truetype("arial.ttf", 16) + + d2.rectangle((30, 40, width2 - 5, height2 - 35), fill='#F5F6F6') + d2.text((40, 50), f"{wrapped_str1}", font=font_comment, fill=(0, 0, 0)) + img_comment.save(f"assets/temp/png/comment_{idx}.png") print_substep("Screenshots downloaded Successfully.", style="bold green") diff --git a/video_creation/voices.py b/video_creation/voices.py index be7da96..b66d7f4 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -10,11 +10,13 @@ from rich.progress import track from TTS.swapper import TTS +console = Console() + from utils.console import print_step, print_substep from utils.voice import sanitize_text -console = Console() - +import translators as ts +import os VIDEO_LENGTH: int = 40 # secs @@ -22,39 +24,51 @@ VIDEO_LENGTH: int = 40 # secs def save_text_to_mp3(reddit_obj): """Saves Text to MP3 files. Args: - reddit_obj : The reddit object you received from the reddit API in the askreddit.py file. + reddit_obj : The reddit object you received from the reddit API in the askreddit.py file. """ print_step("Saving Text to MP3 files...") length = 0 # Create a folder for the mp3 files. Path("assets/temp/mp3").mkdir(parents=True, exist_ok=True) + + if os.getenv("POSTLANG"): + print_substep("Translating Texts...") + tl_title = ts.google(reddit_obj["thread_title"], to_language=os.getenv("POSTLANG")) + else: + print_substep("Skipping Translation...") + tl_title = reddit_obj["thread_title"] + TextToSpeech = TTS() TextToSpeech.tts( - sanitize_text(reddit_obj["thread_title"]), - filename="assets/temp/mp3/title.mp3", + sanitize_text(tl_title), + filename=f"assets/temp/mp3/title.mp3", random_speaker=False, ) try: - length += MP3("assets/temp/mp3/title.mp3").info.length + length += MP3(f"assets/temp/mp3/title.mp3").info.length except HeaderNotFoundError: # note to self AudioFileClip - length += sox.file_info.duration("assets/temp/mp3/title.mp3") + length += sox.file_info.duration(f"assets/temp/mp3/title.mp3") if getenv("STORYMODE").casefold() == "true": TextToSpeech.tts( sanitize_text(reddit_obj["thread_content"]), - filename="assets/temp/mp3/story_content.mp3", + filename=f"assets/temp/mp3/story_content.mp3", random_speaker=False, ) # 'story_content' com = 0 for comment in track((reddit_obj["comments"]), "Saving..."): - # ! Stop creating mp3 files if the length is greater than VIDEO_LENGTH seconds. This can be longer - # but this is just a good_voices starting point + # ! Stop creating mp3 files if the length is greater than VIDEO_LENGTH seconds. This can be longer, but this is just a good_voices starting point if length > VIDEO_LENGTH: break + if os.getenv("POSTLANG"): + tl_comment = ts.google(comment["comment_body"], to_language=os.getenv("POSTLANG")) + else: + tl_comment = comment["comment_body"] + TextToSpeech.tts( - sanitize_text(comment["comment_body"]), + sanitize_text(tl_comment), filename=f"assets/temp/mp3/{com}.mp3", random_speaker=False, ) From 4bb8d67e686fc9f9bee644e02c942f6db4610a8c Mon Sep 17 00:00:00 2001 From: SinecKers Date: Sun, 19 Jun 2022 05:00:59 +0300 Subject: [PATCH 03/10] tts language selection --- TTS/GTTS.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TTS/GTTS.py b/TTS/GTTS.py index fcbcb9b..0017bf4 100644 --- a/TTS/GTTS.py +++ b/TTS/GTTS.py @@ -1,5 +1,5 @@ from gtts import gTTS - +import os class GTTS: def tts( @@ -9,5 +9,5 @@ class GTTS: random_speaker=False, censor=False, ): - tts = gTTS(text=req_text, lang="en", slow=False) + tts = gTTS(text=req_text, lang=os.getenv("POSTLANG") or "en", slow=False) tts.save(f"{filename}") From 2828b6718d8955fac2ce4f8238cea61894aee51b Mon Sep 17 00:00:00 2001 From: SinecKers Date: Sun, 19 Jun 2022 05:08:40 +0300 Subject: [PATCH 04/10] .env.template change --- .env.template | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.env.template b/.env.template index bcd326d..0b2af14 100644 --- a/.env.template +++ b/.env.template @@ -1,3 +1,4 @@ +# This can be found in the email that reddit sent you when you created the app REDDIT_CLIENT_ID="" REDDIT_CLIENT_SECRET="" @@ -23,9 +24,14 @@ MAX_COMMENT_LENGTH="500" # default is 500 # Range is 0 -> 1 recommended around 0.8-0.9 OPACITY="1" -# see different voice options: todo: add docs -VOICE="Matthew" # e.g. en_us_002 -TTsChoice="polly" +# If you want to translate the comments to another language, set the language code here. +# If empty, no translation will be done. +POSTLANG="es" + +# see TTSwrapper.py for all valid options +VOICE="" # e.g. en_us_002 +TTsChoice="gtts" # todo add docs +# IMPORTANT NOTE: if you use translate, you need to set this gtts or set tiktok and use custom voice in your language # IN-PROGRESS - not yet implemented STORYMODE="False" From cf92e6f4c44a659b8d81e5f1e1fbc2c5e1c227b5 Mon Sep 17 00:00:00 2001 From: SinecKers Date: Sun, 19 Jun 2022 05:16:26 +0300 Subject: [PATCH 05/10] requirements update --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 490b0c1..20e2c77 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ pytube==12.1.0 requests==2.28.0 rich==12.4.4 sox==1.4.1 +translators==5.2.2 From 63e7dc04efeb31fa3ca6802208005d5510dfa403 Mon Sep 17 00:00:00 2001 From: SinecKers Date: Tue, 21 Jun 2022 00:07:09 +0300 Subject: [PATCH 06/10] default voice --- .env.template | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.env.template b/.env.template index 0b2af14..6a8a15e 100644 --- a/.env.template +++ b/.env.template @@ -26,11 +26,11 @@ OPACITY="1" # If you want to translate the comments to another language, set the language code here. # If empty, no translation will be done. -POSTLANG="es" +POSTLANG="" # see TTSwrapper.py for all valid options -VOICE="" # e.g. en_us_002 -TTsChoice="gtts" # todo add docs +VOICE="Matthew" # e.g. en_us_002 +TTsChoice="polly" # todo add docs # IMPORTANT NOTE: if you use translate, you need to set this gtts or set tiktok and use custom voice in your language # IN-PROGRESS - not yet implemented From fe97a006483da75eb73abe111d6d5e4162e38cfb Mon Sep 17 00:00:00 2001 From: SinecKers Date: Tue, 21 Jun 2022 00:18:08 +0300 Subject: [PATCH 07/10] forgot smt --- video_creation/screenshot_downloader.py | 26 +++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 63f1f95..1caa909 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -45,7 +45,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): context.add_cookies(cookies) # load preference cookies # Get the thread screenshot page = context.new_page() - page.goto(reddit_object["thread_url"]) + page.goto(reddit_object["thread_url"], timeout=0) 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. @@ -77,8 +77,15 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): wrapper = textwrap.TextWrapper(width=60) wrapped_str = wrapper.fill(text=texts_in_tl) - d1.rectangle((7, 25, width - 20, height - 35), fill='white') - d1.text((10, 30), f"{wrapped_str}", font=font, fill=(0, 0, 0)) + if (getenv("THEME").upper() == "DARK"): + fillmode = "#1a1a1b" + textmode = (255, 255, 255) + else: + fillmode = "whiite" + textmode = (0, 0, 0) + + d1.rectangle((7, 25, width - 20, height - 35), fill=fillmode) + d1.text((10, 30), f"{wrapped_str}", font=font, fill=textmode) img.save("assets/temp/png/title.png") else: print_substep("Skipping translation...") @@ -98,7 +105,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): if page.locator('[data-testid="content-gate"]').is_visible(): 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"]}', timeout=0) page.locator(f"#t1_{comment['comment_id']}").screenshot( path=f"assets/temp/png/comment_{idx}.png" ) @@ -116,7 +123,14 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): d2 = ImageDraw.Draw(img_comment) font_comment = ImageFont.truetype("arial.ttf", 16) - d2.rectangle((30, 40, width2 - 5, height2 - 35), fill='#F5F6F6') - d2.text((40, 50), f"{wrapped_str1}", font=font_comment, fill=(0, 0, 0)) + if (getenv("THEME").upper() == "DARK"): + fillmode1 = "#242426" + textmode1 = (255, 255, 255) + else: + fillmode1 = "#F5F6F6" + textmode1 = (0, 0, 0) + + d2.rectangle((30, 40, width2 - 5, height2 - 35), fill=fillmode1) + d2.text((40, 50), f"{wrapped_str1}", font=font_comment, fill=textmode1) img_comment.save(f"assets/temp/png/comment_{idx}.png") print_substep("Screenshots downloaded Successfully.", style="bold green") From 138522b18702a5dab0931ec653e6eee32bb959e8 Mon Sep 17 00:00:00 2001 From: SinecKers Date: Wed, 22 Jun 2022 00:15:55 +0300 Subject: [PATCH 08/10] wrap fix --- video_creation/screenshot_downloader.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 1caa909..c833abe 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -74,7 +74,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): d1 = ImageDraw.Draw(img) font = ImageFont.truetype("arial.ttf", 22) - wrapper = textwrap.TextWrapper(width=60) + wrapper = textwrap.TextWrapper(width=50) wrapped_str = wrapper.fill(text=texts_in_tl) if (getenv("THEME").upper() == "DARK"): @@ -117,7 +117,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): comment_tl = ts.google(comment["comment_body"], to_language=os.getenv("POSTLANG")) - wrapper1 = textwrap.TextWrapper(width=78) + wrapper1 = textwrap.TextWrapper(width=70) wrapped_str1 = wrapper1.fill(text=comment_tl) d2 = ImageDraw.Draw(img_comment) @@ -131,6 +131,6 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): textmode1 = (0, 0, 0) d2.rectangle((30, 40, width2 - 5, height2 - 35), fill=fillmode1) - d2.text((40, 50), f"{wrapped_str1}", font=font_comment, fill=textmode1) + d2.text((35, 45), f"{wrapped_str1}", font=font_comment, fill=textmode1) img_comment.save(f"assets/temp/png/comment_{idx}.png") print_substep("Screenshots downloaded Successfully.", style="bold green") From 06f259a7c4249654f9aa8d6878704c84b484811d Mon Sep 17 00:00:00 2001 From: SinecKers Date: Wed, 22 Jun 2022 01:45:54 +0300 Subject: [PATCH 09/10] removed old img modify, added textcontent change --- video_creation/screenshot_downloader.py | 67 +++++++------------------ 1 file changed, 17 insertions(+), 50 deletions(-) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index c833abe..f954201 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -12,8 +12,6 @@ import json from rich.console import Console import translators as ts -from PIL import Image, ImageDraw, ImageFont -import textwrap console = Console() @@ -56,40 +54,22 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): '[data-click-id="text"] button' ).click() # Remove "Click to see nsfw" Button in Screenshot - page.locator('[data-test-id="post-content"]').screenshot( - path="assets/temp/png/title.png" - ) - # translate code if getenv("POSTLANG"): print_substep("Translating post...") texts_in_tl = ts.google(reddit_object["thread_title"], to_language=os.getenv("POSTLANG")) - img = Image.open("assets/temp/png/title.png") - - width = img.size[0] - height = img.size[1] - - d1 = ImageDraw.Draw(img) - font = ImageFont.truetype("arial.ttf", 22) - - wrapper = textwrap.TextWrapper(width=50) - wrapped_str = wrapper.fill(text=texts_in_tl) - - if (getenv("THEME").upper() == "DARK"): - fillmode = "#1a1a1b" - textmode = (255, 255, 255) - else: - fillmode = "whiite" - textmode = (0, 0, 0) - - d1.rectangle((7, 25, width - 20, height - 35), fill=fillmode) - d1.text((10, 30), f"{wrapped_str}", font=font, fill=textmode) - img.save("assets/temp/png/title.png") + 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" + ) + if storymode: page.locator('[data-click-id="text"]').screenshot( path="assets/temp/png/story_content.png" @@ -106,31 +86,18 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): page.locator('[data-testid="content-gate"] button').click() page.goto(f'https://reddit.com{comment["comment_url"]}', timeout=0) + + # translate code + + if getenv("POSTLANG"): + comment_tl = ts.google(comment["comment_body"], to_language=os.getenv("POSTLANG")) + print_substep("comment_tl: " + comment_tl) + 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( path=f"assets/temp/png/comment_{idx}.png" ) - if getenv("POSTLANG"): - img_comment = Image.open(f"assets/temp/png/comment_{idx}.png") - width2 = img_comment.size[0] - height2 = img_comment.size[1] - - comment_tl = ts.google(comment["comment_body"], to_language=os.getenv("POSTLANG")) - - wrapper1 = textwrap.TextWrapper(width=70) - wrapped_str1 = wrapper1.fill(text=comment_tl) - - d2 = ImageDraw.Draw(img_comment) - font_comment = ImageFont.truetype("arial.ttf", 16) - - if (getenv("THEME").upper() == "DARK"): - fillmode1 = "#242426" - textmode1 = (255, 255, 255) - else: - fillmode1 = "#F5F6F6" - textmode1 = (0, 0, 0) - - d2.rectangle((30, 40, width2 - 5, height2 - 35), fill=fillmode1) - d2.text((35, 45), f"{wrapped_str1}", font=font_comment, fill=textmode1) - img_comment.save(f"assets/temp/png/comment_{idx}.png") print_substep("Screenshots downloaded Successfully.", style="bold green") From 84bd523ed29f4995c4c899fc4868a79be52b005c Mon Sep 17 00:00:00 2001 From: SinecKers Date: Wed, 22 Jun 2022 01:49:08 +0300 Subject: [PATCH 10/10] remove unnecessary print --- video_creation/screenshot_downloader.py | 1 - 1 file changed, 1 deletion(-) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index f954201..469daf4 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -91,7 +91,6 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): if getenv("POSTLANG"): comment_tl = ts.google(comment["comment_body"], to_language=os.getenv("POSTLANG")) - print_substep("comment_tl: " + comment_tl) 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']] )