Moved features from pyweb to the library

Also fixed some errors
This commit is contained in:
RemixDev 2020-08-16 10:41:18 +02:00
parent 7088bff58d
commit d4bcc728f4
3 changed files with 95 additions and 37 deletions

View File

@ -107,13 +107,13 @@ class DownloadJob:
if isinstance(self.queueItem, QISingle): if isinstance(self.queueItem, QISingle):
result = self.downloadWrapper(self.queueItem.single) result = self.downloadWrapper(self.queueItem.single)
if result: if result:
singleAfterDownload(result) self.singleAfterDownload(result)
elif isinstance(self.queueItem, QICollection): elif isinstance(self.queueItem, QICollection):
tracks = [None] * len(self.queueItem.collection) tracks = [None] * len(self.queueItem.collection)
with ThreadPoolExecutor(self.settings['queueConcurrency']) as executor: with ThreadPoolExecutor(self.settings['queueConcurrency']) as executor:
for pos, track in enumerate(self.queueItem.collection, start=0): for pos, track in enumerate(self.queueItem.collection, start=0):
tracks[pos] = executor.submit(self.downloadWrapper, track) tracks[pos] = executor.submit(self.downloadWrapper, track)
download_path = collectionAfterDownload(tracks) self.collectionAfterDownload(tracks)
if self.interface: if self.interface:
if self.queueItem.cancel: if self.queueItem.cancel:
self.interface.send('currentItemCancelled', self.queueItem.uuid) self.interface.send('currentItemCancelled', self.queueItem.uuid)
@ -486,7 +486,7 @@ class DownloadJob:
logger.info(f"[{track.mainArtist['name']} - {track.title}] Track download completed") logger.info(f"[{track.mainArtist['name']} - {track.title}] Track download completed")
self.queueItem.downloaded += 1 self.queueItem.downloaded += 1
if self.interface: if self.interface:
self.interface.send("updateQueue", {'uuid': queueItem.uuid, 'downloaded': True, 'downloadPath': writepath}) self.interface.send("updateQueue", {'uuid': self.queueItem.uuid, 'downloaded': True, 'downloadPath': writepath})
return result return result
def getPreferredBitrate(self, track): def getPreferredBitrate(self, track):

View File

@ -1,16 +1,22 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
class QueueItem: class QueueItem:
def __init__(self, id=None, bitrate=None, title=None, artist=None, cover=None, size=None, type=None, settings=None, queueItemList=None): def __init__(self, id=None, bitrate=None, title=None, artist=None, cover=None, size=None, type=None, settings=None, queueItemDict=None):
if queueItemList: if queueItemDict:
self.title = queueItemList['title'] self.title = queueItemDict['title']
self.artist = queueItemList['artist'] self.artist = queueItemDict['artist']
self.cover = queueItemList['cover'] self.cover = queueItemDict['cover']
self.size = queueItemList['size'] self.size = queueItemDict['size']
self.type = queueItemList['type'] self.type = queueItemDict['type']
self.id = queueItemList['id'] self.id = queueItemDict['id']
self.bitrate = queueItemList['bitrate'] self.bitrate = queueItemDict['bitrate']
self.settings = queueItemList['settings'] self.downloaded = queueItemDict['downloaded']
self.failed = queueItemDict['failed']
self.errors = queueItemDict['errors']
self.progress = queueItemDict['progress']
self.settings = None
if 'settings' in queueItemDict:
self.settings = queueItemDict['settings']
else: else:
self.title = title self.title = title
self.artist = artist self.artist = artist
@ -20,10 +26,10 @@ class QueueItem:
self.id = id self.id = id
self.bitrate = bitrate self.bitrate = bitrate
self.settings = settings self.settings = settings
self.downloaded = 0 self.downloaded = 0
self.failed = 0 self.failed = 0
self.errors = [] self.errors = []
self.progress = 0 self.progress = 0
self.uuid = f"{self.type}_{self.id}_{self.bitrate}" self.uuid = f"{self.type}_{self.id}_{self.bitrate}"
self.cancel = False self.cancel = False
@ -53,17 +59,17 @@ class QueueItem:
def getSlimmedItem(self): def getSlimmedItem(self):
light = self.toDict() light = self.toDict()
propertiesToDelete = ['single', 'collection', '_EXTRA'] propertiesToDelete = ['single', 'collection', '_EXTRA', 'settings']
for property in propertiesToDelete: for property in propertiesToDelete:
if property in light: if property in light:
del light[property] del light[property]
return light return light
class QISingle(QueueItem): class QISingle(QueueItem):
def __init__(self, id=None, bitrate=None, title=None, artist=None, cover=None, type=None, settings=None, single=None, queueItemList=None): def __init__(self, id=None, bitrate=None, title=None, artist=None, cover=None, type=None, settings=None, single=None, queueItemDict=None):
if queueItemList: if queueItemDict:
super().__init__(queueItemList=queueItemList) super().__init__(queueItemDict=queueItemDict)
self.single = queueItemList['single'] self.single = queueItemDict['single']
else: else:
super().__init__(id, bitrate, title, artist, cover, 1, type, settings) super().__init__(id, bitrate, title, artist, cover, 1, type, settings)
self.single = single self.single = single
@ -74,10 +80,10 @@ class QISingle(QueueItem):
return queueItem return queueItem
class QICollection(QueueItem): class QICollection(QueueItem):
def __init__(self, id=None, bitrate=None, title=None, artist=None, cover=None, size=None, type=None, settings=None, collection=None, queueItemList=None): def __init__(self, id=None, bitrate=None, title=None, artist=None, cover=None, size=None, type=None, settings=None, collection=None, queueItemDict=None):
if queueItemList: if queueItemDict:
super().__init__(queueItemList=queueItemList) super().__init__(queueItemDict=queueItemDict)
self.collection = queueItemList['collection'] self.collection = queueItemDict['collection']
else: else:
super().__init__(id, bitrate, title, artist, cover, size, type, settings) super().__init__(id, bitrate, title, artist, cover, size, type, settings)
self.collection = collection self.collection = collection
@ -88,10 +94,10 @@ class QICollection(QueueItem):
return queueItem return queueItem
class QIConvertable(QICollection): class QIConvertable(QICollection):
def __init__(self, id=None, bitrate=None, title=None, artist=None, cover=None, size=None, type=None, settings=None, extra=None, queueItemList=None): def __init__(self, id=None, bitrate=None, title=None, artist=None, cover=None, size=None, type=None, settings=None, extra=None, queueItemDict=None):
if queueItemList: if queueItemDict:
super().__init__(queueItemList=queueItemList) super().__init__(queueItemDict=queueItemDict)
self.extra = queueItemList['_EXTRA'] self.extra = queueItemDict['_EXTRA']
else: else:
super().__init__(id, bitrate, title, artist, cover, size, type, settings, []) super().__init__(id, bitrate, title, artist, cover, size, type, settings, [])
self.extra = extra self.extra = extra

