2020-05-04 16:23:09 +00:00
|
|
|
#!/usr/bin/env python3
|
2020-04-10 14:12:21 +00:00
|
|
|
from deemix.app.downloader import download
|
2020-04-17 10:31:47 +00:00
|
|
|
from deemix.utils.misc import getIDFromLink, getTypeFromLink, getBitrateInt
|
2020-06-26 10:20:48 +00:00
|
|
|
from deemix.api.deezer import APIError
|
2020-06-29 11:58:06 +00:00
|
|
|
from spotipy.exceptions import SpotifyException
|
2020-05-08 16:06:43 +00:00
|
|
|
import logging
|
2020-06-26 10:20:48 +00:00
|
|
|
import json
|
2020-05-08 16:06:43 +00:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
logger = logging.getLogger('deemix')
|
2020-04-10 14:12:21 +00:00
|
|
|
|
|
|
|
queue = []
|
|
|
|
queueList = {}
|
2020-04-14 14:48:13 +00:00
|
|
|
queueComplete = []
|
2020-04-10 14:12:21 +00:00
|
|
|
currentItem = ""
|
|
|
|
|
|
|
|
"""
|
|
|
|
queueItem base structure
|
|
|
|
title
|
|
|
|
artist
|
|
|
|
cover
|
|
|
|
size
|
|
|
|
downloaded
|
|
|
|
failed
|
2020-05-25 09:58:48 +00:00
|
|
|
errors
|
2020-04-10 14:12:21 +00:00
|
|
|
progress
|
|
|
|
type
|
|
|
|
id
|
|
|
|
bitrate
|
|
|
|
uuid: type+id+bitrate
|
|
|
|
if its a single track
|
|
|
|
single
|
|
|
|
if its an album/playlist
|
|
|
|
collection
|
|
|
|
"""
|
|
|
|
|
2020-05-17 19:26:49 +00:00
|
|
|
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
|
2020-05-25 09:58:48 +00:00
|
|
|
result[item]['errors'] = []
|
2020-05-17 19:26:49 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
def slimQueueItems(items):
|
|
|
|
result = {}
|
|
|
|
for item in items.keys():
|
|
|
|
result[item] = slimQueueItem(items[item])
|
|
|
|
return result
|
|
|
|
|
2020-05-06 10:06:56 +00:00
|
|
|
def slimQueueItem(item):
|
|
|
|
light = item.copy()
|
|
|
|
if 'single' in light:
|
|
|
|
del light['single']
|
|
|
|
if 'collection' in light:
|
|
|
|
del light['collection']
|
|
|
|
return light
|
2020-04-17 10:31:47 +00:00
|
|
|
|
2020-04-16 08:37:02 +00:00
|
|
|
def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interface=None):
|
2020-04-17 10:31:47 +00:00
|
|
|
forcedBitrate = getBitrateInt(bitrate)
|
|
|
|
bitrate = forcedBitrate if forcedBitrate else settings['maxBitrate']
|
|
|
|
type = getTypeFromLink(url)
|
|
|
|
id = getIDFromLink(url, type)
|
|
|
|
result = {}
|
2020-06-19 18:14:20 +00:00
|
|
|
result['link'] = url
|
2020-04-17 10:31:47 +00:00
|
|
|
if type == None or id == None:
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.warn("URL not recognized")
|
2020-04-17 10:31:47 +00:00
|
|
|
result['error'] = "URL not recognized"
|
|
|
|
elif type == "track":
|
2020-06-26 10:20:48 +00:00
|
|
|
if id.startswith("isrc"):
|
|
|
|
try:
|
|
|
|
trackAPI = dz.get_track(id)
|
2020-07-23 13:10:18 +00:00
|
|
|
if 'id' in trackAPI and 'title' in trackAPI:
|
2020-06-26 10:20:48 +00:00
|
|
|
id = trackAPI['id']
|
|
|
|
else:
|
|
|
|
result['error'] = "Track ISRC is not available on deezer"
|
|
|
|
return result
|
|
|
|
except APIError as e:
|
|
|
|
e = json.loads(str(e))
|
2020-06-29 11:58:06 +00:00
|
|
|
result['error'] = f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}"
|
2020-06-26 10:20:48 +00:00
|
|
|
return result
|
2020-06-29 11:58:06 +00:00
|
|
|
try:
|
|
|
|
trackAPI = dz.get_track_gw(id)
|
|
|
|
except APIError as e:
|
|
|
|
e = json.loads(str(e))
|
|
|
|
result['error'] = "Wrong URL"
|
|
|
|
if "DATA_ERROR" in e:
|
|
|
|
result['error'] += f": {e['DATA_ERROR']}"
|
|
|
|
return result
|
2020-04-17 10:31:47 +00:00
|
|
|
if albumAPI:
|
|
|
|
trackAPI['_EXTRA_ALBUM'] = albumAPI
|
2020-05-13 18:40:08 +00:00
|
|
|
if settings['createSingleFolder']:
|
|
|
|
trackAPI['FILENAME_TEMPLATE'] = settings['albumTracknameTemplate']
|
|
|
|
else:
|
|
|
|
trackAPI['FILENAME_TEMPLATE'] = settings['tracknameTemplate']
|
2020-04-17 10:31:47 +00:00
|
|
|
trackAPI['SINGLE_TRACK'] = True
|
2020-04-10 14:12:21 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
result['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
|
2020-05-25 09:58:48 +00:00
|
|
|
result['errors'] = []
|
2020-04-17 10:31:47 +00:00
|
|
|
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
|
2020-04-10 14:12:21 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
elif type == "album":
|
2020-06-26 10:20:48 +00:00
|
|
|
try:
|
|
|
|
albumAPI = dz.get_album(id)
|
|
|
|
except APIError as e:
|
|
|
|
e = json.loads(str(e))
|
2020-06-29 11:58:06 +00:00
|
|
|
result['error'] = f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}"
|
2020-06-26 10:20:48 +00:00
|
|
|
return result
|
|
|
|
if id.startswith('upc'):
|
|
|
|
id = albumAPI['id']
|
2020-04-17 10:31:47 +00:00
|
|
|
albumAPI_gw = dz.get_album_gw(id)
|
|
|
|
albumAPI['nb_disk'] = albumAPI_gw['NUMBER_DISK']
|
|
|
|
albumAPI['copyright'] = albumAPI_gw['COPYRIGHT']
|
|
|
|
if albumAPI['nb_tracks'] == 1:
|
|
|
|
return generateQueueItem(dz, sp, f"https://www.deezer.com/track/{albumAPI['tracks']['data'][0]['id']}",
|
|
|
|
settings, bitrate, albumAPI)
|
|
|
|
tracksArray = dz.get_album_tracks_gw(id)
|
2020-07-13 10:41:40 +00:00
|
|
|
if albumAPI['nb_tracks'] == 255:
|
|
|
|
albumAPI['nb_tracks'] = len(tracksArray)
|
2020-04-10 14:12:21 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
result['title'] = albumAPI['title']
|
|
|
|
result['artist'] = albumAPI['artist']['name']
|
2020-04-23 12:12:27 +00:00
|
|
|
if albumAPI['cover_small'] != None:
|
|
|
|
result['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"
|
2020-04-17 10:31:47 +00:00
|
|
|
result['size'] = albumAPI['nb_tracks']
|
|
|
|
result['downloaded'] = 0
|
|
|
|
result['failed'] = 0
|
2020-05-25 09:58:48 +00:00
|
|
|
result['errors'] = []
|
2020-04-17 10:31:47 +00:00
|
|
|
result['progress'] = 0
|
|
|
|
result['type'] = 'album'
|
|
|
|
result['id'] = id
|
|
|
|
result['bitrate'] = bitrate
|
|
|
|
result['uuid'] = f"{result['type']}_{id}_{bitrate}"
|
|
|
|
result['settings'] = settings or {}
|
|
|
|
totalSize = len(tracksArray)
|
|
|
|
result['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)
|
2020-04-10 14:12:21 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
elif type == "playlist":
|
2020-05-05 17:45:35 +00:00
|
|
|
try:
|
|
|
|
playlistAPI = dz.get_playlist(id)
|
|
|
|
except:
|
2020-06-29 11:58:06 +00:00
|
|
|
try:
|
|
|
|
playlistAPI = dz.get_playlist_gw(id)['results']['DATA']
|
|
|
|
except APIError as e:
|
|
|
|
e = json.loads(str(e))
|
|
|
|
result['error'] = "Wrong URL"
|
|
|
|
if "DATA_ERROR" in e:
|
|
|
|
result['error'] += f": {e['DATA_ERROR']}"
|
|
|
|
return result
|
2020-05-05 17:45:35 +00:00
|
|
|
newPlaylist = {
|
|
|
|
'id': playlistAPI['PLAYLIST_ID'],
|
|
|
|
'title': playlistAPI['TITLE'],
|
|
|
|
'description': playlistAPI['DESCRIPTION'],
|
|
|
|
'duration': playlistAPI['DURATION'],
|
|
|
|
'public': False,
|
|
|
|
'is_loved_track': False,
|
|
|
|
'collaborative': False,
|
|
|
|
'nb_tracks': playlistAPI['NB_SONG'],
|
|
|
|
'fans': playlistAPI['NB_FAN'],
|
|
|
|
'link': "https://www.deezer.com/playlist/"+playlistAPI['PLAYLIST_ID'],
|
|
|
|
'share': None,
|
|
|
|
'picture': "https://api.deezer.com/playlist/"+playlistAPI['PLAYLIST_ID']+"/image",
|
|
|
|
'picture_small': "https://cdns-images.dzcdn.net/images/"+playlistAPI['PICTURE_TYPE']+"/"+playlistAPI['PLAYLIST_PICTURE']+"/56x56-000000-80-0-0.jpg",
|
|
|
|
'picture_medium': "https://cdns-images.dzcdn.net/images/"+playlistAPI['PICTURE_TYPE']+"/"+playlistAPI['PLAYLIST_PICTURE']+"/250x250-000000-80-0-0.jpg",
|
|
|
|
'picture_big': "https://cdns-images.dzcdn.net/images/"+playlistAPI['PICTURE_TYPE']+"/"+playlistAPI['PLAYLIST_PICTURE']+"/500x500-000000-80-0-0.jpg",
|
|
|
|
'picture_xl': "https://cdns-images.dzcdn.net/images/"+playlistAPI['PICTURE_TYPE']+"/"+playlistAPI['PLAYLIST_PICTURE']+"/1000x1000-000000-80-0-0.jpg",
|
|
|
|
'checksum': playlistAPI['CHECKSUM'],
|
|
|
|
'tracklist': "https://api.deezer.com/playlist/"+playlistAPI['PLAYLIST_ID']+"/tracks",
|
|
|
|
'creation_date': playlistAPI['DATE_ADD'],
|
|
|
|
'creator': {
|
|
|
|
'id': playlistAPI['PARENT_USER_ID'],
|
|
|
|
'name': playlistAPI['PARENT_USERNAME'],
|
|
|
|
'tracklist': "https://api.deezer.com/user/"+playlistAPI['PARENT_USER_ID']+"/flow",
|
|
|
|
'type': "user"
|
|
|
|
},
|
|
|
|
'type': "playlist"
|
|
|
|
}
|
|
|
|
playlistAPI = newPlaylist
|
|
|
|
if not playlistAPI['public'] and playlistAPI['creator']['id'] != str(dz.user['id']):
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.warn("You can't download others private playlists.")
|
2020-05-05 17:45:35 +00:00
|
|
|
result['error'] = "You can't download others private playlists."
|
|
|
|
return result
|
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
playlistTracksAPI = dz.get_playlist_tracks_gw(id)
|
|
|
|
playlistAPI['various_artist'] = dz.get_artist(5080)
|
2020-04-10 14:12:21 +00:00
|
|
|
|
2020-04-17 10:31:47 +00:00
|
|
|
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
|
2020-05-25 09:58:48 +00:00
|
|
|
result['errors'] = []
|
2020-04-17 10:31:47 +00:00
|
|
|
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'] = []
|
|
|
|
for pos, trackAPI in enumerate(playlistTracksAPI, start=1):
|
2020-06-22 22:27:43 +00:00
|
|
|
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
|
2020-04-17 10:31:47 +00:00
|
|
|
trackAPI['_EXTRA_PLAYLIST'] = playlistAPI
|
|
|
|
trackAPI['POSITION'] = pos
|
|
|
|
trackAPI['SIZE'] = totalSize
|
|
|
|
trackAPI['FILENAME_TEMPLATE'] = settings['playlistTracknameTemplate']
|
|
|
|
result['collection'].append(trackAPI)
|
2020-06-22 22:27:43 +00:00
|
|
|
if not 'explicit' in playlistAPI:
|
|
|
|
playlistAPI['explicit'] = False
|
2020-04-17 10:31:47 +00:00
|
|
|
|
|
|
|
elif type == "artist":
|
2020-06-26 10:20:48 +00:00
|
|
|
try:
|
|
|
|
albumAPI = artistAPI = dz.get_artist(id)
|
|
|
|
except APIError as e:
|
|
|
|
e = json.loads(str(e))
|
2020-06-29 11:58:06 +00:00
|
|
|
result['error'] = f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}"
|
2020-06-26 10:20:48 +00:00
|
|
|
return result
|
2020-04-17 10:31:47 +00:00
|
|
|
if interface:
|
2020-06-05 08:47:54 +00:00
|
|
|
interface.send("startAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']})
|
2020-04-17 10:31:47 +00:00
|
|
|
artistAPITracks = dz.get_artist_albums(id)
|
|
|
|
albumList = []
|
|
|
|
for album in artistAPITracks['data']:
|
|
|
|
albumList.append(generateQueueItem(dz, sp, album['link'], settings, bitrate))
|
|
|
|
if interface:
|
2020-06-05 08:47:54 +00:00
|
|
|
interface.send("finishAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']})
|
2020-04-17 10:31:47 +00:00
|
|
|
return albumList
|
|
|
|
elif type == "spotifytrack":
|
|
|
|
if not sp.spotifyEnabled:
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.warn("Spotify Features is not setted up correctly.")
|
2020-04-17 10:31:47 +00:00
|
|
|
result['error'] = "Spotify Features is not setted up correctly."
|
|
|
|
return result
|
2020-06-29 11:58:06 +00:00
|
|
|
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
|
2020-04-17 10:31:47 +00:00
|
|
|
if track_id != 0:
|
|
|
|
return generateQueueItem(dz, sp, f'https://www.deezer.com/track/{track_id}', settings, bitrate)
|
|
|
|
else:
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.warn("Track not found on deezer!")
|
2020-04-17 10:31:47 +00:00
|
|
|
result['error'] = "Track not found on deezer!"
|
|
|
|
elif type == "spotifyalbum":
|
|
|
|
if not sp.spotifyEnabled:
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.warn("Spotify Features is not setted up correctly.")
|
2020-04-17 10:31:47 +00:00
|
|
|
result['error'] = "Spotify Features is not setted up correctly."
|
|
|
|
return result
|
2020-06-29 11:58:06 +00:00
|
|
|
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
|
2020-04-17 10:31:47 +00:00
|
|
|
if album_id != 0:
|
|
|
|
return generateQueueItem(dz, sp, f'https://www.deezer.com/album/{album_id}', settings, bitrate)
|
|
|
|
else:
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.warn("Album not found on deezer!")
|
2020-04-17 10:31:47 +00:00
|
|
|
result['error'] = "Album not found on deezer!"
|
|
|
|
elif type == "spotifyplaylist":
|
|
|
|
if not sp.spotifyEnabled:
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.warn("Spotify Features is not setted up correctly.")
|
2020-04-17 10:31:47 +00:00
|
|
|
result['error'] = "Spotify Features is not setted up correctly."
|
|
|
|
return result
|
|
|
|
if interface:
|
2020-06-05 08:47:54 +00:00
|
|
|
interface.send("startConvertingSpotifyPlaylist", str(id))
|
2020-06-29 11:58:06 +00:00
|
|
|
try:
|
|
|
|
playlist = sp.convert_spotify_playlist(dz, id, settings)
|
|
|
|
except SpotifyException as e:
|
|
|
|
result['error'] = "Wrong URL: "+e.msg[e.msg.find('\n')+2:]
|
|
|
|
return result
|
2020-04-17 10:31:47 +00:00
|
|
|
playlist['bitrate'] = bitrate
|
|
|
|
playlist['uuid'] = f"{playlist['type']}_{id}_{bitrate}"
|
|
|
|
result = playlist
|
|
|
|
if interface:
|
2020-06-05 08:47:54 +00:00
|
|
|
interface.send("finishConvertingSpotifyPlaylist", str(id))
|
2020-04-17 10:31:47 +00:00
|
|
|
else:
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.warn("URL not supported yet")
|
2020-04-17 10:31:47 +00:00
|
|
|
result['error'] = "URL not supported yet"
|
|
|
|
return result
|
2020-04-10 14:12:21 +00:00
|
|
|
|
|
|
|
|
2020-04-16 08:37:02 +00:00
|
|
|
def addToQueue(dz, sp, url, settings, bitrate=None, interface=None):
|
2020-04-17 10:31:47 +00:00
|
|
|
global currentItem, queueList, queue
|
|
|
|
if not dz.logged_in:
|
|
|
|
return "Not logged in"
|
2020-06-19 09:08:07 +00:00
|
|
|
if type(url) is list:
|
|
|
|
queueItem = []
|
|
|
|
for link in url:
|
2020-06-19 18:14:20 +00:00
|
|
|
link = link.strip()
|
|
|
|
if link == "":
|
|
|
|
continue
|
2020-06-19 09:08:07 +00:00
|
|
|
logger.info("Generating queue item for: "+link)
|
|
|
|
item = generateQueueItem(dz, sp, link, settings, bitrate, interface=interface)
|
|
|
|
if type(item) is list:
|
|
|
|
queueItem += item
|
|
|
|
else:
|
|
|
|
queueItem.append(item)
|
|
|
|
else:
|
2020-06-19 18:14:20 +00:00
|
|
|
url = url.strip()
|
|
|
|
if url == "":
|
|
|
|
return False
|
2020-06-19 09:08:07 +00:00
|
|
|
logger.info("Generating queue item for: "+url)
|
|
|
|
queueItem = generateQueueItem(dz, sp, url, settings, bitrate, interface=interface)
|
2020-04-17 10:31:47 +00:00
|
|
|
if type(queueItem) is list:
|
|
|
|
for x in queueItem:
|
|
|
|
if 'error' in x:
|
2020-06-19 18:14:20 +00:00
|
|
|
logger.error(f"[{x['link']}] {x['error']}")
|
2020-04-17 10:31:47 +00:00
|
|
|
continue
|
|
|
|
if x['uuid'] in list(queueList.keys()):
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.warn(f"[{x['uuid']}] Already in queue, will not be added again.")
|
2020-04-17 10:31:47 +00:00
|
|
|
continue
|
|
|
|
if interface:
|
2020-05-06 10:06:56 +00:00
|
|
|
interface.send("addedToQueue", slimQueueItem(x))
|
2020-04-17 10:31:47 +00:00
|
|
|
queue.append(x['uuid'])
|
|
|
|
queueList[x['uuid']] = x
|
2020-06-19 09:08:07 +00:00
|
|
|
logger.info(f"[{x['uuid']}] Added to queue.")
|
2020-04-17 10:31:47 +00:00
|
|
|
else:
|
|
|
|
if 'error' in queueItem:
|
2020-06-19 18:14:20 +00:00
|
|
|
logger.error(f"[{queueItem['link']}] {queueItem['error']}")
|
2020-04-17 10:31:47 +00:00
|
|
|
if interface:
|
2020-06-05 08:47:54 +00:00
|
|
|
interface.send("errorMessage", queueItem['error'])
|
2020-04-17 10:31:47 +00:00
|
|
|
return False
|
|
|
|
if queueItem['uuid'] in list(queueList.keys()):
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.warn(f"[{queueItem['uuid']}] Already in queue, will not be added again.")
|
2020-04-17 10:31:47 +00:00
|
|
|
if interface:
|
2020-06-05 08:47:54 +00:00
|
|
|
interface.send("alreadyInQueue", {'uuid': queueItem['uuid'], 'title': queueItem['title']})
|
2020-04-17 10:31:47 +00:00
|
|
|
return False
|
|
|
|
if interface:
|
2020-05-06 10:06:56 +00:00
|
|
|
interface.send("addedToQueue", slimQueueItem(queueItem))
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.info(f"[{queueItem['uuid']}] Added to queue.")
|
2020-04-17 10:31:47 +00:00
|
|
|
queue.append(queueItem['uuid'])
|
|
|
|
queueList[queueItem['uuid']] = queueItem
|
|
|
|
nextItem(dz, interface)
|
|
|
|
return True
|
|
|
|
|
2020-04-10 14:12:21 +00:00
|
|
|
|
2020-04-15 12:49:40 +00:00
|
|
|
def nextItem(dz, interface=None):
|
2020-04-17 10:31:47 +00:00
|
|
|
global currentItem, queueList, queue
|
|
|
|
if currentItem != "":
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
if len(queue) > 0:
|
|
|
|
currentItem = queue.pop(0)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
if interface:
|
|
|
|
interface.send("startDownload", currentItem)
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.info(f"[{currentItem}] Started downloading.")
|
2020-04-17 10:31:47 +00:00
|
|
|
result = download(dz, queueList[currentItem], interface)
|
|
|
|
callbackQueueDone(result)
|
|
|
|
|
2020-04-10 14:12:21 +00:00
|
|
|
|
|
|
|
def callbackQueueDone(result):
|
2020-04-17 10:31:47 +00:00
|
|
|
global currentItem, queueList, queueComplete
|
|
|
|
if 'cancel' in queueList[currentItem]:
|
|
|
|
del queueList[currentItem]
|
|
|
|
else:
|
|
|
|
queueComplete.append(currentItem)
|
2020-05-08 16:06:43 +00:00
|
|
|
logger.info(f"[{currentItem}] Finished downloading.")
|
2020-04-17 10:31:47 +00:00
|
|
|
currentItem = ""
|
|
|
|
nextItem(result['dz'], result['interface'])
|
|
|
|
|
2020-04-11 13:43:59 +00:00
|
|
|
|
2020-04-11 14:24:53 +00:00
|
|
|
def getQueue():
|
2020-04-17 10:31:47 +00:00
|
|
|
global currentItem, queueList, queue, queueComplete
|
|
|
|
return (queue, queueComplete, queueList, currentItem)
|
|
|
|
|
2020-04-11 14:24:53 +00:00
|
|
|
|
2020-05-17 19:26:49 +00:00
|
|
|
def restoreQueue(pqueue, pqueueComplete, pqueueList, dz, interface):
|
|
|
|
global currentItem, queueList, queue, queueComplete
|
|
|
|
queueComplete = pqueueComplete
|
|
|
|
queueList = pqueueList
|
|
|
|
queue = pqueue
|
|
|
|
nextItem(dz, interface)
|
|
|
|
|
|
|
|
|
2020-04-15 12:49:40 +00:00
|
|
|
def removeFromQueue(uuid, interface=None):
|
2020-04-17 10:31:47 +00:00
|
|
|
global currentItem, queueList, queue, queueComplete
|
|
|
|
if uuid == currentItem:
|
|
|
|
if interface:
|
2020-06-05 08:47:54 +00:00
|
|
|
interface.send("cancellingCurrentItem", currentItem)
|
2020-04-17 10:31:47 +00:00
|
|
|
queueList[uuid]['cancel'] = True
|
|
|
|
elif uuid in queue:
|
|
|
|
queue.remove(uuid)
|
|
|
|
del queueList[uuid]
|
|
|
|
if interface:
|
|
|
|
interface.send("removedFromQueue", uuid)
|
|
|
|
elif uuid in queueComplete:
|
|
|
|
queueComplete.remove(uuid)
|
|
|
|
del queueList[uuid]
|
|
|
|
if interface:
|
|
|
|
interface.send("removedFromQueue", uuid)
|
|
|
|
|
2020-04-11 14:24:53 +00:00
|
|
|
|
2020-04-15 12:49:40 +00:00
|
|
|
def cancelAllDownloads(interface=None):
|
2020-04-17 10:31:47 +00:00
|
|
|
global currentItem, queueList, queue, queueComplete
|
|
|
|
queue = []
|
|
|
|
queueComplete = []
|
|
|
|
if currentItem != "":
|
|
|
|
if interface:
|
2020-06-05 08:47:54 +00:00
|
|
|
interface.send("cancellingCurrentItem", currentItem)
|
2020-04-17 10:31:47 +00:00
|
|
|
queueList[currentItem]['cancel'] = True
|
|
|
|
for uuid in list(queueList.keys()):
|
|
|
|
if uuid != currentItem:
|
|
|
|
del queueList[uuid]
|
|
|
|
if interface:
|
|
|
|
interface.send("removedAllDownloads", currentItem)
|
|
|
|
|
2020-04-14 14:48:13 +00:00
|
|
|
|
2020-04-15 12:49:40 +00:00
|
|
|
def removeFinishedDownloads(interface=None):
|
2020-04-17 10:31:47 +00:00
|
|
|
global queueList, queueComplete
|
|
|
|
for uuid in queueComplete:
|
|
|
|
del queueList[uuid]
|
|
|
|
queueComplete = []
|
|
|
|
if interface:
|
|
|
|
interface.send("removedFinishedDownloads")
|