From 44f5ccccc13338c1fc415d6c43ef1daa85a562e2 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Sun, 17 May 2020 21:26:49 +0200 Subject: [PATCH] Added overwriteFile and saveDownloadQueue options --- app.py | 39 +++++++++++++++++++++++++++++++++++---- server.py | 8 +++++++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 4e802a6..58dc103 100644 --- a/app.py +++ b/app.py @@ -1,8 +1,13 @@ #!/usr/bin/env python3 -from deemix.app.queuemanager import addToQueue, removeFromQueue, getQueue, cancelAllDownloads, removeFinishedDownloads +from deemix.app.queuemanager import addToQueue, removeFromQueue, getQueue, cancelAllDownloads, removeFinishedDownloads, restoreQueue, slimQueueItems, resetQueueItems 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 + +import os.path as path +import json +from os import remove settings = {} spotifyHelper = None @@ -19,11 +24,36 @@ def initialize(): def shutdown(interface=None): - getQueue() + if settings['saveDownloadQueue']: + (queue, queueComplete, queueList, currentItem) = getQueue() + if len(queueList) > 0: + queue.insert(0, currentItem) + with open(path.join(getConfigFolder(), '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): + if path.isfile(path.join(getConfigFolder(), 'queue.json')): + if interface: + interface.send('toast', {'msg': "Restoring download queue", 'icon': 'loading', 'dismiss': False, + 'id': 'restoring_queue'}) + with open(path.join(getConfigFolder(), '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(getConfigFolder(), 'queue.json')) + restoreQueue(qd['queue'], qd['queueComplete'], qd['queueList'], dz, interface) + def get_charts(dz): global chartsList if len(chartsList) == 0: @@ -120,8 +150,9 @@ def removeFinishedDownloads_link(interface=None): removeFinishedDownloads(interface) -def getQueue_link(): - return getQueue() +def initDownloadQueue(): + (queue, queueComplete, queueList, currentItem) = getQueue() + return (queue, queueComplete, slimQueueItems(queueList), currentItem) def analyzeLink(dz, link): type = getTypeFromLink(link) diff --git a/server.py b/server.py index c425bed..1778d82 100644 --- a/server.py +++ b/server.py @@ -60,6 +60,7 @@ logging.getLogger('engineio').setLevel(logging.ERROR) #server.logger.disabled = True app.initialize() +firstConnection = True @server.route('/') @@ -92,7 +93,7 @@ def on_connect(): arl = file.readline().rstrip("\n") login(arl) - queue, queueComplete, queueList, currentItem = app.getQueue_link() + queue, queueComplete, queueList, currentItem = app.initDownloadQueue() emit('init_downloadQueue', {'queue': queue, 'queueComplete': queueComplete, 'queueList': queueList, 'currentItem': currentItem}) emit('init_home', session['dz'].get_charts()) @@ -101,6 +102,7 @@ def on_connect(): @socketio.on('login') def login(arl, force=False): + global firstConnection emit('toast', {'msg': "Logging in...", 'icon': 'loading', 'dismiss': False, 'id': "login-toast"}) if not session['dz'].logged_in: result = session['dz'].login_via_arl(arl) @@ -114,6 +116,10 @@ def login(arl, force=False): result = 2 emit('logged_in', {'status': result, 'arl': arl, 'user': session['dz'].user}) emit('init_favorites', app.getUserFavorites(session['dz'])) + if firstConnection and result in [1, 3]: + firstConnection = False + app.loadDownloadQueue(session['dz'], socket_interface) + @socketio.on('logout')