Started refactoring queuemanager as a class

This commit is contained in:
RemixDev 2020-08-14 21:27:50 +02:00
parent 8afdfc7042
commit c611420bd9
2 changed files with 423 additions and 357 deletions

104
deemix/app/queueitem.py Normal file
View File

@ -0,0 +1,104 @@
#!/usr/bin/env python3
"""
queueItem base structure
title
artist
cover
size
downloaded
failed
errors
progress
type
id
bitrate
uuid: type+id+bitrate
"""
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']
else:
self.title = title
self.artist = artist
self.cover = cover
self.size = size
self.type = type
self.id = id
self.bitrate = bitrate
self.settings = settings
self.downloaded = 0
self.failed = 0
self.errors = []
self.progress = 0
self.uuid = f"{self.type}_{self.id}_{self.bitrate}"
def toDict(self):
queueItem = {
'title': self.title,
'artist': self.artist,
'cover': self.cover,
'size': self.size,
'downloaded': self.downloaded,
'failed': self.failed,
'errors': self.errors,
'progress': self.progress,
'type': self.type,
'id': self.id,
'bitrate': self.bitrate,
'uuid': self.uuid
}
return queueItem
def getResettedItem(self):
item = self.toDict()
item['downloaded'] = 0
item['failed'] = 0
item['progress'] = 0
item['errors'] = []
return item
def getSlimmedItem(self):
light = self.toDict()
propertiesToDelete = ['single', 'collection', '_EXTRA']
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']
else:
super().__init__(id, bitrate, title, artist, cover, 1, type, settings)
self.single = single
def toDict(self):
queueItem = super().toDict()
queueItem['single'] = self.single
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']
else:
super().__init__(id, bitrate, title, artist, cover, size, type, settings)
self.collection = collection
def toDict(self):
queueItem = super().toDict()
queueItem['collection'] = self.collection
return queueItem

View File

