optimazation, fix mpd update

This commit is contained in:
Lurkars 2022-02-11 15:54:54 +01:00
parent dfa222a5f2
commit 7b1fd5cbe4
2 changed files with 79 additions and 77 deletions

View File

@ -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,22 +270,24 @@ 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
@ -303,7 +300,6 @@ class Luniebox(object):
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():
@ -316,14 +312,13 @@ class Luniebox(object):
logging.getLogger('luniebox').warn(
"cannot resume spotify: " + self.current)
return True
elif self.service == PlayerService.MPD:
elif self.service == PlayerService.MPD and self.mpd_connect():
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)
mpd_uri = text.replace('mpd:', '')
self.mpd.add(mpd_uri)
self.mpd.play()
except Exception as exception:
logging.getLogger('luniebox').warning(
@ -339,7 +334,6 @@ class Luniebox(object):
logging.getLogger('luniebox').debug(
"play mpd: " + self.current)
elif self.resume and text == self.current:
if self.mpd_connect():
try:
self.mpd.setvol(self.volume)
self.mpd.play()

View File

@ -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 + "/"