Compare commits

..

No commits in common. "e023c4a06a1d2f8847f2fdf210641d26b591b625" and "d8096b0f31de4335b9b7ca5ab7c097d6f8f21822" have entirely different histories.

2 changed files with 22 additions and 37 deletions

View File

@ -6,8 +6,7 @@ import sys
SERVICE_FILE = """ SERVICE_FILE = """
[Unit] [Unit]
Description=Spotify Service Description=Spotify Service
After=network-online.target After=mosquitto.target
Wants=network-online.target
[Service] [Service]
User=%(UID)d User=%(UID)d
Group=%(GID)d Group=%(GID)d

View File

@ -30,10 +30,8 @@ class librespot(object):
# We pipe the output to an internal pipe # We pipe the output to an internal pipe
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
self.__state__ = None self.__state__ = None
self.__preload_state__ = False
self.__title__ = None self.__title__ = None
self.__spot_id__ = None self.__spot_id__ = None
self.__spot_id_preload__ = None
self.set_state(False) self.set_state(False)
self.set_title("") self.set_title("")
@ -51,37 +49,30 @@ class librespot(object):
if output: if output:
out_txt = output.decode('utf-8').strip('\n').strip() out_txt = output.decode('utf-8').strip('\n').strip()
out_txt = out_txt[out_txt.find(']') + 2:] out_txt = out_txt[out_txt.find(']') + 2:]
#logger.debug("librespot output: %s", out_txt) logger.debug("librespot output: %s", out_txt)
# TODO: Parse for "librespot output: Loading <Here Ever After> with Spotify URI <spotify:track:0zckHMfaB6vT5o23ZVBLHJ>" # TODO: Parse for "librespot output: Loading <Here Ever After> with Spotify URI <spotify:track:0zckHMfaB6vT5o23ZVBLHJ>"
if out_txt.lower().startswith("loading"): if out_txt.lower().startswith("loading"):
logger.debug("librespot: %s", out_txt)
if self.__preload_state__:
self.__spot_id_preload__ = out_txt.split("<")[2][:-1]
logger.info("Upcomming Track-ID %s identified", self.__spot_id__)
else:
self.__spot_id__ = out_txt.split("<")[2][:-1] self.__spot_id__ = out_txt.split("<")[2][:-1]
logger.info("Current Track-ID %s identified", self.__spot_id__) logger.info("Track-ID %s identified", self.__spot_id__)
if self.__state__:
self.set_title(self.get_title_by_id())
else:
self.set_title("")
if "command=" in out_txt: if "command=" in out_txt:
command = out_txt[out_txt.find('command=') + 8:].strip().lower() command = out_txt[out_txt.find('command=') + 8:].strip().lower()
logger.debug("librespot command: %s", command) logger.debug("librespot command: %s", command)
if command.startswith("preload"):
self.__preload_state__ = True
if command.startswith("load"): if command.startswith("load"):
self.set_state(command.split(',')[2].strip() == 'true') self.set_state(command.split(',')[2].strip() == 'true')
# #
self.__preload_state__ = False if command in self.ON_CMD:
self.__spot_id__ = self.__spot_id_preload__
#
elif command in self.ON_CMD:
self.set_state(True) self.set_state(True)
elif command in self.OFF_CMD: if command in self.OFF_CMD:
self.set_state(False) self.set_state(False)
if self.__state__: if self.__state__:
self.set_title(self.get_title_by_id()) self.set_title(self.get_title_by_id())
else: else:
self.set_title("") self.set_title("")
def get_title_by_id(self): def get_title_by_id(self):
if self.__spot_id__ is None: if self.__spot_id__ is None:
return "" return ""
@ -109,24 +100,19 @@ class librespot(object):
def send_state_msg_mqtt(state): def send_state_msg_mqtt(state):
client= paho.Client("spotify") client= paho.Client("spotify")
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS) client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
try:
client.connect(config.MQTT_SERVER, 1883) client.connect(config.MQTT_SERVER, 1883)
topic = config.MQTT_TOPIC + "/state" topic = config.MQTT_TOPIC + "/state"
logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state)) logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state))
client.publish(topic, "true" if state else "false") client.publish(topic, "true" if state else "false")
except (socket.timeout, OSError) as e:
logger.warning("Erro while sending state information")
def send_title_msg_mqtt(title): def send_title_msg_mqtt(title):
client= paho.Client("spotify") client= paho.Client("spotify")
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS) client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
try:
client.connect(config.MQTT_SERVER, 1883) client.connect(config.MQTT_SERVER, 1883)
topic = config.MQTT_TOPIC + "/title" topic = config.MQTT_TOPIC + "/title"
logger.info("Sending Spotify status information to mqtt %s = %s", topic, title) logger.info("Sending Spotify status information to mqtt %s = %s", topic, title)
client.publish(topic, title) client.publish(topic, title)
except (socket.timeout, OSError) as e:
logger.warning("Erro while sending title information")
if __name__ == '__main__': if __name__ == '__main__':