hackerbots-live/app.py

52 lines
1.3 KiB
Python
Raw Normal View History

from flask import Flask, render_template
from flask_json import FlaskJSON, as_json
import requests
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
json = FlaskJSON(app)
socketio = SocketIO(app, logger=True, log_output=True)
socketio.init_app(app, cors_allowed_origins="*")
CURRENT_LISTENERS = 0
def heartbeat():
global CURRENT_LISTENERS
backendFetch = requests.get('http://hackerbots.net:8000/status-json.xsl')
stats = backendFetch.json()['icestats']['source']
print(stats)
return {
'title': stats.get('title', 'Unknown Track'),
'listeners': CURRENT_LISTENERS,
'show_title': 'Flask Test Show'
}
@socketio.on('live.hello')
def on_hello(*args, **kwargs):
print("Hello!!!")
@socketio.on('connect')
def on_new_listener():
global CURRENT_LISTENERS
CURRENT_LISTENERS += 1
socketio.emit('live.heartbeat', heartbeat())
@socketio.on('disconnect')
def on_new_listener():
global CURRENT_LISTENERS
CURRENT_LISTENERS -= 1
socketio.emit('live.heartbeat', heartbeat())
@app.route("/")
def index():
return render_template('index.html')
@app.route("/status.json")
@as_json
def status():
return heartbeat()
if __name__ == '__main__':
socketio.run(app, port=5000, host='0.0.0.0')