View File

@ -5,7 +5,9 @@ from deemix.api.deezer import APIError
from spotipy.exceptions import SpotifyException from spotipy.exceptions import SpotifyException
from deemix.app.queueitem import QISingle, QICollection from deemix.app.queueitem import QISingle, QICollection
import logging import logging
import os.path as path
import json import json
from os import remove
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('deemix') logger = logging.getLogger('deemix')
@ -387,7 +389,7 @@ class QueueManager:
if interface: if interface:
interface.send("startDownload", self.currentItem) interface.send("startDownload", self.currentItem)
logger.info(f"[{self.currentItem}] Started downloading.") logger.info(f"[{self.currentItem}] Started downloading.")
DownloadJob(dz, sp, self.queueList[self.currentItem]).start() DownloadJob(dz, sp, self.queueList[self.currentItem], interface).start()
self.afterDownload(dz, sp, interface) self.afterDownload(dz, sp, interface)
def afterDownload(self, dz, sp, interface): def afterDownload(self, dz, sp, interface):
@ -401,14 +403,64 @@ class QueueManager:
def getQueue(self): def getQueue(self):
return (self.queue, self.queueComplete, self.queueList, self.currentItem) return (self.queue, self.queueComplete, self.slimQueueList(), self.currentItem)
# TODO: Convert dicts to QueueItem Objects when restoring def saveQueue(self, configFolder):
def restoreQueue(self, queue, queueComplete, queueList, dz, sp, interface): if len(self.queueList) > 0:
self.queueComplete = queueComplete if self.currentItem != "":
self.queueList = queueList self.queue.insert(0, self.currentItem)
with open(path.join(configFolder, 'queue.json'), 'w') as f:
json.dump({
'queue': self.queue,
'queueComplete': self.queueComplete,
'queueList': self.exportQueueList()
}, f)
def exportQueueList(self):
queueList = {}
for uuid in self.queueList:
if uuid in self.queue:
queueList[uuid] = self.queueList[uuid].getResettedItem()
else:
queueList[uuid] = self.queueList[uuid].toDict()
print(self.queueList[uuid].progress)
return queueList
def slimQueueList(self):
queueList = {}
for uuid in self.queueList:
queueList[uuid] = self.queueList[uuid].getSlimmedItem()
return queueList
def loadQueue(self, dz, sp, configFolder, settings, interface=None):
if path.isfile(path.join(configFolder, 'queue.json')) and not len(self.queue):
if interface:
interface.send('restoringQueue')
with open(path.join(configFolder, 'queue.json'), 'r') as f:
qd = json.load(f)
remove(path.join(configFolder, 'queue.json'))
self.restoreQueue(qd['queue'], qd['queueComplete'], qd['queueList'], settings, dz, sp, interface)
if interface:
interface.send('init_downloadQueue', {
'queue': self.queue,
'queueComplete': self.queueComplete,
'queueList': self.slimQueueList(),
'restored': True
})
self.nextItem(dz, sp, interface)
def restoreQueue(self, queue, queueComplete, queueList, settings, dz, sp, interface=None):
self.queue = queue self.queue = queue
nextItem(dz, sp, interface) self.queueComplete = queueComplete
self.queueList = {}
for uuid in queueList:
if 'single' in queueList[uuid]:
self.queueList[uuid] = QISingle(queueItemDict = queueList[uuid])
if 'collection' in queueList[uuid]:
self.queueList[uuid] = QICollection(queueItemDict = queueList[uuid])
if '_EXTRA' in queueList[uuid]:
self.queueList[uuid] = QIConvertable(queueItemDict = queueList[uuid])
self.queueList[uuid].settings = settings
def removeFromQueue(self, uuid, interface=None): def removeFromQueue(self, uuid, interface=None):
if uuid == self.currentItem: if uuid == self.currentItem: