mirror of
https://gitlab.com/RemixDev/deemix-gui-pyweb.git
synced 2025-01-15 11:05:16 +00:00
optimized import and other small changes
This commit is contained in:
parent
122bc3f818
commit
a4693bb537
60
app.py
60
app.py
|
@ -1,61 +1,73 @@
|
||||||
import deemix.utils.localpaths as localpaths
|
|
||||||
from deemix.app.queuemanager import addToQueue, removeFromQueue, getQueue, cancelAllDownloads, removeFinishedDownloads
|
from deemix.app.queuemanager import addToQueue, removeFromQueue, getQueue, cancelAllDownloads, removeFinishedDownloads
|
||||||
from deemix.app.settings import initSettings, getSettings, saveSettings
|
from deemix.app.settings import initSettings, getSettings, saveSettings
|
||||||
from deemix.app.spotify import SpotifyHelper
|
from deemix.app.spotify import SpotifyHelper
|
||||||
from os import system as execute
|
|
||||||
|
|
||||||
settings = {}
|
settings = {}
|
||||||
spotifyHelper = None
|
spotifyHelper = None
|
||||||
|
|
||||||
|
|
||||||
def getUser(dz):
|
def getUser(dz):
|
||||||
return dz.user
|
return dz.user
|
||||||
|
|
||||||
|
|
||||||
def initialize():
|
def initialize():
|
||||||
global settings
|
global settings
|
||||||
global spotifyHelper
|
global spotifyHelper
|
||||||
settings = initSettings()
|
settings = initSettings()
|
||||||
spotifyHelper = SpotifyHelper()
|
spotifyHelper = SpotifyHelper()
|
||||||
|
|
||||||
|
|
||||||
def shutdown(interface=None):
|
def shutdown(interface=None):
|
||||||
getQueue()
|
getQueue()
|
||||||
cancelAllDownloads(interface)
|
cancelAllDownloads(interface)
|
||||||
if interface:
|
if interface:
|
||||||
interface.send("toast", {'msg': "Server is closed."})
|
interface.send("toast", {'msg': "Server is closed."})
|
||||||
|
|
||||||
|
|
||||||
# Search functions
|
# Search functions
|
||||||
def mainSearch(dz, term):
|
def mainSearch(dz, term):
|
||||||
return dz.search_main_gw(term)
|
return dz.search_main_gw(term)
|
||||||
|
|
||||||
|
|
||||||
def search(dz, term, type, start, nb):
|
def search(dz, term, type, start, nb):
|
||||||
return dz.search(term, type, nb, start)
|
return dz.search(term, type, nb, start)
|
||||||
|
|
||||||
|
|
||||||
# Queue functions
|
# Queue functions
|
||||||
def addToQueue_link(dz, url, bitrate=None, interface=None):
|
def addToQueue_link(dz, url, bitrate=None, interface=None):
|
||||||
return addToQueue(dz, spotifyHelper, url, settings, bitrate, interface)
|
return addToQueue(dz, spotifyHelper, url, settings, bitrate, interface)
|
||||||
|
|
||||||
|
|
||||||
def removeFromQueue_link(uuid, interface=None):
|
def removeFromQueue_link(uuid, interface=None):
|
||||||
removeFromQueue(uuid, interface)
|
removeFromQueue(uuid, interface)
|
||||||
|
|
||||||
|
|
||||||
def cancelAllDownloads_link(interface=None):
|
def cancelAllDownloads_link(interface=None):
|
||||||
cancelAllDownloads(interface)
|
cancelAllDownloads(interface)
|
||||||
|
|
||||||
|
|
||||||
def removeFinishedDownloads_link(interface=None):
|
def removeFinishedDownloads_link(interface=None):
|
||||||
removeFinishedDownloads(interface)
|
removeFinishedDownloads(interface)
|
||||||
|
|
||||||
|
|
||||||
def getQueue_link():
|
def getQueue_link():
|
||||||
return getQueue()
|
return getQueue()
|
||||||
|
|
||||||
|
|
||||||
# Settings functions
|
# Settings functions
|
||||||
def getSettings_link():
|
def getSettings_link():
|
||||||
return getSettings()
|
return getSettings()
|
||||||
|
|
||||||
|
|
||||||
def saveSettings_link(newSettings):
|
def saveSettings_link(newSettings):
|
||||||
global settings
|
global settings
|
||||||
settings = newSettings
|
settings = newSettings
|
||||||
return saveSettings(newSettings)
|
return saveSettings(newSettings)
|
||||||
|
|
||||||
|
|
||||||
def getSpotifyCredentials():
|
def getSpotifyCredentials():
|
||||||
return spotifyHelper.getCredentials()
|
return spotifyHelper.getCredentials()
|
||||||
|
|
||||||
|
|
||||||
def setSpotifyCredentials(newCredentials):
|
def setSpotifyCredentials(newCredentials):
|
||||||
return spotifyHelper.setCredentials(newCredentials)
|
return spotifyHelper.setCredentials(newCredentials)
|
||||||
|
|
212
server.py
212
server.py
|
@ -1,40 +1,42 @@
|
||||||
import json
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
from flask import Flask, render_template, request, session
|
from flask import Flask, render_template, request, session
|
||||||
from flask_socketio import SocketIO, emit
|
from flask_socketio import SocketIO, emit
|
||||||
import logging
|
|
||||||
|
import app
|
||||||
from deemix.api.deezer import Deezer
|
from deemix.api.deezer import Deezer
|
||||||
from deemix.app.MessageInterface import MessageInterface
|
from deemix.app.MessageInterface import MessageInterface
|
||||||
import app
|
|
||||||
import time
|
|
||||||
import sys
|
|
||||||
|
|
||||||
class CustomFlask(Flask):
|
class CustomFlask(Flask):
|
||||||
jinja_options = Flask.jinja_options.copy()
|
jinja_options = Flask.jinja_options.copy()
|
||||||
jinja_options.update(dict(
|
jinja_options.update(dict(
|
||||||
block_start_string='$$',
|
block_start_string='$$',
|
||||||
block_end_string='$$',
|
block_end_string='$$',
|
||||||
variable_start_string='$',
|
variable_start_string='$',
|
||||||
variable_end_string='$',
|
variable_end_string='$',
|
||||||
comment_start_string='$#',
|
comment_start_string='$#',
|
||||||
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')
|
||||||
server = CustomFlask(__name__, static_folder=gui_dir, template_folder=gui_dir)
|
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):
|
class SocketInterface(MessageInterface):
|
||||||
def send(self, message, value=None):
|
def send(self, message, value=None):
|
||||||
if value:
|
if value:
|
||||||
socketio.emit(message, value)
|
socketio.emit(message, value)
|
||||||
else:
|
else:
|
||||||
socketio.emit(message)
|
socketio.emit(message)
|
||||||
|
|
||||||
|
|
||||||
socket_interface = SocketInterface()
|
socket_interface = SocketInterface()
|
||||||
|
|
||||||
|
@ -44,122 +46,138 @@ server.logger.disabled = True
|
||||||
|
|
||||||
app.initialize()
|
app.initialize()
|
||||||
|
|
||||||
|
|
||||||
@server.route('/')
|
@server.route('/')
|
||||||
def landing():
|
def landing():
|
||||||
return render_template('index.html')
|
return render_template('index.html')
|
||||||
|
|
||||||
|
|
||||||
@server.route('/shutdown')
|
@server.route('/shutdown')
|
||||||
def closing():
|
def closing():
|
||||||
app.shutdown(interface=socket_interface)
|
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'
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('connect')
|
@socketio.on('connect')
|
||||||
def on_connect():
|
def on_connect():
|
||||||
session['dz'] = Deezer()
|
session['dz'] = Deezer()
|
||||||
settings = app.getSettings_link()
|
settings = app.getSettings_link()
|
||||||
spotifyCredentials = app.getSpotifyCredentials()
|
spotifyCredentials = app.getSpotifyCredentials()
|
||||||
emit('init_settings', (settings, spotifyCredentials))
|
emit('init_settings', (settings, spotifyCredentials))
|
||||||
queue, queueComplete, queueList, currentItem = app.getQueue_link()
|
queue, queueComplete, queueList, currentItem = app.getQueue_link()
|
||||||
emit('init_downloadQueue', {'queue': queue, 'queueComplete': queueComplete, 'queueList': queueList, 'currentItem': currentItem})
|
emit('init_downloadQueue',
|
||||||
|
{'queue': queue, 'queueComplete': queueComplete, 'queueList': queueList, 'currentItem': currentItem})
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('login')
|
@socketio.on('login')
|
||||||
def login(arl, force=False):
|
def login(arl, force=False):
|
||||||
emit('toast', {'msg': "Logging in...", 'icon': 'loading', 'dismiss': False, 'id': "login-toast"})
|
emit('toast', {'msg': "Logging in...", 'icon': 'loading', 'dismiss': False, 'id': "login-toast"})
|
||||||
if not session['dz'].logged_in:
|
if not session['dz'].logged_in:
|
||||||
result = session['dz'].login_via_arl(arl)
|
result = session['dz'].login_via_arl(arl)
|
||||||
else:
|
else:
|
||||||
if force:
|
if force:
|
||||||
session['dz'] = Deezer()
|
session['dz'] = Deezer()
|
||||||
result = session['dz'].login_via_arl(arl)
|
result = session['dz'].login_via_arl(arl)
|
||||||
if result == 1:
|
if result == 1:
|
||||||
result = 3
|
result = 3
|
||||||
else:
|
else:
|
||||||
result = 2
|
result = 2
|
||||||
emit('logged_in', {'status': result, 'arl': arl, 'user': app.getUser(session['dz'])})
|
emit('logged_in', {'status': result, 'arl': arl, 'user': app.getUser(session['dz'])})
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('logout')
|
@socketio.on('logout')
|
||||||
def logout():
|
def logout():
|
||||||
status = 0
|
status = 0
|
||||||
if session['dz'].logged_in:
|
if session['dz'].logged_in:
|
||||||
session['dz'] = Deezer()
|
session['dz'] = Deezer()
|
||||||
status = 0
|
status = 0
|
||||||
else:
|
else:
|
||||||
status = 1
|
status = 1
|
||||||
emit('logged_out', status)
|
emit('logged_out', status)
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('mainSearch')
|
@socketio.on('mainSearch')
|
||||||
def mainSearch(data):
|
def mainSearch(data):
|
||||||
emit('mainSearch', app.mainSearch(session['dz'], data['term']))
|
emit('mainSearch', app.mainSearch(session['dz'], data['term']))
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('search')
|
@socketio.on('search')
|
||||||
def search(data):
|
def search(data):
|
||||||
result = app.search(session['dz'], data['term'], data['type'], data['start'], data['nb'])
|
result = app.search(session['dz'], data['term'], data['type'], data['start'], data['nb'])
|
||||||
result['type'] = data['type']
|
result['type'] = data['type']
|
||||||
emit('search', result)
|
emit('search', result)
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('addToQueue')
|
@socketio.on('addToQueue')
|
||||||
def addToQueue(data):
|
def addToQueue(data):
|
||||||
result = app.addToQueue_link(session['dz'], data['url'], data['bitrate'], interface=socket_interface)
|
result = app.addToQueue_link(session['dz'], data['url'], data['bitrate'], 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, interface=socket_interface)
|
app.removeFromQueue_link(uuid, interface=socket_interface)
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('removeFinishedDownloads')
|
@socketio.on('removeFinishedDownloads')
|
||||||
def removeFinishedDownloads():
|
def removeFinishedDownloads():
|
||||||
app.removeFinishedDownloads_link(interface=socket_interface)
|
app.removeFinishedDownloads_link(interface=socket_interface)
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('cancelAllDownloads')
|
@socketio.on('cancelAllDownloads')
|
||||||
def cancelAllDownloads():
|
def cancelAllDownloads():
|
||||||
app.cancelAllDownloads_link(interface=socket_interface)
|
app.cancelAllDownloads_link(interface=socket_interface)
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('saveSettings')
|
@socketio.on('saveSettings')
|
||||||
def saveSettings(settings, spotifyCredentials):
|
def saveSettings(settings, spotifyCredentials):
|
||||||
app.saveSettings_link(settings)
|
app.saveSettings_link(settings)
|
||||||
app.setSpotifyCredentials(spotifyCredentials)
|
app.setSpotifyCredentials(spotifyCredentials)
|
||||||
socketio.emit('updateSettings', (settings, spotifyCredentials))
|
socketio.emit('updateSettings', (settings, spotifyCredentials))
|
||||||
|
|
||||||
|
|
||||||
@socketio.on('getTracklist')
|
@socketio.on('getTracklist')
|
||||||
def getTracklist(data):
|
def getTracklist(data):
|
||||||
if data['type'] == 'artist':
|
if data['type'] == 'artist':
|
||||||
artistAPI = session['dz'].get_artist(data['id'])
|
artistAPI = session['dz'].get_artist(data['id'])
|
||||||
artistAlbumsAPI = session['dz'].get_artist_albums(data['id'])['data']
|
artistAlbumsAPI = session['dz'].get_artist_albums(data['id'])['data']
|
||||||
tracksData = { 'all': []}
|
tracksData = {'all': []}
|
||||||
for release in artistAlbumsAPI:
|
for release in artistAlbumsAPI:
|
||||||
if not release['record_type'] in tracksData:
|
if not release['record_type'] in tracksData:
|
||||||
tracksData[release['record_type']] = []
|
tracksData[release['record_type']] = []
|
||||||
tracksData[release['record_type']].append(release)
|
tracksData[release['record_type']].append(release)
|
||||||
tracksData['all'].append(release)
|
tracksData['all'].append(release)
|
||||||
artistAPI['releases'] = tracksData
|
artistAPI['releases'] = tracksData
|
||||||
emit('show_artist', artistAPI)
|
emit('show_artist', artistAPI)
|
||||||
else:
|
else:
|
||||||
releaseAPI = getattr(session['dz'], 'get_'+data['type'])(data['id'])
|
releaseAPI = getattr(session['dz'], 'get_' + data['type'])(data['id'])
|
||||||
releaseTracksAPI = getattr(session['dz'], 'get_'+data['type']+'_tracks')(data['id'])['data']
|
releaseTracksAPI = getattr(session['dz'], 'get_' + data['type'] + '_tracks')(data['id'])['data']
|
||||||
tracks = []
|
tracks = []
|
||||||
showdiscs = False
|
showdiscs = False
|
||||||
if data['type'] == 'album' and len(releaseTracksAPI) and releaseTracksAPI[-1]['disk_number'] != 1:
|
if data['type'] == 'album' and len(releaseTracksAPI) and releaseTracksAPI[-1]['disk_number'] != 1:
|
||||||
current_disk = 0
|
current_disk = 0
|
||||||
showdiscs = True
|
showdiscs = True
|
||||||
for track in releaseTracksAPI:
|
for track in releaseTracksAPI:
|
||||||
if showdiscs and int(track['disk_number']) != current_disk:
|
if showdiscs and int(track['disk_number']) != current_disk:
|
||||||
current_disk = int(track['disk_number'])
|
current_disk = int(track['disk_number'])
|
||||||
tracks.append({'type': 'disc_separator', 'number': current_disk})
|
tracks.append({'type': 'disc_separator', 'number': current_disk})
|
||||||
track['selected'] = False
|
track['selected'] = False
|
||||||
tracks.append(track)
|
tracks.append(track)
|
||||||
releaseAPI['tracks'] = tracks
|
releaseAPI['tracks'] = tracks
|
||||||
emit('show_'+data['type'], releaseAPI)
|
emit('show_' + data['type'], releaseAPI)
|
||||||
|
|
||||||
|
|
||||||
def run_server(port):
|
def run_server(port):
|
||||||
print("Starting server at http://127.0.0.1:"+str(port))
|
print("Starting server at http://127.0.0.1:" + str(port))
|
||||||
socketio.run(server, host='0.0.0.0', port=port)
|
socketio.run(server, host='0.0.0.0', port=port)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if len(sys.argv) >= 2:
|
if len(sys.argv) >= 2:
|
||||||
port = int(sys.argv[1])
|
port = int(sys.argv[1])
|
||||||
else:
|
else:
|
||||||
port = 33333
|
port = 33333
|
||||||
run_server(port)
|
run_server(port)
|
||||||
|
|
Loading…
Reference in a new issue