Cleaned up some code

This commit is contained in:
RemixDev 2020-09-12 13:08:28 +02:00
parent e81adde17d
commit d6106f30b7
7 changed files with 43 additions and 35 deletions

View File

@ -1,3 +1,3 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
__version__ = "1.2.22" __version__ = "1.3.0"

View File

@ -9,4 +9,4 @@ class deemix:
self.set = Settings(configFolder) self.set = Settings(configFolder)
self.dz = Deezer() self.dz = Deezer()
self.sp = SpotifyHelper(configFolder) self.sp = SpotifyHelper(configFolder)
self.qm = QueueManager() self.qm = QueueManager(self.sp)

View File

@ -10,7 +10,7 @@ from os import makedirs, remove, system as execute
from tempfile import gettempdir from tempfile import gettempdir
from time import sleep from time import sleep
from deemix.app.queueitem import QIConvertable, QISingle, QICollection from deemix.app.queueitem import QISingle, QICollection
from deemix.app.track import Track from deemix.app.track import Track
from deemix.utils.misc import changeCase from deemix.utils.misc import changeCase
from deemix.utils.pathtemplates import generateFilename, generateFilepath, settingsRegexAlbum, settingsRegexArtist, settingsRegexPlaylistFile from deemix.utils.pathtemplates import generateFilename, generateFilepath, settingsRegexAlbum, settingsRegexArtist, settingsRegexPlaylistFile
@ -91,12 +91,9 @@ def formatDate(date, template):
return template return template
class DownloadJob: class DownloadJob:
def __init__(self, dz, sp, queueItem, interface=None): def __init__(self, dz, queueItem, interface=None):
self.dz = dz self.dz = dz
self.sp = sp
self.interface = interface self.interface = interface
if isinstance(queueItem, QIConvertable) and queueItem.extra:
self.sp.convert_spotify_playlist(self.dz, queueItem, interface=self.interface)
self.queueItem = queueItem self.queueItem = queueItem
self.settings = queueItem.settings self.settings = queueItem.settings
self.bitrate = queueItem.bitrate self.bitrate = queueItem.bitrate

View File

