Usage of mqtt app_name for client instance + publishing title information
This commit is contained in:
parent
062b249eaf
commit
89b113959d
49
spotify.py
49
spotify.py
@ -29,18 +29,19 @@ class librespot(object):
|
||||
self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME],
|
||||
shell=False,
|
||||
# We pipe the output to an internal pipe
|
||||
stderr=subprocess.PIPE)
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT)
|
||||
self.__state__ = None
|
||||
self.__preload_state__ = False
|
||||
self.__title__ = None
|
||||
self.__spot_id__ = None
|
||||
self.__spot_id_preload__ = None
|
||||
self.__title_preload__ = None
|
||||
self.__title_published__ = None
|
||||
self.set_state(False)
|
||||
self.set_title("")
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
output = self.__process__.stderr.readline()
|
||||
output = self.__process__.stdout.readline()
|
||||
# Polling returns None when the program is still running, return_code otherwise
|
||||
return_code = self.__process__.poll()
|
||||
if return_code is not None:
|
||||
@ -53,15 +54,15 @@ class librespot(object):
|
||||
out_txt = output.decode('utf-8').strip('\n').strip()
|
||||
out_txt = out_txt[out_txt.find(']') + 2:]
|
||||
#logger.debug("librespot output: %s", out_txt)
|
||||
# TODO: Parse for "librespot output: Loading <Here Ever After> with Spotify URI <spotify:track:0zckHMfaB6vT5o23ZVBLHJ>"
|
||||
if out_txt.lower().startswith("loading"):
|
||||
logger.debug("librespot: %s", out_txt)
|
||||
title = out_txt[out_txt.index("<") + 1:out_txt.index(">")]
|
||||
if self.__preload_state__:
|
||||
self.__spot_id_preload__ = out_txt.split("<")[2][:-1]
|
||||
logger.info("Upcomming Track-ID %s identified", self.__spot_id__)
|
||||
self.__title_preload__ = title
|
||||
logger.info("Upcomming Title %s identified", title)
|
||||
else:
|
||||
self.__spot_id__ = out_txt.split("<")[2][:-1]
|
||||
logger.info("Current Track-ID %s identified", self.__spot_id__)
|
||||
self.__title__ = title
|
||||
logger.info("Current Title %s identified", title)
|
||||
if "command=" in out_txt:
|
||||
command = out_txt[out_txt.find('command=') + 8:].strip().lower()
|
||||
logger.debug("librespot command: %s", command)
|
||||
@ -71,29 +72,19 @@ class librespot(object):
|
||||
self.set_state(command.split(',')[2].strip() == 'true')
|
||||
#
|
||||
self.__preload_state__ = False
|
||||
self.__spot_id__ = self.__spot_id_preload__
|
||||
if self.__title_preload__ is not None:
|
||||
self.__title__ = self.__title_preload__
|
||||
self.__title_preload__ = None
|
||||
#
|
||||
elif command in self.ON_CMD:
|
||||
self.set_state(True)
|
||||
elif command in self.OFF_CMD:
|
||||
self.set_state(False)
|
||||
if self.__state__:
|
||||
self.set_title(self.get_title_by_id())
|
||||
self.set_title(self.__title__)
|
||||
else:
|
||||
self.set_title("")
|
||||
|
||||
|
||||
def get_title_by_id(self):
|
||||
if self.__spot_id__ is None:
|
||||
return ""
|
||||
else:
|
||||
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=config.SPOTIFY_CLIENT_ID,client_secret=config.SPOTIFY_CLIENT_SECRET))
|
||||
try:
|
||||
track = sp.track(self.__spot_id__)
|
||||
except Exception:
|
||||
return None
|
||||
return track["artists"][0]["name"] + " - " + track["name"]
|
||||
|
||||
def set_state(self, target_state):
|
||||
if target_state != self.__state__:
|
||||
self.__state__ = target_state
|
||||
@ -101,14 +92,14 @@ class librespot(object):
|
||||
self.__state_callback__(self.__state__)
|
||||
|
||||
def set_title(self, title):
|
||||
if title != self.__title__:
|
||||
self.__title__ = title
|
||||
logger.info("spotify title changed to \"%s\"", self.__title__)
|
||||
self.__title_callback__(self.__title__)
|
||||
if self.__title_published__ != title:
|
||||
self.__title_published__= title
|
||||
logger.info("spotify title changed to \"%s\"", title)
|
||||
self.__title_callback__(title)
|
||||
|
||||
|
||||
def send_state_msg_mqtt(state):
|
||||
client= paho.Client("spotify")
|
||||
client= paho.Client(config.APP_NAME)
|
||||
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
|
||||
try:
|
||||
client.connect(config.MQTT_SERVER, 1883)
|
||||
@ -119,7 +110,7 @@ def send_state_msg_mqtt(state):
|
||||
logger.warning("Erro while sending state information")
|
||||
|
||||
def send_title_msg_mqtt(title):
|
||||
client= paho.Client("spotify")
|
||||
client= paho.Client(config.APP_NAME)
|
||||
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
|
||||
try:
|
||||
client.connect(config.MQTT_SERVER, 1883)
|
||||
|
Loading…
x
Reference in New Issue
Block a user