-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.py
81 lines (66 loc) · 2.27 KB
/
stream.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from flask import Flask, Response, render_template
from datetime import datetime
from src.show import getShow
import threading
import time
import gc
app = Flask(__name__)
filepath = "./templates/show.mp3"
staticpath = "./templates/static.mp3"
format = "mpeg"
buffersize = 1024
def updateShow():
while True:
now = datetime.now()
hour = now.replace(hour=now.hour, minute=0, second=0, microsecond=0)
seconds_since_hour = (now - hour).total_seconds()
seconds_into_show = seconds_since_hour % (15 * 60)
if seconds_into_show >= (13 * 60):
threading.Thread(target=getShow, args=(filepath,)).start()
gc.collect()
time.sleep(10 * 60)
else:
time.sleep(30)
print("starting update show thread...")
threading.Thread(target=updateShow).start()
def deliverShow(offset):
with open(f'{filepath}', 'rb') as feed:
feed.seek(offset)
data = feed.read(buffersize)
while data:
yield data
data = feed.read(buffersize)
print("Finished sending")
def deliverStatic():
with open(f'{staticpath}', 'rb') as feed:
data = feed.read(buffersize)
while data:
yield data
data = feed.read(buffersize)
@app.route("/")
def home():
return render_template('index.html')
@app.route("/feed")
def stream():
length = 15 * 60
now = datetime.now()
hour = now.replace(hour=now.hour, minute=0, second=0, microsecond=0)
seconds_since_hour = (now - hour).total_seconds()
seconds_into_show = seconds_since_hour % length
byterate = 16000
offset = int(seconds_into_show * byterate)
response = Response(deliverShow(offset), mimetype=f"audio/{format}")
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
response.headers.pop("Content-Length", None)
return response
@app.route("/show")
def show():
response = Response(deliverShow(0), mimetype=f"audio/{format}")
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
response.headers.pop("Content-Length", None)
return response
# app.run()