Compare commits
8 commits
dbbf1b757c
...
8cf956c2f0
Author | SHA1 | Date | |
---|---|---|---|
8cf956c2f0 | |||
03f2f966d7 | |||
ea96df1774 | |||
08287a9758 | |||
2306bbc2e7 | |||
90746d58b2 | |||
8f27f8d216 | |||
658c676be8 |
3 changed files with 117 additions and 29 deletions
52
app.py
52
app.py
|
@ -55,28 +55,48 @@ app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
def render(data={}):
|
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)
|
return chevron.render(template=open('index.mustache', 'r'), data=data)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/serverstatus", methods=['GET'])
|
@app.route("/api/serverstatus", methods=['GET'])
|
||||||
def getServerStatus():
|
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 = session.get(stream_data['stream_url'] + '/api/yp')
|
||||||
response_data = response.json()
|
response_data = response.json()
|
||||||
return json.dumps({
|
data = {
|
||||||
'name': response_data['name'],
|
'name': response_data['name'],
|
||||||
'online': response_data['online'],
|
'online': response_data['online'],
|
||||||
'overallMaxViewerCount': response_data['overallMaxViewerCount'],
|
'overallMaxViewerCount': response_data['overallMaxViewerCount'],
|
||||||
'sessionMaxViewerCount': response_data['sessionMaxViewerCount'],
|
'sessionMaxViewerCount': response_data['sessionMaxViewerCount'],
|
||||||
'streamTitle': response_data['streamTitle'],
|
|
||||||
'viewerCount': response_data['viewerCount'],
|
'viewerCount': response_data['viewerCount'],
|
||||||
'description': response_data['description'],
|
'description': response_data['description'],
|
||||||
'tags': response_data['tags'],
|
'tags': response_data['tags'],
|
||||||
'nsfw': response_data['nsfw'],
|
'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'])
|
@app.route("/api/update/streamtitle", methods=['POST'])
|
||||||
def updateStreamTitle():
|
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(
|
response = session.post(
|
||||||
stream_data['stream_url'] + '/api/admin/config/streamtitle',
|
stream_data['stream_url'] + '/api/admin/config/streamtitle',
|
||||||
data=json.dumps({'value': request.get_json(force=True)})
|
data=json.dumps({'value': request.get_json(force=True)})
|
||||||
|
@ -85,29 +105,27 @@ def updateStreamTitle():
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/update/servertags', methods=['POST'])
|
@app.route('/api/update/servertags', methods=['POST'])
|
||||||
def updateServerTags(tags_list: list):
|
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(',')]
|
||||||
response = session.post(
|
response = session.post(
|
||||||
stream_data['stream_url'] + '/api/admin/config/tags',
|
stream_data['stream_url'] + '/api/admin/config/tags',
|
||||||
data=json.dumps({
|
data=json.dumps({
|
||||||
'value': tags_list
|
'value': values
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
return response.status_code, json.loads(response.text)
|
return Response(status=response.status_code)
|
||||||
|
|
||||||
|
|
||||||
@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("/")
|
@app.route("/")
|
||||||
def index():
|
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()))
|
return render(json.loads(getServerStatus()))
|
||||||
|
|
||||||
|
|
||||||
|
|
56
resources/script.js
Normal file
56
resources/script.js
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
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()
|
|
@ -4,29 +4,43 @@ async function getEndpoint(url = '') {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uiUpdate() {
|
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() {
|
||||||
window.setInterval(async () => {
|
window.setInterval(async () => {
|
||||||
data = await getEndpoint('http://127.0.0.1:5000/api/serverstatus');
|
uiUpdateOnce();
|
||||||
document.getElementById("streamTitle").innerHTML = data.streamTitle
|
}, 1000);
|
||||||
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) {
|
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',
|
response = await fetch('http://127.0.0.1:5000/api/update/streamtitle',
|
||||||
{
|
{
|
||||||
method:'POST',
|
method:'POST',
|
||||||
body:JSON.stringify(value)
|
body:JSON.stringify(value)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
await uiUpdate()
|
await uiUpdateOnce();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function Main() {
|
async function updateTags(value) {
|
||||||
await uiUpdate()
|
// 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
Main()
|
continuousUiUpdate()
|
||||||
|
|
Loading…
Reference in a new issue