-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathannounce.py
65 lines (58 loc) · 1.85 KB
/
announce.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from flask import jsonify, send_from_directory
from logger import log
import os
import glob
import time
import ledticker, room
from conf import *
from subprocess import Popen, PIPE
from misc import error504
"""
Call a bash script which creates a mp3 with espeak content
and moves it into our restapi directory.
"""
def announce(lang, text):
if not room.isRoomOpen():
message = { 'success': False, 'status': 'Room is not open. Announcements are forbidden at the moment.' }
resp = jsonify(message)
resp.status_code = 403
elif lang in ['de', 'en']:
espeak = Popen([ESPEAK_LOCATION, lang, text], stdout=PIPE, stderr=PIPE)
returnMsg = espeak.communicate()[0]
if espeak.returncode > 0:
log('espeak returned "%s"' % returnMsg)
message = { 'success': False, 'status': 'Unknown error. Error logged.' }
else:
message = { 'success': True }
resp = jsonify(message)
else:
message = { 'success': False, 'status': 'Language not found.' }
resp = jsonify(message)
resp.status_code = 403
return resp
"""
When the mp3 announcement file is ready, call this method to deliver it.
"""
def serveAnnouncement(announcement):
try:
dlLocation = 'data/downloadable.mp3'
os.rename(announcement, API_PATH+'/'+dlLocation)
return send_from_directory(API_PATH, dlLocation, as_attachment=True)
except OSError:
log.exception('Could not move %s.' % announcement)
message = { 'success': False, 'status': 'Announcement is not ready yet.' }
resp = jsonify(message)
resp.status_code = 403
return resp
"""
Determine oldest announcement and return it.
"""
def serveOldestAnnouncement():
# get mp3 announces sorted by creation time, oldest first
mtime = lambda f: os.stat(f).st_mtime
announces = list(sorted(glob.glob(ANNOUNCE_LOCATION), key=mtime))
if len(announces) > 0:
# return oldest announcement
return serveAnnouncement(announces[0])
else:
return error504()