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