From ef80347f2079b773a055b97a4a63b8cf27b39dd5 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Wed, 15 Apr 2020 14:49:40 +0200 Subject: [PATCH] Abstractized socketio with MessageInterface --- app.py | 24 ++++++++++++------------ server.py | 20 ++++++++++++++------ 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/app.py b/app.py index b32d48c..c7ab4a8 100644 --- a/app.py +++ b/app.py @@ -12,11 +12,11 @@ def initialize(): global settings settings = initSettings() -def shutdown(socket=None): +def shutdown(interface=None): print(getQueue()) - cancelAllDownloads(socket) - if socket: - socket.emit("toast", {'msg': "Server is closed."}) + cancelAllDownloads(interface) + if interface: + interface.send("toast", {'msg': "Server is closed."}) def mainSearch(dz, term): return dz.search_main_gw(term) @@ -24,17 +24,17 @@ def mainSearch(dz, term): def search(dz, term, type, start, nb): return dz.search_gw(term, type, start, nb) -def addToQueue_link(dz, url, bitrate=None, socket=None): - return addToQueue(dz, url, settings, bitrate, socket) +def addToQueue_link(dz, url, bitrate=None, interface=None): + return addToQueue(dz, url, settings, bitrate, interface) -def removeFromQueue_link(uuid, socket=None): - removeFromQueue(uuid, socket) +def removeFromQueue_link(uuid, interface=None): + removeFromQueue(uuid, interface) -def cancelAllDownloads_link(socket=None): - cancelAllDownloads(socket) +def cancelAllDownloads_link(interface=None): + cancelAllDownloads(interface) -def removeFinishedDownloads_link(socket=None): - removeFinishedDownloads(socket) +def removeFinishedDownloads_link(interface=None): + removeFinishedDownloads(interface) def getSettings_link(): return getSettings() diff --git a/server.py b/server.py index f9d65e2..011d1fe 100644 --- a/server.py +++ b/server.py @@ -7,6 +7,7 @@ from flask import Flask, render_template, request, session from flask_socketio import SocketIO, emit import logging from deemix.api.deezer import Deezer +from deemix.app.MessageInterface import MessageInterface import app import time import sys @@ -22,6 +23,7 @@ class CustomFlask(Flask): comment_end_string='#$', )) + gui_dir = os.path.join(os.path.dirname(__file__), 'public') # development path if not os.path.exists(gui_dir): # frozen executable path 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 socketio = SocketIO(server) +class SocketInterface(MessageInterface): + def send(self, message, value): + socketio.emit(message, value) + +socket_interface = SocketInterface() + serverLog = logging.getLogger('werkzeug') serverLog.disabled = True server.logger.disabled = True @@ -41,7 +49,7 @@ def landing(): @server.route('/shutdown') def closing(): - app.shutdown(socket=socketio) + app.shutdown(interface=socket_interface) func = request.environ.get('werkzeug.server.shutdown') func() return 'server closed' @@ -110,26 +118,26 @@ def search(data): @socketio.on('addToQueue') 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": emit('toast', {'msg': "You need to log in to download tracks!", 'icon': 'report'}) @socketio.on('removeFromQueue') def removeFromQueue(uuid): - app.removeFromQueue_link(uuid, socket=socketio) + app.removeFromQueue_link(uuid, interface=socket_interface) @socketio.on('removeFinishedDownloads') def removeFinishedDownloads(): - app.removeFinishedDownloads_link(socket=socketio) + app.removeFinishedDownloads_link(interface=socket_interface) @socketio.on('cancelAllDownloads') def cancelAllDownloads(): - app.cancelAllDownloads_link(socket=socketio) + app.cancelAllDownloads_link(interface=socket_interface) @socketio.on('saveSettings') def saveSettings(settings): app.saveSettings_link(settings) - socketio.emit('updateSettings', settings) + socket_interface.send('updateSettings', settings) # Example code leftover, could be usefull later on @server.route('/choose/path', methods=['POST'])