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 = """
[Unit]
Description=Spotify Service
After=network-online.target
Wants=network-online.target
After=mosquitto.target
[Service]
User=%(UID)d
Group=%(GID)d

View File

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