|
|
@ -1,3 +1,4 @@
|
|
|
|
|
|
|
|
import os
|
|
|
|
from gtts import gTTS
|
|
|
|
from gtts import gTTS
|
|
|
|
from pathlib import Path
|
|
|
|
from pathlib import Path
|
|
|
|
from mutagen.mp3 import MP3
|
|
|
|
from mutagen.mp3 import MP3
|
|
|
@ -12,18 +13,64 @@ def save_text_to_mp3(reddit_obj):
|
|
|
|
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 🎶")
|
|
|
|
print_step("Saving Text to MP3 files 🎶")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if os.getenv("GOOGLE_APPLICATION_CREDENTIALS"):
|
|
|
|
|
|
|
|
from google.cloud import texttospeech
|
|
|
|
|
|
|
|
|
|
|
|
length = 0
|
|
|
|
length = 0
|
|
|
|
|
|
|
|
|
|
|
|
# Create a folder for the mp3 files.
|
|
|
|
# Create a folder for the mp3 files.
|
|
|
|
Path("assets/mp3").mkdir(parents=True, exist_ok=True)
|
|
|
|
Path("assets/mp3").mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if os.getenv("GOOGLE_APPLICATION_CREDENTIALS"):
|
|
|
|
|
|
|
|
client = texttospeech.TextToSpeechClient()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
thread_title = texttospeech.SynthesisInput(text=reddit_obj["thread_title"])
|
|
|
|
|
|
|
|
voice = texttospeech.VoiceSelectionParams(
|
|
|
|
|
|
|
|
language_code="en-GB", name="en-GB-Wavenet-D"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
audio_config = texttospeech.AudioConfig(
|
|
|
|
|
|
|
|
audio_encoding=texttospeech.AudioEncoding.MP3
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response = client.synthesize_speech(
|
|
|
|
|
|
|
|
request={
|
|
|
|
|
|
|
|
"input": thread_title,
|
|
|
|
|
|
|
|
"voice": voice,
|
|
|
|
|
|
|
|
"audio_config": audio_config,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
with open("assets/mp3/title.mp3", "wb") as out:
|
|
|
|
|
|
|
|
out.write(response.audio_content)
|
|
|
|
|
|
|
|
length += MP3(f"assets/mp3/title.mp3").info.length
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for idx, comment in track(
|
|
|
|
|
|
|
|
enumerate(reddit_obj["comments"]), "Saving comments to MP3 files"
|
|
|
|
|
|
|
|
):
|
|
|
|
|
|
|
|
if length > 35:
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
comment_body = texttospeech.SynthesisInput(text=comment["comment_body"])
|
|
|
|
|
|
|
|
response = client.synthesize_speech(
|
|
|
|
|
|
|
|
request={
|
|
|
|
|
|
|
|
"input": comment_body,
|
|
|
|
|
|
|
|
"voice": voice,
|
|
|
|
|
|
|
|
"audio_config": audio_config,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
with open(f"assets/mp3/{idx}.mp3", "wb") as out:
|
|
|
|
|
|
|
|
out.write(response.audio_content)
|
|
|
|
|
|
|
|
length += MP3(f"assets/mp3/{idx}.mp3").info.length
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return length, idx
|
|
|
|
|
|
|
|
|
|
|
|
tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False, tld="co.uk")
|
|
|
|
tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False, tld="co.uk")
|
|
|
|
tts.save(f"assets/mp3/title.mp3")
|
|
|
|
tts.save(f"assets/mp3/title.mp3")
|
|
|
|
length += MP3(f"assets/mp3/title.mp3").info.length
|
|
|
|
length += MP3(f"assets/mp3/title.mp3").info.length
|
|
|
|
|
|
|
|
|
|
|
|
for idx, comment in track(enumerate(reddit_obj["comments"]), "Saving..."):
|
|
|
|
for idx, comment in track(enumerate(reddit_obj["comments"]), "Saving..."):
|
|
|
|
# ! 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:
|
|
|
|
if length > 35:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
tts = gTTS(text=comment["comment_body"], lang="en")
|
|
|
|
tts = gTTS(text=comment["comment_body"], lang="en")
|
|
|
|
tts.save(f"assets/mp3/{idx}.mp3")
|
|
|
|
tts.save(f"assets/mp3/{idx}.mp3")
|
|
|
|