@ -3,73 +3,31 @@ from deemix.app.downloader import download
from deemix.utils.misc import getIDFromLink, getTypeFromLink, getBitrateInt
from deemix.api.deezer import APIError
from spotipy.exceptions import SpotifyException
from deemix.app.queueitem import QISingle, QICollection
import logging
import json
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('deemix')
queue = []
queueList = {}
queueComplete = []
currentItem = ""
class QueueManager:
def __init__(self):
self.queue = []
self.queueList = {}
self.queueComplete = []
self.currentItem = ""
"""
queueItem base structure
title
artist
cover
size
downloaded
failed
errors
progress
type
id
bitrate
uuid: type+id+bitrate
if its a single track
single
if its an album/playlist
collection
"""
def resetQueueItems(items, q):
result = {}
for item in items.keys():
result[item] = items[item].copy()
if item in q:
result[item]['downloaded'] = 0
result[item]['failed'] = 0
result[item]['progress'] = 0
result[item]['errors'] = []
return result
def slimQueueItems(items):
result = {}
for item in items.keys():
result[item] = slimQueueItem(items[item])
return result
def slimQueueItem(item):
light = item.copy()
propertiesToDelete = ['single', 'collection', 'unconverted', '_EXTRA']
for property in propertiesToDelete:
if property in light:
del light[property]
return light
def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interface=None):
def generateQueueItem(self, dz, sp, url, settings, bitrate=None, albumAPI=None, interface=None):
forcedBitrate = getBitrateInt(bitrate)
bitrate = forcedBitrate if forcedBitrate else settings['maxBitrate']
type = getTypeFromLink(url)
id = getIDFromLink(url, type)
result = {}
result['link'] = url
if type == None or id == None:
logger.warn("URL not recognized")
result['error'] = "URL not recognized"
result['errid'] = "invalidURL"
return queueError(url, "URL not recognized", "invalidURL")
elif type == "track":
if id.startswith("isrc"):
try:
@ -77,21 +35,18 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
if 'id' in trackAPI and 'title' in trackAPI:
id = trackAPI['id']
else:
result['error'] = "Track ISRC is not available on deezer"
result['errid'] = "ISRCnotOnDeezer"
return result
except APIError as e:
e = json.loads(str(e))
result['error'] = f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}"
return result
return queueError(url, f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}")
try:
trackAPI = dz.get_track_gw(id)
except APIError as e:
e = json.loads(str(e))
result['error'] = "Wrong URL"
message = "Wrong URL"
if "DATA_ERROR" in e:
result['error'] += f": {e['DATA_ERROR']}"
return result
message += f": {e['DATA_ERROR']}"
return queueError(url, message)
if albumAPI:
trackAPI['_EXTRA_ALBUM'] = albumAPI
if settings['createSingleFolder']:
@ -100,31 +55,26 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
trackAPI['FILENAME_TEMPLATE'] = settings['tracknameTemplate']
trackAPI['SINGLE_TRACK'] = True
result['title'] = trackAPI['SNG_TITLE']
title = trackAPI['SNG_TITLE']
if 'VERSION' in trackAPI and trackAPI['VERSION']:
result['title'] += " " + trackAPI['VERSION']
result['artist'] = trackAPI['ART_NAME']
result[
'cover'] = f"https://e-cdns-images.dzcdn.net/images/cover/{trackAPI['ALB_PICTURE']}/75x75-000000-80-0-0.jpg"
result['size'] = 1
result['downloaded'] = 0
result['failed'] = 0
result['errors'] = []
result['progress'] = 0
result['type'] = 'track'
result['id'] = id
result['bitrate'] = bitrate
result['uuid'] = f"{result['type']}_{id}_{bitrate}"
result['settings'] = settings or {}
result['single'] = trackAPI
title += " " + trackAPI['VERSION']
return QISingle(
id,
bitrate,
title,
trackAPI['ART_NAME'],
f"https://e-cdns-images.dzcdn.net/images/cover/{trackAPI['ALB_PICTURE']}/75x75-000000-80-0-0.jpg",
'track',
settings,
trackAPI,
)
elif type == "album":
try:
albumAPI = dz.get_album(id)
except APIError as e:
e = json.loads(str(e))
result['error'] = f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}"
return result
return queueError(url, f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}")
if id.startswith('upc'):
id = albumAPI['id']
albumAPI_gw = dz.get_album_gw(id)
@ -137,30 +87,32 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
if albumAPI['nb_tracks'] == 255:
albumAPI['nb_tracks'] = len(tracksArray)
result['title'] = albumAPI['title']
result['artist'] = albumAPI['artist']['name']
if albumAPI['cover_small'] != None:
result['cover'] = albumAPI['cover_small'][:-24] + '/75x75-000000-80-0-0.jpg'
cover = albumAPI['cover_small'][:-24] + '/75x75-000000-80-0-0.jpg'
else:
result['cover'] = f"https://e-cdns-images.dzcdn.net/images/cover/{albumAPI_gw['ALB_PICTURE']}/75x75-000000-80-0-0.jpg"
result['size'] = albumAPI['nb_tracks']
result['downloaded'] = 0
result['failed'] = 0
result['errors'] = []
result['progress'] = 0
result['type'] = 'album'
result['id'] = id
result['bitrate'] = bitrate
result['uuid'] = f"{result['type']}_{id}_{bitrate}"
result['settings'] = settings or {}
cover = f"https://e-cdns-images.dzcdn.net/images/cover/{albumAPI_gw['ALB_PICTURE']}/75x75-000000-80-0-0.jpg"
totalSize = len(tracksArray)
result['collection'] = []
collection = []
for pos, trackAPI in enumerate(tracksArray, start=1):
trackAPI['_EXTRA_ALBUM'] = albumAPI
trackAPI['POSITION'] = pos
trackAPI['SIZE'] = totalSize
trackAPI['FILENAME_TEMPLATE'] = settings['albumTracknameTemplate']
result['collection'].append(trackAPI)
collection.append(trackAPI)
return return QICollection(
id,
bitrate,
albumAPI['title'],
albumAPI['artist']['name'],
cover,
totalSize,
'album',
settings,
collection,
)
elif type == "playlist":
try:
@ -170,10 +122,10 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
playlistAPI = dz.get_playlist_gw(id)['results']['DATA']
except APIError as e:
e = json.loads(str(e))
result['error'] = "Wrong URL"
message = "Wrong URL"
if "DATA_ERROR" in e:
result['error'] += f": {e['DATA_ERROR']}"
return result
message += f": {e['DATA_ERROR']}"
return queueError(url, message)
newPlaylist = {
'id': playlistAPI['PLAYLIST_ID'],
'title': playlistAPI['TITLE'],
@ -205,28 +157,13 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
playlistAPI = newPlaylist
if not playlistAPI['public'] and playlistAPI['creator']['id'] != str(dz.user['id']):
logger.warn("You can't download others private playlists.")
result['error'] = "You can't download others private playlists."
result['errid'] = "notYourPrivatePlaylist"
return result
return return queueError(url, "You can't download others private playlists.", "notYourPrivatePlaylist")
playlistTracksAPI = dz.get_playlist_tracks_gw(id)
playlistAPI['various_artist'] = dz.get_artist(5080)
result['title'] = playlistAPI['title']
result['artist'] = playlistAPI['creator']['name']
result['cover'] = playlistAPI['picture_small'][:-24] + '/75x75-000000-80-0-0.jpg'
result['size'] = playlistAPI['nb_tracks']
result['downloaded'] = 0
result['failed'] = 0
result['errors'] = []
result['progress'] = 0
result['type'] = 'playlist'
result['id'] = id
result['bitrate'] = bitrate
result['uuid'] = f"{result['type']}_{id}_{bitrate}"
result['settings'] = settings or {}
totalSize = len(playlistTracksAPI)
result['collection'] = []
collection = []
for pos, trackAPI in enumerate(playlistTracksAPI, start=1):
if 'EXPLICIT_TRACK_CONTENT' in trackAPI and 'EXPLICIT_LYRICS_STATUS' in trackAPI['EXPLICIT_TRACK_CONTENT'] and trackAPI['EXPLICIT_TRACK_CONTENT']['EXPLICIT_LYRICS_STATUS'] in [1,4]:
playlistAPI['explicit'] = True
@ -234,51 +171,70 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
trackAPI['POSITION'] = pos
trackAPI['SIZE'] = totalSize
trackAPI['FILENAME_TEMPLATE'] = settings['playlistTracknameTemplate']
result['collection'].append(trackAPI)
collection.append(trackAPI)
if not 'explicit' in playlistAPI:
playlistAPI['explicit'] = False
return return QICollection(
id,
bitrate,
playlistAPI['title'],
playlistAPI['creator']['name'],
playlistAPI['picture_small'][:-24] + '/75x75-000000-80-0-0.jpg',
totalSize,
'playlist',
settings,
collection,
)
elif type == "artist":
try:
artistAPI = dz.get_artist(id)
except APIError as e:
e = json.loads(str(e))
result['error'] = f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}"
return result
return return queueError(url, f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}")
if interface:
interface.send("startAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']})
artistAPITracks = dz.get_artist_albums(id)
albumList = []
for album in artistAPITracks['data']:
albumList.append(generateQueueItem(dz, sp, album['link'], settings, bitrate))
if interface:
interface.send("finishAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']})
return albumList
elif type == "artistdiscography":
try:
artistAPI = dz.get_artist(id)
except APIError as e:
e = json.loads(str(e))
result['error'] = f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}"
return result
return return queueError(url, f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}")
if interface:
interface.send("startAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']})
artistDiscographyAPI = dz.get_artist_discography_gw(id, 100)
albumList = []
for type in artistDiscographyAPI:
if type != 'all':
for album in artistDiscographyAPI[type]:
albumList.append(generateQueueItem(dz, sp, album['link'], settings, bitrate))
if interface:
interface.send("finishAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']})
return albumList
elif type == "artisttop":
try:
artistAPI = dz.get_artist(id)
except APIError as e:
e = json.loads(str(e))
result['error'] = f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}"
return result
return return queueError(url, f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}")
playlistAPI = {
'id': str(artistAPI['id'])+"_top_track",
@ -312,21 +268,8 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
playlistAPI['various_artist'] = dz.get_artist(5080)
playlistAPI['nb_tracks'] = len(artistTopTracksAPI_gw)
result['title'] = playlistAPI['title']
result['artist'] = playlistAPI['creator']['name']
result['cover'] = playlistAPI['picture_small'][:-24] + '/75x75-000000-80-0-0.jpg'
result['size'] = playlistAPI['nb_tracks']
result['downloaded'] = 0
result['failed'] = 0
result['errors'] = []
result['progress'] = 0
result['type'] = 'playlist'
result['id'] = id
result['bitrate'] = bitrate
result['uuid'] = f"{result['type']}_{id}_{bitrate}"
result['settings'] = settings or {}
totalSize = len(artistTopTracksAPI_gw)
result['collection'] = []
collection = []
for pos, trackAPI in enumerate(artistTopTracksAPI_gw, start=1):
if 'EXPLICIT_TRACK_CONTENT' in trackAPI and 'EXPLICIT_LYRICS_STATUS' in trackAPI['EXPLICIT_TRACK_CONTENT'] and trackAPI['EXPLICIT_TRACK_CONTENT']['EXPLICIT_LYRICS_STATUS'] in [1,4]:
playlistAPI['explicit'] = True
@ -334,62 +277,70 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf
trackAPI['POSITION'] = pos
trackAPI['SIZE'] = totalSize
trackAPI['FILENAME_TEMPLATE'] = settings['playlistTracknameTemplate']
result['collection'].append(trackAPI)
collection.append(trackAPI)
if not 'explicit' in playlistAPI:
playlistAPI['explicit'] = False
return return QICollection(
id,
bitrate,
playlistAPI['title'],
playlistAPI['creator']['name'],
playlistAPI['picture_small'][:-24] + '/75x75-000000-80-0-0.jpg',
totalSize,
'playlist',
settings,
collection,
)
elif type == "spotifytrack":
if not sp.spotifyEnabled:
logger.warn("Spotify Features is not setted up correctly.")
result['error'] = "Spotify Features is not setted up correctly."
result['errid'] = "spotifyDisabled"
return result
return queueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled")
try:
track_id = sp.get_trackid_spotify(dz, id, settings['fallbackSearch'])
except SpotifyException as e:
result['error'] = "Wrong URL: "+e.msg[e.msg.find('\n')+2:]
return result
return queueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:])
if track_id != 0:
return generateQueueItem(dz, sp, f'https://www.deezer.com/track/{track_id}', settings, bitrate)
else:
logger.warn("Track not found on deezer!")
result['error'] = "Track not found on deezer!"
result['errid'] = "trackNotOnDeezer"
return queueError(url, "Track not found on deezer!", "trackNotOnDeezer")
elif type == "spotifyalbum":
if not sp.spotifyEnabled:
logger.warn("Spotify Features is not setted up correctly.")
result['error'] = "Spotify Features is not setted up correctly."
result['errid'] = "spotifyDisabled"
return result
return queueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled")
try:
album_id = sp.get_albumid_spotify(dz, id)
except SpotifyException as e:
result['error'] = "Wrong URL: "+e.msg[e.msg.find('\n')+2:]
return result
return queueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:])
if album_id != 0:
return generateQueueItem(dz, sp, f'https://www.deezer.com/album/{album_id}', settings, bitrate)
else:
logger.warn("Album not found on deezer!")
result['error'] = "Album not found on deezer!"
result['errid'] = "albumNotOnDeezer"
return queueError(url, "Album not found on deezer!", "albumNotOnDeezer")
elif type == "spotifyplaylist":
if not sp.spotifyEnabled:
logger.warn("Spotify Features is not setted up correctly.")
result['error'] = "Spotify Features is not setted up correctly."
result['errid'] = "spotifyDisabled"
return result
return queueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled")
try:
playlist = sp.adapt_spotify_playlist(dz, id, settings)
except SpotifyException as e:
result['error'] = "Wrong URL: "+e.msg[e.msg.find('\n')+2:]
return result
playlist['bitrate'] = bitrate
playlist['uuid'] = f"{playlist['type']}_{id}_{bitrate}"
result = playlist
return playlist
except SpotifyException as e:
return queueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:])
else:
logger.warn("URL not supported yet")
result['error'] = "URL not supported yet"
result['errid'] = "unsupportedURL"
return result
return queueError(url, "URL not supported yet", "unsupportedURL")
def addToQueue(dz, sp, url, settings, bitrate=None, interface=None):
@ -527,3 +478,14 @@ def removeFinishedDownloads(interface=None):
queueComplete = []
if interface:
interface.send("removedFinishedDownloads")
class queueError:
def __init__(self, link, message, errid=None):
self.link = link
self.message = message
self.errid = errid
def toList(self):
error = {
'link'
}