added profane filter in voices.py

pull/447/head
Drugsosos 3 years ago
parent 862d7f25e3
commit 37d9d050ca
No known key found for this signature in database
GPG Key ID: F96ED7B0753A13E6

File diff suppressed because it is too large Load Diff

@ -5,10 +5,26 @@ from mutagen.mp3 import MP3
from utils.console import print_step, print_substep
from rich.progress import track
from rich.console import Console
console = Console()
import re
def save_text_to_mp3(reddit_obj):
from video_creation.profane_list.en import profane_list
console = Console()
def profane_filter(text: str, word_list: list) -> str:
word_list.sort(reverse=True, key=len)
filtered_text = text
if any([word in word_list for word in text.split()]):
for word in word_list:
if word in text:
word_lenght = word.__len__()
word_half = word_lenght // 2 if word_lenght % 2 == 0 else word_lenght // 2 + 1
filtered_text = re.sub(word, f'{word[:word_half]}{(word_lenght - word_half) * "*"}', filtered_text)
return filtered_text
def save_text_to_mp3(reddit_obj: dict) -> tuple:
"""Saves Text to MP3 files.
Args:
@ -20,7 +36,7 @@ def save_text_to_mp3(reddit_obj):
# Create a folder for the mp3 files.
Path("assets/mp3").mkdir(parents=True, exist_ok=True)
tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False)
tts = gTTS(text=profane_filter(reddit_obj["thread_title"], profane_list), lang="en", slow=False)
tts.save("assets/mp3/title.mp3")
length += MP3("assets/mp3/title.mp3").info.length
@ -36,19 +52,22 @@ def save_text_to_mp3(reddit_obj):
for idx, comment in track(enumerate(reddit_obj["comments"])):
#allow user to see what comment is being saved
# Allow user to see what comment is being saved
print_substep(f"Saving MP3 {idx + 1} ")
# ! Stop creating mp3 files if the length is greater than 50 seconds. This can be longer, but this is just a good starting point
# Stop creating mp3 files if the length is greater than 50 seconds.
# This can be longer, but this is just a good starting point
if length > 50:
break
comment=comment["comment_body"]
text=re.sub('((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*', '', comment)
comment = comment["comment_body"]
text = re.sub('((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*',
'', comment)
text = profane_filter(text, profane_list)
tts = gTTS(text, lang="en", slow=False)
tts.save(f"assets/mp3/{idx}.mp3")
length += MP3(f"assets/mp3/{idx}.mp3").info.length
#let user know that the MP3 files are saved
# Let user know that the MP3 files are saved
console.log(f"[bold green]Saved {idx + 1} MP3 Files.")
# ! Return the index so we know how many screenshots of comments we need to make.
# Return the index, so we know how many screenshots of comments we need to make.
return length, idx

Loading…
Cancel
Save