add listeners count function
This commit is contained in:
parent
8bf430e040
commit
9ee5d9547a
@ -4,5 +4,5 @@ pkgs.python3Packages.buildPythonApplication {
|
||||
pname = "radio";
|
||||
src = self;
|
||||
version = "0.1";
|
||||
propagatedBuildInputs = with pkgs.python3Packages; [ pip ffmpeg-python flask ];
|
||||
}
|
||||
propagatedBuildInputs = with pkgs.python3Packages; [ pip ffmpeg-python flask requests pkgs.ffmpeg ];
|
||||
}
|
||||
|
15
radio.py
15
radio.py
@ -4,14 +4,19 @@ import transcoder
|
||||
from time import sleep
|
||||
from flask import Flask, request
|
||||
from queue import Queue
|
||||
import json
|
||||
import requests
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
host = "localhost:8000"
|
||||
stream = "stream.mp3"
|
||||
|
||||
class Radio(object):
|
||||
def __init__(self):
|
||||
self.downloader = None
|
||||
self.transcoder = None
|
||||
self.uploader = uploader.Uploader()
|
||||
self.uploader = uploader.Uploader(host, stream)
|
||||
self.playingUrl = None
|
||||
self.queue = Queue()
|
||||
|
||||
@ -52,6 +57,10 @@ class Radio(object):
|
||||
self.stopPlaying()
|
||||
self.playIfSongAvailable()
|
||||
|
||||
def listenerCount(self):
|
||||
r = requests.get("http://" + host + "/status-json.xsl")
|
||||
return json.loads(r.text)['icestats']['source']['listeners']
|
||||
|
||||
r = Radio()
|
||||
|
||||
@app.route('/play', methods=['POST'])
|
||||
@ -75,6 +84,10 @@ def current():
|
||||
def queue():
|
||||
return str(list(r.queue.queue))
|
||||
|
||||
@app.route('/listeners', methods=['GET'])
|
||||
def listeners():
|
||||
return str(r.listenerCount())
|
||||
|
||||
def run():
|
||||
app.run(host="0.0.0.0")
|
||||
|
||||
|
@ -48,14 +48,12 @@ class StreamListener(Thread):
|
||||
def run(self):
|
||||
while main_thread().is_alive() and not self.quit:
|
||||
while True:
|
||||
output = None
|
||||
if not self.stream is None:
|
||||
output = non_block_read(self.stream)
|
||||
if output == None or output == b'':
|
||||
if not self.backupStream is None:
|
||||
output = non_block_read(self.backupStream)
|
||||
if output == None or output == b'':
|
||||
break
|
||||
else:
|
||||
break
|
||||
self.listener.write(output)
|
||||
if (output == None or output == b'') and not self.backupStream is None:
|
||||
output = non_block_read(self.backupStream)
|
||||
if output == None or output == b'':
|
||||
break
|
||||
self.listener.write(output)
|
||||
sleep(0.1)
|
@ -6,13 +6,15 @@ import nullsrc
|
||||
|
||||
class Uploader(object):
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, host, stream):
|
||||
self.host = host
|
||||
self.stream = stream
|
||||
self.connect()
|
||||
|
||||
def connect(self):
|
||||
self.process = ( ffmpeg
|
||||
.input('pipe:', re=None)
|
||||
.output("icecast://source:hackme@localhost:8000/stream.mp3", format='mp3', content_type="audio/mpeg")
|
||||
.output("icecast://source:hackme@" + self.host + "/" + self.stream, format='mp3', content_type="audio/mpeg")
|
||||
.run_async(pipe_stdin=True, pipe_stderr=True)
|
||||
)
|
||||
logger.add(self.process.stderr, "uploader.log")
|
||||
|
Loading…
x
Reference in New Issue
Block a user