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

View File

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

View File

@ -5,7 +5,9 @@ from deemix.api.deezer import APIError
from spotipy.exceptions import SpotifyException
from deemix.app.queueitem import QISingle, QICollection
import logging
import os.path as path
import json
from os import remove
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('deemix')
@ -387,7 +389,7 @@ class QueueManager:
if interface:
interface.send("startDownload", self.currentItem)
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)
def afterDownload(self, dz, sp, interface):
@ -401,14 +403,64 @@ class QueueManager:
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 restoreQueue(self, queue, queueComplete, queueList, dz, sp, interface):
self.queueComplete = queueComplete
self.queueList = queueList
def saveQueue(self, configFolder):
if len(self.queueList) > 0:
if self.currentItem != "":
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
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):
if uuid == self.currentItem: