2021-02-25 20:40:58 +01:00

87 lines
2.0 KiB
Python

import omitted
import os.path
from bottle import route, run, static_file, template
from os import path
from flask import request
import hashlib
from gtts import gTTS
import pychromecast
BASE_DIR = '/home/thomas/googleHome'
TALK_DIR = '/home/thomas/googleHome/var'
#app = bottle()
@route('/talks/<file_path:path>')
def get_talk_mp3(file_path):
print(file_path)
print(TALK_DIR)
return static_file(file_path, root=TALK_DIR)
@route('/form/<text>/<lang>', method='GET')
def get_talk_form(text, lang):
"" "form display" ""
content = BASE_DIR + '/template.html'
text = text or ''
lang = lang or 'ja'
return template(content.open().read(), langs=gTTS.LANGUAGES, lang=lang, text=text)
@route('/form/<text>/<lang>', method='POST')
def post_talk_form(text, lang):
"" "Save the talk text, playback instruction" ""
print('ceci est un tesssst')
print(text)
print(lang)
text_token = generate_talk(text, lang)
#because troublesome, beating decided to GoogleCast terminal attached to the eye
chromecasts, browser = pychromecast.get_listed_chromecasts(friendly_names=["Bureau Mini"])
#chromecasts = pychromecast.get_chromecasts()
chromecasts[0].wait()
print(chromecasts[0].device)
cast = chromecasts[0]
print(f"http://192.168.31.140:8080/talks/{text_token}")
mc = cast.media_controller
mc.play_media(f"http://192.168.31.140:8080/talks/{text_token}", 'audio/mp3')
mc.block_until_active()
print(mc.status)
return get_talk_form(text, lang)
def generate_talk(text, lang):
"" "languages to use the cache When not match the text" ""
text_token = hashlib.sha256((lang + text).encode()).hexdigest()
text_token+='.mp3'
talk_path = TALK_DIR+ '/' + text_token
if not path.exists(talk_path):
tts = gTTS(text=text, lang=lang)
tts.save(talk_path)
print(talk_path)
print(text_token)
return text_token
if __name__ == '__main__':
if not path.isdir(TALK_DIR):
os.mkdir(TALK_DIR)
run(host='0.0.0.0', port='8080', reloader=True)