@ -15,13 +15,14 @@ logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('deemix') logger = logging.getLogger('deemix')
class QueueManager: class QueueManager:
def __init__(self): def __init__(self, spotifyHelper=None):
self.queue = [] self.queue = []
self.queueList = {} self.queueList = {}
self.queueComplete = [] self.queueComplete = []
self.currentItem = "" self.currentItem = ""
self.sp = spotifyHelper
def generateQueueItem(self, dz, sp, url, settings, bitrate=None, albumAPI=None, interface=None): def generateQueueItem(self, dz, url, settings, bitrate=None, albumAPI=None, interface=None):
forcedBitrate = getBitrateInt(bitrate) forcedBitrate = getBitrateInt(bitrate)
bitrate = forcedBitrate if forcedBitrate else settings['maxBitrate'] bitrate = forcedBitrate if forcedBitrate else settings['maxBitrate']
if 'deezer.page.link' in url: if 'deezer.page.link' in url:
@ -270,15 +271,17 @@ class QueueManager:
collection, collection,
) )
elif type == "spotifytrack": elif type == "spotifytrack" and self.sp:
if not sp.spotifyEnabled: if not self.sp.spotifyEnabled:
logger.warn("Spotify Features is not setted up correctly.") logger.warn("Spotify Features is not setted up correctly.")
return QueueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled") return QueueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled")
try: try:
track_id = sp.get_trackid_spotify(dz, id, settings['fallbackSearch']) track_id = self.sp.get_trackid_spotify(dz, id, settings['fallbackSearch'])
except SpotifyException as e: except SpotifyException as e:
return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:]) return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:])
except Exception as e:
return QueueError(url, "Something went wrong: "+str(e))
if track_id != "0": if track_id != "0":
return self.generateQueueItem(dz, sp, f'https://www.deezer.com/track/{track_id}', settings, bitrate) return self.generateQueueItem(dz, sp, f'https://www.deezer.com/track/{track_id}', settings, bitrate)
@ -286,15 +289,17 @@ class QueueManager:
logger.warn("Track not found on deezer!") logger.warn("Track not found on deezer!")
return QueueError(url, "Track not found on deezer!", "trackNotOnDeezer") return QueueError(url, "Track not found on deezer!", "trackNotOnDeezer")
elif type == "spotifyalbum": elif type == "spotifyalbum" and self.sp:
if not sp.spotifyEnabled: if not self.sp.spotifyEnabled:
logger.warn("Spotify Features is not setted up correctly.") logger.warn("Spotify Features is not setted up correctly.")
return QueueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled") return QueueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled")
try: try:
album_id = sp.get_albumid_spotify(dz, id) album_id = self.sp.get_albumid_spotify(dz, id)
except SpotifyException as e: except SpotifyException as e:
return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:]) return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:])
except Exception as e:
return QueueError(url, "Something went wrong: "+str(e))
if album_id != "0": if album_id != "0":
return self.generateQueueItem(dz, sp, f'https://www.deezer.com/album/{album_id}', settings, bitrate) return self.generateQueueItem(dz, sp, f'https://www.deezer.com/album/{album_id}', settings, bitrate)
@ -302,21 +307,23 @@ class QueueManager:
logger.warn("Album not found on deezer!") logger.warn("Album not found on deezer!")
return QueueError(url, "Album not found on deezer!", "albumNotOnDeezer") return QueueError(url, "Album not found on deezer!", "albumNotOnDeezer")
elif type == "spotifyplaylist": elif type == "spotifyplaylist" and self.sp:
if not sp.spotifyEnabled: if not self.sp.spotifyEnabled:
logger.warn("Spotify Features is not setted up correctly.") logger.warn("Spotify Features is not setted up correctly.")
return QueueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled") return QueueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled")
try: try:
return sp.generate_playlist_queueitem(dz, id, bitrate, settings) return self.sp.generate_playlist_queueitem(dz, id, bitrate, settings)
except SpotifyException as e: except SpotifyException as e:
return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:]) return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:])
except Exception as e:
return QueueError(url, "Something went wrong: "+str(e))
else: else:
logger.warn("URL not supported yet") logger.warn("URL not supported yet")
return QueueError(url, "URL not supported yet", "unsupportedURL") return QueueError(url, "URL not supported yet", "unsupportedURL")
def addToQueue(self, dz, sp, url, settings, bitrate=None, interface=None): def addToQueue(self, dz, url, settings, bitrate=None, interface=None):
if not dz.logged_in: if not dz.logged_in:
if interface: if interface:
interface.send("loginNeededToDownload") interface.send("loginNeededToDownload")
@ -327,7 +334,7 @@ class QueueManager:
if link == "": if link == "":
return False return False
logger.info("Generating queue item for: "+link) logger.info("Generating queue item for: "+link)
return self.generateQueueItem(dz, sp, link, settings, bitrate, interface=interface) return self.generateQueueItem(dz, link, settings, bitrate, interface=interface)
if type(url) is list: if type(url) is list:
queueItem = [] queueItem = []
@ -381,10 +388,10 @@ class QueueManager:
self.queue.append(queueItem.uuid) self.queue.append(queueItem.uuid)
self.queueList[queueItem.uuid] = queueItem self.queueList[queueItem.uuid] = queueItem
self.nextItem(dz, sp, interface) self.nextItem(dz, interface)
return True return True
def nextItem(self, dz, sp, interface=None): def nextItem(self, dz, interface=None):
if self.currentItem != "": if self.currentItem != "":
return None return None
else: else:
@ -392,20 +399,24 @@ class QueueManager:
self.currentItem = self.queue.pop(0) self.currentItem = self.queue.pop(0)
else: else:
return None return None
if isinstance(self.queueList[self.currentItem], QIConvertable) and self.queueList[self.currentItem].extra:
logger.info(f"[{self.currentItem}] Converting tracks to deezer.")
self.sp.convert_spotify_playlist(dz, self.queueList[self.currentItem], interface=interface)
logger.info(f"[{self.currentItem}] Tracks converted.")
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], interface).start() DownloadJob(dz, self.queueList[self.currentItem], interface).start()
self.afterDownload(dz, sp, interface) self.afterDownload(dz, interface)
def afterDownload(self, dz, sp, interface): def afterDownload(self, dz, interface):
if self.queueList[self.currentItem].cancel: if self.queueList[self.currentItem].cancel:
del self.queueList[self.currentItem] del self.queueList[self.currentItem]
else: else:
self.queueComplete.append(self.currentItem) self.queueComplete.append(self.currentItem)
logger.info(f"[{self.currentItem}] Finished downloading.") logger.info(f"[{self.currentItem}] Finished downloading.")
self.currentItem = "" self.currentItem = ""
self.nextItem(dz, sp, interface) self.nextItem(dz, interface)
def getQueue(self): def getQueue(self):

View File

