diff --git a/app.py b/app.py index 1b3c2f5..80e7555 100755 --- a/app.py +++ b/app.py @@ -55,48 +55,28 @@ app = Flask(__name__) def render(data={}): - """ Template rendering function using mustache (via the pypi chevron - implementation. - - :returns: A full HTML template with relevant data from the server. - """ return chevron.render(template=open('index.mustache', 'r'), data=data) @app.route("/api/serverstatus", methods=['GET']) def getServerStatus(): - """ Obtains useful information from the server from either /api/yp or - /api/status. - - :returns: the collected data as a JSON formatted dict. - """ response = session.get(stream_data['stream_url'] + '/api/yp') response_data = response.json() - data = { + return json.dumps({ 'name': response_data['name'], 'online': response_data['online'], 'overallMaxViewerCount': response_data['overallMaxViewerCount'], 'sessionMaxViewerCount': response_data['sessionMaxViewerCount'], + 'streamTitle': response_data['streamTitle'], 'viewerCount': response_data['viewerCount'], 'description': response_data['description'], 'tags': response_data['tags'], 'nsfw': response_data['nsfw'], - } - - response = session.get(stream_data['stream_url'] + '/api/status') - response_data = response.json() - - data['streamTitle'] = response_data['streamTitle'] - - return json.dumps(data) + }) @app.route("/api/update/streamtitle", methods=['POST']) def updateStreamTitle(): - """ An endpoint to allow the user to update the stream title. - - :returns: the status code of the post request made to the server. - """ response = session.post( stream_data['stream_url'] + '/api/admin/config/streamtitle', data=json.dumps({'value': request.get_json(force=True)}) @@ -105,27 +85,29 @@ def updateStreamTitle(): @app.route('/api/update/servertags', methods=['POST']) -def updateServerTags(): - """ An endpoint to allow the user to update the stream title. - - :returns: the status code of the post request made to the server. - """ - values = [i.strip() for i in request.get_json(force=True).split(',')] +def updateServerTags(tags_list: list): response = session.post( stream_data['stream_url'] + '/api/admin/config/tags', data=json.dumps({ - 'value': values + 'value': tags_list }) ) - return Response(status=response.status_code) + return response.status_code, json.loads(response.text) + + +@app.route('/api/update/nsfw', methods=['POST']) +def updateServerNSFW(boolean: bool): + response = session.post( + stream_data['stream_url'] + '/api/admin/config/nsfw', + data=json.dumps({ + 'value': boolean + }) + ) + return response.status_code, json.loads(response.text) @app.route("/") def index(): - """ A simple initial endpoint that loads in relevant data from the server. - - :returns: the rendered template (see app.render for more) - """ return render(json.loads(getServerStatus())) diff --git a/resources/script.js b/resources/script.js deleted file mode 100644 index f7780fd..0000000 --- a/resources/script.js +++ /dev/null @@ -1,56 +0,0 @@ -const headers = new Headers(); -// note, userName and streamKey are both derived in a file called data.js -headers.append('Authorization', 'Basic ' + btoa(userName + ":" + streamKey)); - -async function getEndpoint(url = '') { - const response = await fetch(url, {method:'GET', headers: headers}) - const data = await response.json() - return data - } - - async function postEndpoint(url = '', data = {}) { - const response = await fetch(url, { - method: 'POST', - body: JSON.stringify(data), // serialized data - headers: headers // authentication header - }) - return response - } - - async function getStatus() { - return getEndpoint(api_url + 'admin/status') - } - - async function updateElements(data) { - var {online, viewerCount, sessionPeakViewerCount} = data - updateViewers(viewerCount); - updateOnline(online); - updateSessionPeak(sessionPeakViewerCount); - -} - -async function updateOnline(online) { - document.getElementById('online').innerHTML = online -} -async function updateViewers(viewers) { - document.getElementById('currentViewers').innerHTML = viewers -} -async function updateSessionPeak(viewers) { - document.getElementById('sessionPeak').innerHTML = viewers -} - -async function Main() { - // update visual elements using data from 'api/admin/status' - // updateElements(await getStatus()) - const response = await fetch(api_url + 'yp', {mode:'no-cors'}) - const data = await response.json() - - // update the broadcast title - // postEndpoint(`` - // api_url + 'admin/config/streamtitle', - // {value: "testing" + Math.floor(Math.random() * 1000)} - // ).then(response => response.status) - // .then(data => console.log(data)) -} - -// Main() diff --git a/static/script.js b/static/script.js index c5c5a60..d9e4f3f 100644 --- a/static/script.js +++ b/static/script.js @@ -4,43 +4,29 @@ async function getEndpoint(url = '') { return data; } -async function uiUpdateOnce() { - var data = await getEndpoint('http://127.0.0.1:5000/api/serverstatus'); - document.getElementById("streamTitle").innerHTML = data.streamTitle; - document.getElementById("currentViewers").innerHTML = data.viewerCount; - document.getElementById("sessionMaxViewerCount").innerHTML = data.sessionMaxViewerCount; - document.getElementById("overallMaxViewerCount").innerHTML = data.overallMaxViewerCount; - document.getElementById("tags").innerHTML = data.tags; -} - -async function continuousUiUpdate() { +async function uiUpdate() { window.setInterval(async () => { - uiUpdateOnce(); - }, 1000); + data = await getEndpoint('http://127.0.0.1:5000/api/serverstatus'); + document.getElementById("streamTitle").innerHTML = data.streamTitle + document.getElementById("currentViewers").innerHTML = data.viewerCount + document.getElementById("sessionMaxViewerCount").innerHTML = data.sessionMaxViewerCount + document.getElementById("overallMaxViewerCount").innerHTML = data.overallMaxViewerCount + document.getElementById("tags").innerHTML = data.tags + }, 1000) } async function updateStreamTitle(value) { - // this function simply passes data along to the python script, and allows - // python to handle data processing response = await fetch('http://127.0.0.1:5000/api/update/streamtitle', { method:'POST', body:JSON.stringify(value) } ); - await uiUpdateOnce(); + await uiUpdate() } -async function updateTags(value) { - // this function simply passes data along to the python script, and allows - // python to handle data processing - response = await fetch('http://127.0.0.1:5000/api/update/servertags', - { - method:'POST', - body:JSON.stringify(value) - } - ); - await uiUpdateOnce(); +async function Main() { + await uiUpdate() } -continuousUiUpdate() +Main()