Abstractized socketio with MessageInterface

This commit is contained in:
RemixDev 2020-04-15 14:49:40 +02:00
parent 75adf8914e
commit ef80347f20
2 changed files with 26 additions and 18 deletions

24
app.py
View file

@ -12,11 +12,11 @@ def initialize():
global settings global settings
settings = initSettings() settings = initSettings()
def shutdown(socket=None): def shutdown(interface=None):
print(getQueue()) print(getQueue())
cancelAllDownloads(socket) cancelAllDownloads(interface)
if socket: if interface:
socket.emit("toast", {'msg': "Server is closed."}) interface.send("toast", {'msg': "Server is closed."})
def mainSearch(dz, term): def mainSearch(dz, term):
return dz.search_main_gw(term) return dz.search_main_gw(term)
@ -24,17 +24,17 @@ def mainSearch(dz, term):
def search(dz, term, type, start, nb): def search(dz, term, type, start, nb):
return dz.search_gw(term, type, start, nb) return dz.search_gw(term, type, start, nb)
def addToQueue_link(dz, url, bitrate=None, socket=None): def addToQueue_link(dz, url, bitrate=None, interface=None):
return addToQueue(dz, url, settings, bitrate, socket) return addToQueue(dz, url, settings, bitrate, interface)
def removeFromQueue_link(uuid, socket=None): def removeFromQueue_link(uuid, interface=None):
removeFromQueue(uuid, socket) removeFromQueue(uuid, interface)
def cancelAllDownloads_link(socket=None): def cancelAllDownloads_link(interface=None):
cancelAllDownloads(socket) cancelAllDownloads(interface)
def removeFinishedDownloads_link(socket=None): def removeFinishedDownloads_link(interface=None):
removeFinishedDownloads(socket) removeFinishedDownloads(interface)
def getSettings_link(): def getSettings_link():
return getSettings() return getSettings()

View file

@ -7,6 +7,7 @@ from flask import Flask, render_template, request, session
from flask_socketio import SocketIO, emit from flask_socketio import SocketIO, emit
import logging import logging
from deemix.api.deezer import Deezer from deemix.api.deezer import Deezer
from deemix.app.MessageInterface import MessageInterface
import app import app
import time import time
import sys import sys
@ -22,6 +23,7 @@ class CustomFlask(Flask):
comment_end_string='#$', comment_end_string='#$',
)) ))
gui_dir = os.path.join(os.path.dirname(__file__), 'public') # development path gui_dir = os.path.join(os.path.dirname(__file__), 'public') # development path
if not os.path.exists(gui_dir): # frozen executable path if not os.path.exists(gui_dir): # frozen executable path
gui_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'public') gui_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'public')
@ -29,6 +31,12 @@ server = CustomFlask(__name__, static_folder=gui_dir, template_folder=gui_dir)
server.config['SEND_FILE_MAX_AGE_DEFAULT'] = 1 # disable caching server.config['SEND_FILE_MAX_AGE_DEFAULT'] = 1 # disable caching
socketio = SocketIO(server) socketio = SocketIO(server)
class SocketInterface(MessageInterface):
def send(self, message, value):
socketio.emit(message, value)
socket_interface = SocketInterface()
serverLog = logging.getLogger('werkzeug') serverLog = logging.getLogger('werkzeug')
serverLog.disabled = True serverLog.disabled = True
server.logger.disabled = True server.logger.disabled = True
@ -41,7 +49,7 @@ def landing():
@server.route('/shutdown') @server.route('/shutdown')
def closing(): def closing():
app.shutdown(socket=socketio) app.shutdown(interface=socket_interface)
func = request.environ.get('werkzeug.server.shutdown') func = request.environ.get('werkzeug.server.shutdown')
func() func()
return 'server closed' return 'server closed'
@ -110,26 +118,26 @@ def search(data):
@socketio.on('addToQueue') @socketio.on('addToQueue')
def addToQueue(data): def addToQueue(data):
result = app.addToQueue_link(session['dz'], data['url'], socket=socketio) result = app.addToQueue_link(session['dz'], data['url'], interface=socket_interface)
if result == "Not logged in": if result == "Not logged in":
emit('toast', {'msg': "You need to log in to download tracks!", 'icon': 'report'}) emit('toast', {'msg': "You need to log in to download tracks!", 'icon': 'report'})
@socketio.on('removeFromQueue') @socketio.on('removeFromQueue')
def removeFromQueue(uuid): def removeFromQueue(uuid):
app.removeFromQueue_link(uuid, socket=socketio) app.removeFromQueue_link(uuid, interface=socket_interface)
@socketio.on('removeFinishedDownloads') @socketio.on('removeFinishedDownloads')
def removeFinishedDownloads(): def removeFinishedDownloads():
app.removeFinishedDownloads_link(socket=socketio) app.removeFinishedDownloads_link(interface=socket_interface)
@socketio.on('cancelAllDownloads') @socketio.on('cancelAllDownloads')
def cancelAllDownloads(): def cancelAllDownloads():
app.cancelAllDownloads_link(socket=socketio) app.cancelAllDownloads_link(interface=socket_interface)
@socketio.on('saveSettings') @socketio.on('saveSettings')
def saveSettings(settings): def saveSettings(settings):
app.saveSettings_link(settings) app.saveSettings_link(settings)
socketio.emit('updateSettings', settings) socket_interface.send('updateSettings', settings)
# Example code leftover, could be usefull later on # Example code leftover, could be usefull later on
@server.route('/choose/path', methods=['POST']) @server.route('/choose/path', methods=['POST'])