diff --git a/config_example/config.py b/config_example/config.py index 8b99a72..36c15fd 100644 --- a/config_example/config.py +++ b/config_example/config.py @@ -15,10 +15,10 @@ DEVICE_NAME = "Multimedia" # __BASEPATH__ = os.path.abspath(os.path.dirname(__file__)) APP_NAME = "spotify" -LOGTARGET = 'logfile' # possible choices are: 'logfile' or 'stdout' +LOGTARGET = 'stdout' # possible choices are: 'logfile' or 'stdout' LOGLVL = 'DEBUG' LOGHOST = 'cutelog' LOGPORT = 19996 -formatter = report.LONG_FMT +formatter = report.SHORT_FMT diff --git a/requirements.txt b/requirements.txt index 8579e8b..171808a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,3 @@ paho-mqtt +spotipy + diff --git a/spotify.py b/spotify.py index 4feca3b..ad472eb 100644 --- a/spotify.py +++ b/spotify.py @@ -4,6 +4,10 @@ import paho.mqtt.client as paho import report import subprocess import time +import spotipy +from spotipy.oauth2 import SpotifyClientCredentials +import config +import json try: @@ -17,15 +21,19 @@ class librespot(object): ON_CMD = ['play', ] OFF_CMD = ['pause', 'stop', ] - def __init__(self, state_callback): + def __init__(self, state_callback, title_callback): logger.info("Starting Librespot...") self.__state_callback__ = state_callback + self.__title_callback__ = title_callback self.__process__ = subprocess.Popen(["librespot", "-v", "--name", config.DEVICE_NAME], shell=False, # We pipe the output to an internal pipe stderr=subprocess.PIPE) self.__state__ = None + self.__title__ = None + self.__spot_id__ = None self.set_state(False) + self.set_title("") def run(self): while True: @@ -42,6 +50,14 @@ 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 with Spotify URI " + if out_txt.lower().startswith("loading"): + 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.set_title("") if "command=" in out_txt: command = out_txt[out_txt.find('command=') + 8:].strip().lower() logger.debug("librespot command: %s", command) @@ -52,6 +68,21 @@ class librespot(object): self.set_state(True) 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("") + + 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__: @@ -59,18 +90,35 @@ class librespot(object): logger.info("spotify state changed to %s", self.__state__) 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__) + -def send_msg_mqtt(state): +def send_state_msg_mqtt(state): client= paho.Client("spotify") client.username_pw_set(config.MQTT_USER, config.MQTT_PASS) client.connect(config.MQTT_SERVER, 1883) - logger.info("Sending Spotify status information to mqtt %s = %s", config.MQTT_TOPIC, str(state)) - client.publish(config.MQTT_TOPIC, "true" if state else "false") + 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) + 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__': report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT) - ls = librespot(send_msg_mqtt) + ls = librespot(send_state_msg_mqtt, send_title_msg_mqtt) ls.run() + +