- これをcloneする
- virtualenvを用意する
pip install -r requirements.txt
で依存パッケージをインストールするpython server.py
でサーバーを起動する- ブラウザで、サーバーにアクセスして、フォームにテキストと言語を指定して「喋る」ボタンを押す
- Google Homeが喋った!
Last active
November 23, 2020 13:56
-
-
Save attakei/68936f7378ca4b95cb75e5d0cfd21c2a to your computer and use it in GitHub Desktop.
Google home notifications by python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pychromecast | |
gTTS | |
bottle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
import hashlib | |
import logging | |
from pathlib import Path | |
from bottle import Bottle, request, template, static_file | |
from gtts import gTTS | |
import pychromecast | |
BASE_DIR = Path(__file__).parent | |
TALK_DIR = BASE_DIR / 'var' | |
app = Bottle() | |
@app.route('/talks/<file_path:path>') | |
def post_talk(file_path): | |
return static_file(file_path, root=TALK_DIR) | |
@app.route('/form', method='GET') | |
def get_talk_form(): | |
content = BASE_DIR / 'template.html' | |
text = request.forms.text or '' | |
lang = request.forms.lang or 'ja' | |
return template( | |
content.open().read(), langs=gTTS.LANGUAGES, lang=lang, text=text) | |
@app.route('/form', method='POST') | |
def post_talk_form(): | |
text = request.forms.text | |
lang = request.forms.lang | |
text_token = generate_talk(text, lang) | |
url = f"http://{app.host}:{app.port}/talks/{text_token}" | |
chromecasts = pychromecast.get_chromecasts() | |
cast = chromecasts[0] | |
mc = cast.media_controller | |
mc.play_media(url, 'audio/mp3') | |
return get_talk_form() | |
def generate_talk(text, lang): | |
text_token = hashlib.sha256((lang + text).encode()).hexdigest() | |
talk_path = TALK_DIR / text_token | |
if not talk_path.exists(): | |
tts = gTTS(text=text, lang=lang) | |
tts.save(talk_path) | |
return text_token | |
if __name__ == '__main__': | |
if not TALK_DIR.exists(): | |
TALK_DIR.mkdir() | |
app.host = os.environ.get('SERVER_HOST', '192.168.0.20') | |
app.port = os.environ.get('SERVER_PORT', '8080') | |
app.run(host='0.0.0.0', port=app.port, reloader=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Google Home notifier</h1> | |
<hr /> | |
<form method="POST"> | |
<p> | |
Text: | |
<textarea name="text">{{text}}</textarea> | |
</p> | |
<p> | |
Lang: | |
<select name="lang"> | |
% for code, title in langs.items(): | |
% if code == lang: | |
<option value="{{code}}" selected>{{title}}</option> | |
% else: | |
<option value="{{code}}">{{title}}</option> | |
% end | |
% end | |
</select> | |
</p> | |
<p> | |
<button>喋る</button> | |
</p> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment