From fdc055bcd4309f984eeecff29db446c063b54df1 Mon Sep 17 00:00:00 2001 From: PatatjeMC Date: Mon, 6 Jun 2022 16:14:38 +0200 Subject: [PATCH] Unlimited comment length I added unlimited comment length by splitting the tts is parts in a way that doesn't make it sound weird. It works great for me but if someone experiences and issues just contact me. --- .env.template | 1 + reddit/subreddit.py | 6 ++--- video_creation/TTSwrapper.py | 26 +++++++++++++++---- .../{cookie.json => cookie-dark-mode.json} | 6 +++++ video_creation/data/cookie-light-mode.json | 8 ++++++ video_creation/screenshot_downloader.py | 8 +++--- 6 files changed, 44 insertions(+), 11 deletions(-) rename video_creation/data/{cookie.json => cookie-dark-mode.json} (73%) create mode 100644 video_creation/data/cookie-light-mode.json diff --git a/.env.template b/.env.template index 3cd9dd4..54e872c 100644 --- a/.env.template +++ b/.env.template @@ -6,3 +6,4 @@ SUBREDDIT="AskReddit" ALLOW_NSFW="False" POST_ID="" THEME="LIGHT" +MAX_COMMENT_LENGTH="500" diff --git a/reddit/subreddit.py b/reddit/subreddit.py index c2a59df..c7ac06d 100755 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -16,7 +16,7 @@ def textify(text): def get_subreddit_threads(): """ - Returns a list of threads from the AskReddit subreddit. + Returns a list of threads from the selected subreddit. """ print_step("Getting subreddit threads...") @@ -24,7 +24,7 @@ def get_subreddit_threads(): content = {} load_dotenv() reddit = praw.Reddit(client_id=getenv("REDDIT_CLIENT_ID"), client_secret=getenv("REDDIT_CLIENT_SECRET"), - user_agent="Accessing AskReddit threads", username=getenv("REDDIT_USERNAME"), + user_agent="Accessing subreddit threads", username=getenv("REDDIT_USERNAME"), password=getenv("REDDIT_PASSWORD"), ) """ Ask user for subreddit input @@ -58,7 +58,7 @@ def get_subreddit_threads(): content["comments"] = [] for top_level_comment in submission.comments: - if len(top_level_comment.body) <= 250: + if len(top_level_comment.body) <= int(environ["MAX_COMMENT_LENGTH"]): content["comments"].append( {"comment_body": top_level_comment.body, "comment_url": top_level_comment.permalink, "comment_id": top_level_comment.id, }) diff --git a/video_creation/TTSwrapper.py b/video_creation/TTSwrapper.py index 0b50b15..6fa9d97 100644 --- a/video_creation/TTSwrapper.py +++ b/video_creation/TTSwrapper.py @@ -1,4 +1,6 @@ import requests, base64, random, os +import re +from moviepy.editor import AudioFileClip, concatenate_audioclips, CompositeAudioClip # https://twitter.com/scanlime/status/1512598559769702406 voices = [ # DISNEY VOICES @@ -55,15 +57,29 @@ class TTTTSWrapper: # TikTok Text-to-Speech Wrapper def tts(self, req_text: str = "TikTok Text To Speech", filename: str = 'title.mp3', random_speaker: bool = False): req_text = req_text.replace("+", "plus").replace(" ", "+").replace("&", "and") + voice = self.randomvoice() if random_speaker else 'en_us_002' - r = requests.post(f"{self.URI_BASE}{voice}&req_text={req_text}&speaker_map_type=0") - vstr = [r.json()["data"]["v_str"]][0] + chunks = [m.group().strip() for m in re.finditer(r' *((.{0,200})(\.|.$))',req_text)] + + audio_clips = [] + + chunkId = 0 + for chunk in chunks: + r = requests.post(f"{self.URI_BASE}{voice}&req_text={chunk}&speaker_map_type=0") + vstr = [r.json()["data"]["v_str"]][0] + b64d = base64.b64decode(vstr) + + with open(f"{filename}-{chunkId}", "wb") as out: + out.write(b64d) + + audio_clips.append(AudioFileClip(f"{filename}-{chunkId}")) - b64d = base64.b64decode(vstr) + chunkId = chunkId+1; - with open(filename, "wb") as out: - out.write(b64d) + audio_concat = concatenate_audioclips(audio_clips) + audio_composite = CompositeAudioClip([audio_concat]) + audio_composite.write_audiofile(filename, 44100, 2, 2000, None) @staticmethod def randomvoice(): diff --git a/video_creation/data/cookie.json b/video_creation/data/cookie-dark-mode.json similarity index 73% rename from video_creation/data/cookie.json rename to video_creation/data/cookie-dark-mode.json index 2e4e116..1ed51a9 100644 --- a/video_creation/data/cookie.json +++ b/video_creation/data/cookie-dark-mode.json @@ -4,5 +4,11 @@ "value": "eyJwcmVmcyI6eyJ0b3BDb250ZW50RGlzbWlzc2FsVGltZSI6MCwiZ2xvYmFsVGhlbWUiOiJSRURESVQiLCJuaWdodG1vZGUiOnRydWUsImNvbGxhcHNlZFRyYXlTZWN0aW9ucyI6eyJmYXZvcml0ZXMiOmZhbHNlLCJtdWx0aXMiOmZhbHNlLCJtb2RlcmF0aW5nIjpmYWxzZSwic3Vic2NyaXB0aW9ucyI6ZmFsc2UsInByb2ZpbGVzIjpmYWxzZX0sInRvcENvbnRlbnRUaW1lc0Rpc21pc3NlZCI6MH19", "domain": ".reddit.com", "path": "/" + }, + { + "name": "eu_cookie", + "value": "{%22opted%22:true%2C%22nonessential%22:false}", + "domain": ".reddit.com", + "path": "/" } ] diff --git a/video_creation/data/cookie-light-mode.json b/video_creation/data/cookie-light-mode.json new file mode 100644 index 0000000..87eeec9 --- /dev/null +++ b/video_creation/data/cookie-light-mode.json @@ -0,0 +1,8 @@ +[ + { + "name": "eu_cookie", + "value": "{%22opted%22:true%2C%22nonessential%22:false}", + "domain": ".reddit.com", + "path": "/" + } +] diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 12ea28d..48797c3 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -26,9 +26,11 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): context = browser.new_context() if getenv("THEME").upper() == "DARK": - cookie_file = open('./video_creation/data/cookie.json') - cookies = json.load(cookie_file) - context.add_cookies(cookies) + cookie_file = open('./video_creation/data/cookie-dark-mode.json') + else: + cookie_file = open('./video_creation/data/cookie-light-mode.json') + cookies = json.load(cookie_file) + context.add_cookies(cookies) # Get the thread screenshot page = context.new_page() page.goto(reddit_object["thread_url"])