Skip to content

Instantly share code, notes, and snippets.

@LucaTNT
Last active April 20, 2022 15:53
Show Gist options
  • Save LucaTNT/e13f7ef98d26327496925b4520789e39 to your computer and use it in GitHub Desktop.
Save LucaTNT/e13f7ef98d26327496925b4520789e39 to your computer and use it in GitHub Desktop.
Script that synthesizes EasyApple intros with Azure's text-to-speech APIs
import requests
def getToken():
req = requests.post("https://westeurope.api.cognitive.microsoft.com/sts/v1.0/issuetoken",
headers={"Ocp-Apim-Subscription-Key": "YOUR_AZURE_API_KEY_HERE"},
)
return req.text
def textToMp3(text, voice="it-IT-ElsaNeural"):
headers = {
"Authorization": f"Bearer: {getToken()}",
"Content-Type": "application/ssml+xml",
"X-Microsoft-OutputFormat": "audio-24khz-160kbitrate-mono-mp3",
"User-Agent": "IntroEasyApple Python 0.1"
}
req = requests.post("https://westeurope.tts.speech.microsoft.com/cognitiveservices/v1",
headers=headers,
data=f"""<speak version='1.0' xml:lang='it-IT'><voice xml:lang='it-iT' xml:gender='Female'
name='it-IT-ElsaNeural'>
{text}
</voice></speak>"""
)
return req.content
def getLastEpisodeNumber(show="easyapple"):
req = requests.get(f"https://www.easypodcast.it/api/v1/show/{show}/lastEpisode")
response = req.json()
return int(response['episode']['episode_number'])
def getNextEpisodeNumber(show="easyapple"):
return getLastEpisodeNumber(show) + 1
next_episode_number = getNextEpisodeNumber()
donatori = "Luca Z., Federico F." # ultimi_donatori.main() # This is the actual function you don't have access to...
print(f"Prepring intro for episode {next_episode_number}…")
with open(f"/Users/luca/Downloads/intro-{next_episode_number}.mp3", "wb") as f:
testo = f"Benvenuti alla puntata {next_episode_number} di EasyApple. "
if donatori != 'Nessuno, purtroppo':
testo += f"Questa puntata è stata resa possibile dalle donazioni di {donatori}. "
testo += "E ora la parola ai conduttori, Luca Zorzi e Federico Travaini."
f.write(textToMp3(testo))
print("Done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment