diff --git a/app.py b/app.py index 271cb15..be5ba03 100644 --- a/app.py +++ b/app.py @@ -1,204 +1,168 @@ #!/usr/bin/env python3 -from deemix.app.queuemanager import addToQueue, removeFromQueue, getQueue, cancelAllDownloads, removeFinishedDownloads, restoreQueue, slimQueueItems, resetQueueItems +from deemix.api.deezer import Deezer +from deemix.app.settings import Settings +from deemix.app.queuemanager import QueueManager +from deemix.app.spotifyhelper import SpotifyHelper, emptyPlaylist as emptySpotifyPlaylist + from deemix.utils.misc import getTypeFromLink, getIDFromLink -from deemix.app.settings import initSettings, getSettings, getDefaultSettings, saveSettings -from deemix.app.spotify import SpotifyHelper from deemix.utils.localpaths import getConfigFolder +#from deemix.app.queuemanager import addToQueue, removeFromQueue, getQueue, cancelAllDownloads, removeFinishedDownloads, restoreQueue, slimQueueItems, resetQueueItems +# +#from deemix.app.settings import initSettings, getSettings, getDefaultSettings, saveSettings +#from deemix.app.spotify import SpotifyHelper +# + import os.path as path import json -from os import remove - -settings = {} -spotifyHelper = None -chartsList = [] -configFolder = "" -def initialize(portable): - global settings - global spotifyHelper - global defaultSettings - global configFolder - if portable: - configFolder = portable - else: - configFolder = getConfigFolder() - settings = initSettings(configFolder=configFolder) - defaultSettings = getDefaultSettings() - spotifyHelper = SpotifyHelper(configFolder=configFolder) +class deemix: + def __init__(self, portable): + self.configFolder = portable + if not portable: + self.configFolder = getConfigFolder() + self.set = Settings(self.configFolder) + self.sp = SpotifyHelper(self.configFolder) + self.qm = QueueManager() + self.chartsList = [] -def shutdown(interface=None): - global configFolder - if settings['saveDownloadQueue']: - (queue, queueComplete, queueList, currentItem) = getQueue() - if len(queueList) > 0: - if currentItem != "": - queue.insert(0, currentItem) - with open(path.join(configFolder, 'queue.json'), 'w') as f: - json.dump({ - 'queue': queue, - 'queueComplete': queueComplete, - 'queueList': resetQueueItems(queueList, queue) - }, f) - cancelAllDownloads(interface) - if interface: - interface.send("toast", {'msg': "Server is closed."}) - -def loadDownloadQueue(dz, interface=None): - global configFolder - if path.isfile(path.join(configFolder, 'queue.json')): + def shutdown(self, interface=None): + if self.set.settings['saveDownloadQueue']: + self.qm.saveQueue(self.configFolder) + self.qm.cancelAllDownloads(interface) if interface: - interface.send('toast', {'msg': "Restoring download queue", 'icon': 'loading', 'dismiss': False, - 'id': 'restoring_queue'}) - with open(path.join(configFolder, 'queue.json'), 'r') as f: - qd = json.load(f) - if interface: - interface.send('init_downloadQueue', - {'queue': qd['queue'], 'queueComplete': qd['queueComplete'], 'queueList': slimQueueItems(qd['queueList'])}) - if interface: - interface.send('toast', {'msg': "Download queue restored!", 'icon': 'done', 'dismiss': True, - 'id': 'restoring_queue'}) - remove(path.join(configFolder, 'queue.json')) - restoreQueue(qd['queue'], qd['queueComplete'], qd['queueList'], dz, interface) + interface.send("toast", {'msg': "Server is closed."}) -def getDownloadFolder(): - global settings - return settings['downloadLocation'] + def restoreDownloadQueue(self, dz, interface=None): + self.qm.loadQueue(dz, self.sp, self.configFolder, self.set.settings, interface) -def get_charts(dz): - global chartsList - if len(chartsList) == 0: - temp = dz.get_charts_countries() - countries = [] - for i in range(len(temp)): - countries.append({ - 'title': temp[i]['title'].replace("Top ", ""), - 'id': temp[i]['id'], - 'picture_small': temp[i]['picture_small'], - 'picture_medium': temp[i]['picture_medium'], - 'picture_big': temp[i]['picture_big'] - }) - chartsList = countries - return chartsList + def get_charts(self, dz): + if len(self.chartsList) == 0: + temp = dz.get_charts_countries() + countries = [] + for i in range(len(temp)): + countries.append({ + 'title': temp[i]['title'].replace("Top ", ""), + 'id': temp[i]['id'], + 'picture_small': temp[i]['picture_small'], + 'picture_medium': temp[i]['picture_medium'], + 'picture_big': temp[i]['picture_big'] + }) + self.chartsList = countries + return self.chartsList -def getUserFavorites(dz): - user_id = dz.user['id'] - result = {} - try: - result['playlists'] = dz.get_user_playlists(user_id)['data'] - result['albums'] = dz.get_user_albums(user_id)['data'] - result['artists'] = dz.get_user_artists(user_id)['data'] - result['tracks'] = dz.get_user_tracks(user_id)['data'] - except: - result['playlists'] = dz.get_user_playlists_gw(user_id) - result['albums'] = dz.get_user_albums_gw(user_id) - result['artists'] = dz.get_user_artists_gw(user_id) - result['tracks'] = dz.get_user_tracks_gw(user_id) - return result + def getDownloadFolder(self): + return self.set.settings['downloadLocation'] -def updateUserSpotifyPlaylists(user): - if user == "" or not spotifyHelper.spotifyEnabled: - return [] - try: - return spotifyHelper.get_user_playlists(user) - except: - return [] + def getUserFavorites(self, dz): + user_id = dz.user['id'] + result = {} + try: + result['playlists'] = dz.get_user_playlists(user_id)['data'] + result['albums'] = dz.get_user_albums(user_id)['data'] + result['artists'] = dz.get_user_artists(user_id)['data'] + result['tracks'] = dz.get_user_tracks(user_id)['data'] + except: + result['playlists'] = dz.get_user_playlists_gw(user_id) + result['albums'] = dz.get_user_albums_gw(user_id) + result['artists'] = dz.get_user_artists_gw(user_id) + result['tracks'] = dz.get_user_tracks_gw(user_id) + return result + + def updateUserSpotifyPlaylists(self, user): + if user == "" or not self.sp.spotifyEnabled: + return [] + try: + return self.sp.get_user_playlists(user) + except: + return [] + + def updateUserPlaylists(self, dz): + user_id = dz.user['id'] + try: + return dz.get_user_playlists(user_id)['data'] + except: + return dz.get_user_playlists_gw(user_id) + + def updateUserAlbums(self, dz): + user_id = dz.user['id'] + try: + return dz.get_user_albums(user_id)['data'] + except: + return dz.get_user_albums_gw(user_id) + + def updateUserArtists(self, dz): + user_id = dz.user['id'] + try: + return dz.get_user_artists(user_id)['data'] + except: + return dz.get_user_artists_gw(user_id) + + def updateUserTracks(self, dz): + user_id = dz.user['id'] + try: + return dz.get_user_tracks(user_id)['data'] + except: + return dz.get_user_tracks_gw(user_id) + + def getSpotifyPlaylistTracklist(self, id): + if id == "" or not self.sp.spotifyEnabled: + return emptySpotifyPlaylist + return self.sp.get_playlist_tracklist(id) + + # Search functions + def mainSearch(self, dz, term): + return dz.search_main_gw(term) + + def search(self, dz, term, type, start, nb): + return dz.search(term, type, nb, start) + + # Queue functions + def addToQueue(self, dz, url, bitrate=None, interface=None): + if ';' in url: + url = url.split(";") + self.qm.addToQueue(dz, self.sp, url, self.set.settings, bitrate, interface) -def updateUserPlaylists(dz): - user_id = dz.user['id'] - try: - return dz.get_user_playlists(user_id)['data'] - except: - return dz.get_user_playlists_gw(user_id) - -def updateUserAlbums(dz): - user_id = dz.user['id'] - try: - return dz.get_user_albums(user_id)['data'] - except: - return dz.get_user_albums_gw(user_id) - -def updateUserArtists(dz): - user_id = dz.user['id'] - try: - return dz.get_user_artists(user_id)['data'] - except: - return dz.get_user_artists_gw(user_id) - -def updateUserTracks(dz): - user_id = dz.user['id'] - try: - return dz.get_user_tracks(user_id)['data'] - except: - return dz.get_user_tracks_gw(user_id) - -def getSpotifyPlaylistTracklist(id): - if id == "" or not spotifyHelper.spotifyEnabled: - return spotifyHelper.emptyPlaylist - return spotifyHelper.get_playlist_tracklist(id) - -# Search functions -def mainSearch(dz, term): - return dz.search_main_gw(term) + def removeFromQueue(self, uuid, interface=None): + self.qm.removeFromQueue(uuid, interface) -def search(dz, term, type, start, nb): - return dz.search(term, type, nb, start) + def cancelAllDownloads(self, interface=None): + self.qm.cancelAllDownloads(interface) -# Queue functions -def addToQueue_link(dz, url, bitrate=None, interface=None): - if ';' in url: - url = url.split(";") - return addToQueue(dz, spotifyHelper, url, settings, bitrate, interface) + def removeFinishedDownloads(self, interface=None): + self.qm.removeFinishedDownloads(interface) -def removeFromQueue_link(uuid, interface=None): - removeFromQueue(uuid, interface) + def initDownloadQueue(self): + (queue, queueComplete, queueList, currentItem) = self.qm.getQueue() + return (queue, queueComplete, queueList, currentItem) + def analyzeLink(self, dz, link): + type = getTypeFromLink(link) + relID = getIDFromLink(link, type) + if type in ["track", "album"]: + data = getattr(dz, 'get_' + type)(relID) + else: + data = {} + return (type, data) -def cancelAllDownloads_link(interface=None): - cancelAllDownloads(interface) + # Settings functions + def getDefaultSettings(self): + return self.set.defaultSettings + def getSettings(self): + return self.set.settings -def removeFinishedDownloads_link(interface=None): - removeFinishedDownloads(interface) + def saveSettings(self, newSettings): + return self.set.saveSettings(newSettings) + def getSpotifyCredentials(self): + return self.sp.getCredentials() -def initDownloadQueue(): - (queue, queueComplete, queueList, currentItem) = getQueue() - return (queue, queueComplete, slimQueueItems(queueList), currentItem) - -def analyzeLink(dz, link): - type = getTypeFromLink(link) - relID = getIDFromLink(link, type) - if type in ["track", "album"]: - data = getattr(dz, 'get_' + type)(relID) - else: - data = {} - return (type, data) - -# Settings functions -def getDefaultSettings_link(): - return defaultSettings - - -def getSettings_link(): - return getSettings() - - -def saveSettings_link(newSettings): - global settings - settings = newSettings - return saveSettings(newSettings) - - -def getSpotifyCredentials(): - return spotifyHelper.getCredentials() - - -def setSpotifyCredentials(newCredentials): - return spotifyHelper.setCredentials(newCredentials) + def setSpotifyCredentials(self, newCredentials): + return self.sp.setCredentials(newCredentials) diff --git a/server.py b/server.py index ddecd83..9d5d3b1 100644 --- a/server.py +++ b/server.py @@ -9,9 +9,9 @@ from flask import Flask, render_template, request, session, redirect, copy_curre from flask_socketio import SocketIO, emit from werkzeug.middleware.proxy_fix import ProxyFix -import app +from app import deemix from deemix.api.deezer import Deezer -from deemix.app.MessageInterface import MessageInterface +from deemix.app.messageinterface import MessageInterface # Workaround for MIME type error in certain Windows installs # https://github.com/pallets/flask/issues/1045#issuecomment-42202749 @@ -24,6 +24,8 @@ mimetypes.add_type('text/javascript', '.js') from engineio.payload import Payload Payload.max_decode_packets = 500 +app = None + class CustomFlask(Flask): jinja_options = Flask.jinja_options.copy() jinja_options.update(dict( @@ -82,7 +84,7 @@ def not_found_handler(e): def closing(): app.shutdown(interface=socket_interface) socketio.stop() - return 'server closed' + return 'Server Closed' serverwide_arl = "--serverwide-arl" in sys.argv if serverwide_arl: @@ -91,9 +93,9 @@ if serverwide_arl: @socketio.on('connect') def on_connect(): session['dz'] = Deezer() - settings = app.getSettings_link() + settings = app.getSettings() spotifyCredentials = app.getSpotifyCredentials() - defaultSettings = app.getDefaultSettings_link() + defaultSettings = app.getDefaultSettings() emit('init_settings', (settings, spotifyCredentials, defaultSettings)) emit('init_autologin') @@ -104,8 +106,13 @@ def on_connect(): login(arl) queue, queueComplete, queueList, currentItem = app.initDownloadQueue() - emit('init_downloadQueue', - {'queue': queue, 'queueComplete': queueComplete, 'queueList': queueList, 'currentItem': currentItem}) + if len(queueList.keys()): + emit('init_downloadQueue',{ + 'queue': queue, + 'queueComplete': queueComplete, + 'queueList': queueList, + 'currentItem': currentItem + }) emit('init_home', session['dz'].get_charts()) emit('init_charts', app.get_charts(session['dz'])) @@ -130,7 +137,7 @@ def login(arl, force=False, child=0): emit('logged_in', {'status': result, 'arl': arl, 'user': session['dz'].user}) if firstConnection and result in [1, 3]: firstConnection = False - app.loadDownloadQueue(session['dz'], socket_interface) + app.restoreDownloadQueue(session['dz'], socket_interface) if result != 0: emit('familyAccounts', session['dz'].childs) emit('init_favorites', app.getUserFavorites(session['dz'])) @@ -169,29 +176,29 @@ def search(data): @socketio.on('addToQueue') def addToQueue(data): - result = app.addToQueue_link(session['dz'], data['url'], data['bitrate'], interface=socket_interface) + result = app.addToQueue(session['dz'], data['url'], data['bitrate'], interface=socket_interface) if result == "Not logged in": emit('loginNeededToDownload') @socketio.on('removeFromQueue') def removeFromQueue(uuid): - app.removeFromQueue_link(uuid, interface=socket_interface) + app.removeFromQueue(uuid, interface=socket_interface) @socketio.on('removeFinishedDownloads') def removeFinishedDownloads(): - app.removeFinishedDownloads_link(interface=socket_interface) + app.removeFinishedDownloads(interface=socket_interface) @socketio.on('cancelAllDownloads') def cancelAllDownloads(): - app.cancelAllDownloads_link(interface=socket_interface) + app.cancelAllDownloads(interface=socket_interface) @socketio.on('saveSettings') def saveSettings(settings, spotifyCredentials, spotifyUser): - app.saveSettings_link(settings) + app.saveSettings(settings) app.setSpotifyCredentials(spotifyCredentials) socketio.emit('updateSettings', (settings, spotifyCredentials)) if spotifyUser != False: @@ -327,7 +334,8 @@ def applogin(): print("Can't open folder selection, you're not running pywebview") def run_server(port, host="127.0.0.1", portable=None): - app.initialize(portable) + global app + app = deemix(portable) print("Starting server at http://" + host + ":" + str(port)) socketio.run(server, host=host, port=port) diff --git a/webui b/webui index 26d68a9..5f24888 160000 --- a/webui +++ b/webui @@ -1 +1 @@ -Subproject commit 26d68a9e41d11df1011ea7f06a870d004a336c51 +Subproject commit 5f2488846bfc6ff758d1749191fd4746a5126405