Skip to content

Commit 9b18bb0

Browse files
committed
migrated to python for stability sake
1 parent eaf8597 commit 9b18bb0

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

main.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from flask import Flask, request
2+
import socket
3+
import pychromecast
4+
import logging
5+
from gtts import gTTS
6+
from slugify import slugify
7+
from pathlib import Path
8+
from urllib.parse import urlparse
9+
10+
logging.basicConfig(level=logging.INFO)
11+
logger = logging.getLogger(__name__)
12+
13+
14+
app = Flask(__name__)
15+
logging.info("Starting up chromecasts")
16+
chromecasts = pychromecast.get_chromecasts()
17+
cast = next(cc for cc in chromecasts if cc.device.friendly_name == "Home group")
18+
19+
def play_tts(text, lang='en', slow=False):
20+
tts = gTTS(text=text, lang=lang, slow=slow)
21+
filename = slugify(text+"-"+lang+"-"+str(slow)) + ".mp3"
22+
path = "/static/cache/"
23+
cache_filename = "." + path + filename
24+
tts_file = Path(cache_filename)
25+
if not tts_file.is_file():
26+
logging.info(tts)
27+
tts.save(cache_filename)
28+
29+
urlparts = urlparse(request.url)
30+
mp3_url = "http://" +urlparts.netloc + path + filename
31+
logging.info(mp3_url)
32+
play_mp3(mp3_url)
33+
34+
35+
def play_mp3(mp3_url):
36+
print(mp3_url)
37+
cast.wait()
38+
mc = cast.media_controller
39+
mc.play_media(mp3_url, 'audio/mp3')
40+
41+
@app.route('/static/<path:path>')
42+
def send_static(path):
43+
return send_from_directory('static', path)
44+
45+
@app.route('/play/<filename>')
46+
def play(filename):
47+
urlparts = urlparse(request.url)
48+
mp3 = Path("./static/"+filename)
49+
if mp3.is_file():
50+
play_mp3("http://"+urlparts.netloc+"/static/"+filename)
51+
return filename
52+
else:
53+
return "False"
54+
55+
@app.route('/say/')
56+
def say():
57+
text = request.args.get("text")
58+
lang = request.args.get("lang")
59+
if not text:
60+
return False
61+
if not lang:
62+
lang = "en"
63+
play_tts(text, lang=lang)
64+
return text
65+
66+
if __name__ == '__main__':
67+
app.run(debug=True,host='0.0.0.0')

notify.sh

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
3+
BASEDIR=$(dirname $0)
4+
cd $BASEDIR
5+
6+
IMAGE_NAME="harperreed/notifier"
7+
CONTAINER_NAME=notifier
8+
9+
ACTION=$1
10+
11+
if [ -z "$ACTION" ];
12+
then
13+
echo "usage: $0 <build|run|stop|start|remove|rerun|attach|push|logs|debug>";
14+
exit 1;
15+
fi
16+
17+
# Build
18+
_build() {
19+
docker build --tag="$IMAGE_NAME" .
20+
}
21+
22+
# Run (first time)
23+
_run() {
24+
docker run -d --name $CONTAINER_NAME --net=host $IMAGE_NAME
25+
}
26+
27+
# Debugging mode with terminal access
28+
_debug() {
29+
docker run -i -t --entrypoint /bin/bash --name $CONTAINER_NAME --net=host $IMAGE_NAME
30+
}
31+
32+
# Stop
33+
_stop() {
34+
docker stop $CONTAINER_NAME
35+
}
36+
37+
# Start (after stopping)
38+
_start() {
39+
docker start $CONTAINER_NAME
40+
}
41+
42+
# Remove
43+
_remove() {
44+
docker rm $CONTAINER_NAME
45+
}
46+
47+
# Remove container and create a new one
48+
_rerun() {
49+
_stop
50+
_remove
51+
_run
52+
}
53+
54+
# Manually open bash
55+
_attach() {
56+
docker exec -ti $CONTAINER_NAME /bin/bash
57+
}
58+
59+
# Container logs
60+
_logs() {
61+
docker logs $CONTAINER_NAME
62+
}
63+
64+
# Publish contents
65+
_push() {
66+
docker push $IMAGE_NAME
67+
}
68+
69+
eval _$ACTION

requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
gTTS==1.2.2
2+
Flask==0.12.2
3+
pychromecast==0.8.2
4+
unicode_slugify==0.1.3
5+
pathlib==1.0.1

0 commit comments

Comments
 (0)