@ -6,7 +6,7 @@ from os import mkdir
import spotipy import spotipy
from spotipy.oauth2 import SpotifyClientCredentials from spotipy.oauth2 import SpotifyClientCredentials
from deemix.utils.localpaths import getConfigFolder from deemix.utils.localpaths import getConfigFolder
from deemix.app.queueitem import QIConvertable, QICollection from deemix.app.queueitem import QIConvertable
emptyPlaylist = { emptyPlaylist = {
'collaborative': False, 'collaborative': False,

View File

@ -142,7 +142,7 @@ class Track:
def parseData(self, dz, settings, trackAPI_gw, trackAPI, albumAPI_gw, albumAPI): def parseData(self, dz, settings, trackAPI_gw, trackAPI, albumAPI_gw, albumAPI):
self.discNumber = trackAPI_gw.get('DISK_NUMBER') self.discNumber = trackAPI_gw.get('DISK_NUMBER')
self.explicit = bool(int(trackAPI_gw.get('EXPLICIT_LYRICS') or "0")) self.explicit = bool(int(trackAPI_gw.get('EXPLICIT_LYRICS', "0")))
self.copyright = trackAPI_gw.get('COPYRIGHT') self.copyright = trackAPI_gw.get('COPYRIGHT')
self.replayGain = "" self.replayGain = ""
if 'GAIN' in trackAPI_gw: if 'GAIN' in trackAPI_gw:
@ -152,7 +152,7 @@ class Track:
self.contributors = trackAPI_gw['SNG_CONTRIBUTORS'] self.contributors = trackAPI_gw['SNG_CONTRIBUTORS']
self.lyrics = { self.lyrics = {
'id': trackAPI_gw.get('LYRICS_ID'), 'id': trackAPI_gw.get('LYRICS_ID', "0"),
'unsync': None, 'unsync': None,
'sync': None, 'sync': None,
'syncID3': None 'syncID3': None
@ -232,16 +232,16 @@ class Track:
self.album['trackTotal'] = albumAPI['nb_tracks'] self.album['trackTotal'] = albumAPI['nb_tracks']
self.album['recordType'] = albumAPI['record_type'] self.album['recordType'] = albumAPI['record_type']
self.album['barcode'] = albumAPI.get('upc') or self.album['barcode'] self.album['barcode'] = albumAPI.get('upc', self.album['barcode'])
self.album['label'] = albumAPI.get('label') or self.album['label'] self.album['label'] = albumAPI.get('label', self.album['label'])
self.album['explicit'] = bool(albumAPI.get('explicit_lyrics')) self.album['explicit'] = bool(albumAPI.get('explicit_lyrics', False))
if 'release_date' in albumAPI: if 'release_date' in albumAPI:
self.album['date'] = { self.album['date'] = {
'day': albumAPI["release_date"][8:10], 'day': albumAPI["release_date"][8:10],
'month': albumAPI["release_date"][5:7], 'month': albumAPI["release_date"][5:7],
'year': albumAPI["release_date"][0:4] 'year': albumAPI["release_date"][0:4]
} }
self.album['discTotal'] = albumAPI.get('nb_disk') self.album['discTotal'] = albumAPI.get('nb_disk', "1")
self.copyright = albumAPI.get('copyright') self.copyright = albumAPI.get('copyright')
if not self.album['pic']: if not self.album['pic']:
@ -267,7 +267,7 @@ class Track:
self.album['trackTotal'] = albumAPI_gw['NUMBER_TRACK'] self.album['trackTotal'] = albumAPI_gw['NUMBER_TRACK']
self.album['discTotal'] = albumAPI_gw['NUMBER_DISK'] self.album['discTotal'] = albumAPI_gw['NUMBER_DISK']
self.album['recordType'] = "Album" self.album['recordType'] = "Album"
self.album['label'] = albumAPI_gw.get('LABEL_NAME') or self.album['label'] self.album['label'] = albumAPI_gw.get('LABEL_NAME', self.album['label'])
if 'EXPLICIT_ALBUM_CONTENT' in albumAPI_gw and 'EXPLICIT_LYRICS_STATUS' in albumAPI_gw['EXPLICIT_ALBUM_CONTENT']: if 'EXPLICIT_ALBUM_CONTENT' in albumAPI_gw and 'EXPLICIT_LYRICS_STATUS' in albumAPI_gw['EXPLICIT_ALBUM_CONTENT']:
self.album['explicit'] = albumAPI_gw['EXPLICIT_ALBUM_CONTENT']['EXPLICIT_LYRICS_STATUS'] in [1,4] self.album['explicit'] = albumAPI_gw['EXPLICIT_ALBUM_CONTENT']['EXPLICIT_LYRICS_STATUS'] in [1,4]
if not self.album['pic']: if not self.album['pic']:

View File

@ -7,7 +7,7 @@ README = (HERE / "README.md").read_text()
setup( setup(
name="deemix", name="deemix",
version="1.2.22", version="1.3.0",
description="A barebone deezer downloader library", description="A barebone deezer downloader library",
long_description=README, long_description=README,
long_description_content_type="text/markdown", long_description_content_type="text/markdown",