From 7b1fd5cbe42f1f8f1da0f4b1cca20c2c0d5cc972 Mon Sep 17 00:00:00 2001 From: Lurkars Date: Fri, 11 Feb 2022 15:54:54 +0100 Subject: [PATCH] optimazation, fix mpd update --- application/luniebox.py | 140 +++++++++++++++++++-------------------- application/spotifydl.py | 16 +++-- 2 files changed, 79 insertions(+), 77 deletions(-) diff --git a/application/luniebox.py b/application/luniebox.py index fc0e64c..115cfac 100644 --- a/application/luniebox.py +++ b/application/luniebox.py @@ -69,7 +69,6 @@ class Luniebox(object): self.zspotify_path = self.get_setting('spotify', 'zspotify_path') if self.zspotify_path and self.mpd: self.spotifydl_connect() - else: logging.getLogger('luniebox').info("spotify disabled") self.spotify = False @@ -82,9 +81,9 @@ class Luniebox(object): self.service = PlayerService.NONE if self.current != None: - if self.current.startswith("spotify:"): + if self.current.startswith('spotify:'): self.service = PlayerService.SPOTIFY - elif self.current.startswith("mpd:"): + elif self.current.startswith('mpd:'): self.service = PlayerService.MPD self.volume_max = int(self.get_setting('luniebox', 'volume_max', 100)) @@ -160,12 +159,8 @@ class Luniebox(object): logging.getLogger('luniebox').warn("spotifydl disabled!") return False - username = self.spotify.get_setting("username") - password = self.spotify.get_setting("password") - root = self.get_setting('mpd', 'library_path') try: - self.spotifydl = SpotifyDL( - self.zspotify_path, username, password, root) + self.spotifydl = SpotifyDL(luniebox) logging.getLogger('luniebox').info("spotifydl enabled!") return True except Exception as exception: @@ -275,85 +270,84 @@ class Luniebox(object): def play(self, text): if text != "": - if text.startswith("spotify:"): + if text.startswith('spotify:') and self.spotify: self.service = PlayerService.SPOTIFY - elif text.startswith("mpd:"): + elif text.startswith('mpd:') and self.mpd: self.service = PlayerService.MPD + else: + self.service = PlayerService.NONE if self.service == PlayerService.SPOTIFY and self.spotifydl_connect(): downloadStatus = self.spotifydl.downloadStatus(text) - if downloadStatus == SpotifyDLStatus.FINISHED: - self.mpd.update(text.replace('mpd:', '')) + if downloadStatus == SpotifyDLStatus.FINISHED and self.mpd_connect(): + self.mpd.update() + self.mpd.idle('update') self.service = PlayerService.MPD elif self.get_setting('spotify', 'auto_download') == 'True' and downloadStatus == SpotifyDLStatus.NONE or downloadStatus == SpotifyDLStatus.ERROR: self.spotifydl.download(text) - if self.service == PlayerService.SPOTIFY: + if self.service == PlayerService.SPOTIFY and self.spotify_connect(): if text != self.current: - if self.spotify_connect(): - self.spotify.volume(self.volume) - if self.spotify.play(text): - self.current = text - self.set_setting( - 'luniebox', 'current', self.current) - self.resume = False - logging.getLogger('luniebox').debug( - "play spotify: " + self.current) - else: - logging.getLogger('luniebox').warn( - "cannot play spotify: " + self.current) - elif self.resume and text == self.current: - if self.spotify_connect(): - self.spotify.volume(self.volume) - play = self.current - if self.spotify.is_active(): - play = None - if self.spotify.play(play): - self.resume = False - logging.getLogger('luniebox').debug( - "resume spotify: " + self.current) - else: - logging.getLogger('luniebox').warn( - "cannot resume spotify: " + self.current) - return True - elif self.service == PlayerService.MPD: - if text != self.current: - if self.mpd_connect(): - try: - self.mpd.setvol(self.volume) - self.mpd.clear() - text = text.replace('mpd:', '') - self.mpd.add(text) - self.mpd.play() - except Exception as exception: - logging.getLogger('luniebox').warning( - "cannot not play mpd '" + text + "': " + str(exception)) - return False + self.spotify.volume(self.volume) + if self.spotify.play(text): self.current = text - self.set_setting('luniebox', 'current', self.current) + self.set_setting( + 'luniebox', 'current', self.current) self.resume = False - if text.startswith('spotify:'): - logging.getLogger('luniebox').debug( - "play spotify from mpd: " + text) - else: - logging.getLogger('luniebox').debug( - "play mpd: " + self.current) + logging.getLogger('luniebox').debug( + "play spotify: " + self.current) + else: + logging.getLogger('luniebox').warn( + "cannot play spotify: " + self.current) elif self.resume and text == self.current: - if self.mpd_connect(): - try: - self.mpd.setvol(self.volume) - self.mpd.play() - except Exception as exception: - logging.getLogger('luniebox').warning( - "cannot not resume mpd '" + self.current + "': " + str(exception)) - return False + self.spotify.volume(self.volume) + play = self.current + if self.spotify.is_active(): + play = None + if self.spotify.play(play): self.resume = False - if text.startswith('spotify:'): - logging.getLogger('luniebox').debug( - "resume spotify from mpd: " + text) - else: - logging.getLogger('luniebox').debug( - "resume mpd: " + self.current) + logging.getLogger('luniebox').debug( + "resume spotify: " + self.current) + else: + logging.getLogger('luniebox').warn( + "cannot resume spotify: " + self.current) + return True + elif self.service == PlayerService.MPD and self.mpd_connect(): + if text != self.current: + try: + self.mpd.setvol(self.volume) + self.mpd.clear() + mpd_uri = text.replace('mpd:', '') + self.mpd.add(mpd_uri) + self.mpd.play() + except Exception as exception: + logging.getLogger('luniebox').warning( + "cannot not play mpd '" + text + "': " + str(exception)) + return False + self.current = text + self.set_setting('luniebox', 'current', self.current) + self.resume = False + if text.startswith('spotify:'): + logging.getLogger('luniebox').debug( + "play spotify from mpd: " + text) + else: + logging.getLogger('luniebox').debug( + "play mpd: " + self.current) + elif self.resume and text == self.current: + try: + self.mpd.setvol(self.volume) + self.mpd.play() + except Exception as exception: + logging.getLogger('luniebox').warning( + "cannot not resume mpd '" + self.current + "': " + str(exception)) + return False + self.resume = False + if text.startswith('spotify:'): + logging.getLogger('luniebox').debug( + "resume spotify from mpd: " + text) + else: + logging.getLogger('luniebox').debug( + "resume mpd: " + self.current) return True elif text != None: logging.getLogger('luniebox').info( diff --git a/application/spotifydl.py b/application/spotifydl.py index 7b22b34..cdd1234 100644 --- a/application/spotifydl.py +++ b/application/spotifydl.py @@ -23,19 +23,27 @@ class SpotifyDLStatus(Enum): class SpotifyDL(): - def __init__(self, zspotify_path, username, password, root, credentialsLocation=defaultCredentialsLocation): - if zspotify_path: - self.zspotify_path = zspotify_path - else: + def __init__(self, luniebox, credentialsLocation=defaultCredentialsLocation): + + if not luniebox.zspotify_path: raise ValueError("No zspotify path provivded!") + else: + self.zspotify_path = luniebox.zspotify_path + if credentialsLocation: self.credentialsLocation = credentialsLocation else: raise ValueError("No credentialsLocation provivded!") + + username = luniebox.spotify.get_setting("username") if not username: raise ValueError("No username provided!") + + password = luniebox.spotify.get_setting("password") if not password: raise ValueError("No password provided!") + + root = luniebox.get_setting('mpd', 'library_path') if root: if not root.endswith("/"): root = root + "/"